验证中...
码云 Gitee IDE 全新上线——支持 Git 管理的轻量在线编码环境
DBHelper.java
原始数据 复制代码
import java.sql.*;
public class DBHelper {
public static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String URL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=ConstructionDB";
public static final String USERNAME = "sa";
public static final String PASSWORD = "sa1234";
public Connection CONNECTION = null;
private PreparedStatement psm = null;
private ResultSet rs = null;
/**
* 使用的时候仅需三步:
* 第一步:DBHelper helper = new DBHelper();
* 第二步:ResultSet rs = helper.sql(sql).query();
* 或: int result = helper.sql(sql).update();
* 第三步:helper.close();
*/
public DBHelper() {
try {
Class.forName(DRIVER);
CONNECTION = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void close() {
if (null != CONNECTION) {
try {
CONNECTION.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (null != psm) {
try {
psm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public DBHelper sql(String sql) {
try {
psm = CONNECTION.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return this;
}
public ResultSet query() {
try {
rs = psm.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public int update() {
int result = 0;
try {
psm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助