JSP连接SQL数据库

来源:百度知道 编辑:UC知道 时间:2024/05/31 06:32:44
求助:我们在做毕业设计做网页但必须有后台。怎么连接数据库呢。。又在哪里打代码呢?。。是在JSP里打代码还是在SQL里面配置呢?求助。。。。。本人Q119748446.。。
那这串代码。打在何处呢?

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