JSP连接SQL数据库
来源:百度知道 编辑:UC知道 时间:2024/05/31 06:32:44
那这串代码。打在何处呢?
public class DBMain {
//驱动名称
private static final String DRIVE = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//数据库连接语句
private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=test";
//数据库用户名和密码
private final String SQL_NAME = "sa";
private final String SQL_PASS = "sa";
private Connection con = null;
public PreparedStatement getPreparedStatement(String sqlStr)
throws ClassNotFoundException, SQLException {
PreparedStatement pst = null;
// 加载数据库驱动
Class.forName(DRIVE);
// 获得数据库连接
if (con == null || con.isClosed()) {
con = DriverManager.getConnection(URL, SQL_NAME, SQL_PASS);
}
pst = con.prepareStatement(sqlStr);
return pst;
}
/**
* 关闭数据库连接
* @throws SQLException
*/
public void releaes() throws SQLException {
if (con != null) {
con