import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao{
protected Connection conn=null;
protected PreparedStatement prst=null;
protected ResultSet rs=null;
private final String connString="jdbc:sqlserver://localhost;databasename=xxx";
private final String user="xxx";
private final String pwd="xxxx";
protected void openConn(){
try {
//SQLSERVER
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//ORACLE
Class.forName("Oracle.jdbc.driver.OracleDriver");
//mysql
Class.forName("com.mysql.jdbc.Drive");
conn=DriverManager.getConnection(connString, user, pwd);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
protected void closeConn(){
try {
if(this.prst!=null)
prst.close();
if(this.rs!=null)
rs.close();
if(this.conn!=null)
conn.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
protected void executeUpdate(String sql, Object... para){
try {
prst = conn.prepareStatement(sql);
for (int i = 0; i < para.length; i++) {
prst.setObject(i + 1, para[i]);
}
prst.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
protected ResultSet executeQuery(String sql, Object... para) {
try {
prst = conn.prepareStatement(sql);
for (int i = 0; i < para.length; i++) {
prst.setObject(i + 1, para[i]);
}
this.rs = prst.executeQuery();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return this.rs;
}
}
jdbc连接,方便自己查阅
最新推荐文章于 2021-02-06 16:46:35 发布