标签:
package com.pb.news.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import com.pb.news.util.ConfigManager;
//基类:数据库操作通用类
public class BaseDao {
protected Connection conn;
protected PreparedStatement ps;
protected Statement st;
protected ResultSet rs;
//1.获取数据库连接
public boolean getConnection(){
//读取配置信息
String driver=ConfigManager.getInstance().getString("jdbc.driver_class");
String url=ConfigManager.getInstance().getString("jdbc.connection.url");
String username=ConfigManager.getInstance().getString("jdbc.connection.username");
String password=ConfigManager.getInstance().getString("jdbc.connection.password");
try {
//加载驱动
Class.forName(driver);
//与数据库建立连接
conn=DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
//获取数据库连接2(数据源连接JNDI)
public Connection getConnection2(){
try {
//初始化一个上下文
Context cxt=new InitialContext();
//获取与逻辑名相关联的数据源对象
DataSource ds=(DataSource) cxt.lookup("java:comp/env/jdbc/news");
//拿到数据源对象的连接
conn=ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//2.增删改, 假设要册除 delete from news_Detail where id=? and title=?
public int excuteUpdate(String sql,Object[] params){
int updateRows=0;//影响的行数
getConnection();
try {
ps=conn.prepareStatement(sql);
//填充占位符
for(int i=0;i<params.length;i++){
ps.setObject(i+1,params[i]);
}
updateRows=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return updateRows;
}
//3.查看
public ResultSet excuteSQL(String sql,Object[] params){
getConnection();
try {
ps=conn.prepareStatement(sql);
//填充占位符
for(int i=0;i<params.length;i++){
ps.setObject(i+1,params[i]);
}
rs=ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
//4.关闭资源
public boolean closeResource(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
return true;
}
}
标签:
原文地址:http://www.cnblogs.com/JesseCary/p/4507599.html