📄 basedao.java
字号:
package model.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DBURL = "jdbc:sqlserver://localhost:1433;DataBaseName=addressBook";
private static final String DBNAME = "sa";
private static final String DBPASS = "";
// 连接
public static Connection getCon() {
Connection con = null;
try {
Class.forName(DBDRIVER);
con = DriverManager.getConnection(DBURL, DBNAME, DBPASS);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
return con;
}
// 关闭连接
public static void CloseAll(Connection con, PreparedStatement pstmt,
ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
// 增删改通用方法
public static int executeSQL(String sql, String[] param) {
System.out.println(sql);
System.out.println(param.length);
Connection con = null;
PreparedStatement pstmt = null;
int num = 0;
try {
con = getCon();
pstmt = con.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setString(i + 1, param[i]);
}
}
num = pstmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
CloseAll(con, pstmt, null);
}
return num;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -