该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
通用数据库连接类BaseDao
package com.demo.Dao;
import java.sql.*;
public class BaseDao {
//SqlServer
/**
* 加载驱动字符串
*/
private final String className="com.microsoft.sqlserver.jdbc.SQLServerDriver";
/**
* 连接URL
*/
private final String url="jdbc:sqlserver://localhost:1433;database=epet";
/**
* 数据库登陆账号
*/
private final String userName="sa";
/**
* 数据库登陆密码
*/
private final String pwd="ok";
//Oracle
//private String className="oracle.jdbc.driver.OracleDriver";
//private String url="jdbc:oracle:thin:@localhost:1521:orcl";
//private String userName="system";
//private String pwd="ok";
/**
* 数据库连接类
*/
private Connection conn;
/**
* 发送指令
*/
private PreparedStatement ps;
/**
* 结果集
*/
private ResultSet rs;
/**
* 加载驱动
* 获得connection对象
*/
private void getConnection(){
try {
Class.forName(className);
conn=DriverManager.getConnection(url,userName,pwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 执行查询操作,返回结果集
* @param sql sql语句
* @param petm 参数数组
* @return 返回结果集
*/
public ResultSet executeResult(String sql,Object[] petm){
try {
getConnection();
ps=conn.prepareStatement(sql);
if(petm!=null){
for(int i=0;i
ps.setObject(i+1,petm[i]);
}
}
rs=ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/**
* 执行增加、删除、修改操作
* @param sql sql语句
* @param petm 参数数组
* @return 返回受影响行数
*/
public int exercuteUpdate(String sql,Object [] petm){
int num=0;
try {
getConnection();
ps=conn.prepareStatement(sql);
if(petm!=null){
for(int i=0;i
ps.setObject(i+1,petm[i]);
}
}
num=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
/**
* 关闭数据库连接
*/
public void close(){
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}