JSP连接sql server 2008数据库

2024-12-03 10:55:33
推荐回答(1个)
回答1:

package com.pet.dao;

import java.sql.*;

public class BaseDao {

private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";

private static final String URL = "jdbc:oracle:thin:@192.168.3.31:1522:accp11g";

private static final String USER = "dupengchao";

private static final String PASS = "dupengchao";

//变量
protected Connection con = null;

protected PreparedStatement pstmt = null;

protected ResultSet rs = null;

//两个方法
/**
* 获取连接对象
* */
public Connection getConn() throws ClassNotFoundException,SQLException{
Connection con = null;

//1.加载驱动
Class.forName(DRIVER);
//2.获取连接
con = DriverManager.getConnection(URL,USER,PASS);

return con;
}
/**
* 释放所有资源
* */
public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection con){
try {
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}