连接数据库的5个步骤,加载驱动JDBC,创建数据库连接,执行SQL语句,获得查询结果,关闭连接,但是我的项目里没有调用executeUpdate()或executeQuery()方法的代码,是不是不对?以...
连接数据库的5个步骤,加载驱动JDBC,创建数据库连接,执行SQL语句,获得查询结果,关闭连接,但是我的项目里没有调用executeUpdate()或executeQuery()方法的代码,是不是不对?以下是我工程中DB.java里的代码:
public class DB {
private static String sDBDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String sConnStr = "jdbc:sqlserver://localhost:1433;DatabaseName=shop";
private static String name = "sa";
private static String password = "911021";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName();
conn = DriverManager.getConnection(sConnStr, name, password);
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
conn = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void closePreparedStatement(PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
pstmt = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void closed(ResultSet rs, Statement stmt, Connection conn) {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void closed(ResultSet rs, PreparedStatement stmt, Connection conn) {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void closed(Statement stmt, Connection conn) {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
展开