import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.swing.JOptionPane;public class ConnectDB {
private static Connection conn;
public static Statement st;
public static ResultSet rs;
private static String url;
static {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=E:\\EclipseWorkSpace\\AddressList\\source\\DB.mdb";
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, "数据库驱动类加载出错!" + ex.getMessage(),
"加载出错", JOptionPane.ERROR_MESSAGE);
}
} public static Connection createConnection() {
try {
conn = DriverManager.getConnection(url, "amin", "admin");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "数据库连接错误" + "SQL exception:"
+ ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
return conn;
} public static Statement getStatement() {
return st;
} public static ResultSet getResultSet() {
return rs;
} public static void closeAll() {
if (conn == null && st == null && rs == null)
return;
try {
rs.close();
st.close();
if (!conn.isClosed())
{
conn.close();
if(conn.isClosed())
System.out.println("关闭成功");
}
} catch (SQLException eConn) {
JOptionPane.showMessageDialog(null, "关闭数据库连接失败!" + eConn.getMessage(),
"关闭出错", JOptionPane.ERROR_MESSAGE);
}
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.swing.JOptionPane;public class ConnectDB {
private static Connection conn;
public static Statement st;
public static ResultSet rs;
private static String url;
static {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=E:\\EclipseWorkSpace\\AddressList\\source\\DB.mdb";
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, "数据库驱动类加载出错!" + ex.getMessage(),
"加载出错", JOptionPane.ERROR_MESSAGE);
}
} public static Connection createConnection() {
try {
conn = DriverManager.getConnection(url, "amin", "admin");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "数据库连接错误" + "SQL exception:"
+ ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
return conn;
} public static Statement getStatement() {
return st;
} public static ResultSet getResultSet() {
return rs;
} public static void closeAll() {
if (conn == null && st == null && rs == null)
return;
try {
rs.close();
st.close();
if (!conn.isClosed())
{
conn.close();
if(conn.isClosed())
System.out.println("关闭成功");
}
} catch (SQLException eConn) {
JOptionPane.showMessageDialog(null, "关闭数据库连接失败!" + eConn.getMessage(),
"关闭出错", JOptionPane.ERROR_MESSAGE);
}
}
}
return st;//你这个都还没有初始话啊
} public static ResultSet getResultSet() {
return rs;//你这个都还没有初始话啊
}我觉得你的url最好是放到property配置文件里来读取,那样灵活性更大些呵呵
个人的一点愚见
public static void closeAll() {
try{if(rs!=null){rs.close();rs=null;}}catch(SQLException e){e.printStacktrace;}
try{if(ps!=null){ps.close();ps=null;}}catch(SQLException e){e.printStacktrace;}
try{if(con!=null){con.close();con=null;}}catch(SQLException e){e.printStacktrace;}
}关闭的时候一般按照这个顺序
其次,如果只有一个连接对象,请考虑同步问题;如果考虑到并发连接,可以使用对象池。
driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:1433
username=sa
password=sa
try{if(rs!=null){rs.close();rs=null;}}catch(SQLException e){e.printStacktrace;}
try{if(ps!=null){ps.close();ps=null;}}catch(SQLException e){e.printStacktrace;}
try{if(con!=null){con.close();con=null;}}catch(SQLException e){e.printStacktrace;}
}
这样封装不太好吧
2,关闭时的第一句有问题。改正上面两个之后我们再来逐步完善吧。一般来说,为了解决多线程访问的问题,强烈建议不要将数据库连接的三个对象设为成员变量。
import java.sql.*;
import java.util.ResourceBundle;/**
* 说明:数据库工具类
*/
public class DBUtils { /**web项目源文件根目录下DBconnection.propertise的属性文件*/
private static final String DB_FILE_NAME = "DBconnection"; @SuppressWarnings("unused")
private static DBUtils connDB = new DBUtils();
/** 驱动 */
private static String driverManager; /** 数据库 */
private static String url; /** 用户名 */
private static String user; private static String pwd; /* static methods */ /* constructors */ /** 加载驱动 */
private DBUtils() {
try {
this.getProperties();
Class.forName(driverManager);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /** 读取属性文件 */
private void getProperties() throws IOException {
try { ResourceBundle res = ResourceBundle.getBundle(DB_FILE_NAME);
driverManager = res.getString("DriverManager").trim();
url = res.getString("URL").trim();
user = res.getString("UserName").trim();
pwd = res.getString("Password").trim(); // Properties prop = new Properties();
// InputStream input = getClass().getResourceAsStream(
// "DBconnection.properties");
// prop.load(input);
// driverManager = prop.getProperty("DriverManager");
// url = prop.getProperty("URL");
// user = prop.getProperty("UserName");
// pwd = prop.getProperty("Password"); } catch (Exception e) {
System.out
.println("cn.com.siwi.j2se.fc.util.DBUtils ** 读取属性文件异常");
System.out.println(e.getMessage());
}
} /* fields */ /* methods */ /** 单例,只读取一次配置文件 */
public static DBUtils getInstance() {
return connDB;
} /** 获取连接 */
public Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
} /**
* 执行select操作
*
* @param isCloseConnection
* 是否关闭 connection链接(true关闭connection和statement,false不关闭)
*/
public int executeQuery(Connection conn, String sql,
boolean isCloseConnection) {
// 是否正确执行,返回 >0正确,<=0有异常
int result = -1;
Statement stmt = null;
try {
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.executeQuery(sql);
conn.commit();
result = 1;
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
close(stmt);
if (isCloseConnection) {
close(conn);
}
}
return result;
} /**
* 执行insert,update,delete操作
*
* @param isCloseConnection
* 是否关闭 connection链接(true关闭connection和statement,false不关闭)
*/
public int executeUpdate(Connection conn, String sql,
boolean isCloseConnection) {
// 是否正确执行,返回 >0正确,<=0有异常
int result = -1;
Statement stmt = null;
try {
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
conn.commit();
result = 1;
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
close(stmt);
if (isCloseConnection) {
close(conn);
}
}
return result;
} /**
* 获取一个表 参数table 中一共有多少行数据("select count(*) from " + tableName)
*
* @param tableName
* 数据库中表的名字
* @param isCloseConnection
* 是否关闭 connection链接(true关闭con,false不关闭)
*/
public int getRowCount(Connection conn, String tableName,
boolean isCloseConnection) {
String sql = "select count(*) from " + tableName;
Statement stmt = null;
ResultSet rs = null;
int rowCount = 0;
try {
stmt = getConnection().createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
rowCount = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(rs);
close(stmt);
if (isCloseConnection) {
close(conn);
}
}
return rowCount;
} public static void close(Connection conn, Statement stmt, ResultSet rs) {
close(rs);
close(stmt);
close(conn);
} /**
* // 关闭连接
*/
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
System.out.println(e.getMessage() + "/n"
+ "cn.com.siwi.j2se.fc.util.DBUtils ** 关闭conn异常");
}
}
} public static void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
System.out.println(e.getMessage() + "/n"
+ "cn.com.siwi.j2se.fc.util.DBUtils ** 关闭rs异常");
}
}
} public static void close(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
System.out.println(e.getMessage() + "/n"
+ "cn.com.siwi.j2se.fc.util.DBUtils ** 关闭stmt异常");
}
}
} public static void main(String[] args) { // try {
// Connection c = DBUtils.getInstance().getConnection();
// ResultSet rs = c.getMetaData().getTables("", "", "", null);
// int i = 1;
// while (rs.next()) {
// // System.out.println(rs.getString("table_name"));
// if (rs.getString("TABLE_NAME").indexOf("sort") > -1) {
// System.out.println(rs.getString("table_name"));
// }
// if (rs.getString("TABLE_NAME").indexOf("_") < 0) {
// System.out.println(rs.getString("table_name"));
// }
// }
// System.out.println(i);
// } catch (Exception e) {
//
// }
// ConnDB.insertInto_fenci_province_table();
} /* extends methods */ /* implements methods */ /* properties */
}
#DBconnection.propertise
URL=jdbc:mysql://127.0.0.1/lucenesearch?useUnicode=true&characterEncoding=GBK
Password=123456
UserName=root
DriverManager=com.mysql.jdbc.Driver
呵呵,楼主还带改进啊,很多东西是不能设置成static的,并发的时候,数据岂非乱套了