码迷,mamicode.com
首页 > 其他好文 > 详细

NEWS-包名-baseTest-类名-BaseDao

时间:2015-05-16 13:11:40      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

package baseTest;

import java.sql.Connection;
import java.sql.DriverManager;
//通过配置文件读取BASEDAO
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class BaseDao {
// private static final String driver=ConfigManager.getInstance().getString("jdbc_driver_class");
// private static final String url=ConfigManager.getInstance().getString("jdbc.connection.url");
// private static final String username=ConfigManager.getInstance().getString("jdbc.connection.username");
// private static final String pwd=ConfigManager.getInstance().getString("jdbc.connection.[assword");
//
private static Connection con;
private static PreparedStatement ps;
private static ResultSet rs;
//
//
// public static Connection getConnection(){
// try {
// Class.forName(driver);
// con=DriverManager.getConnection(url,username,pwd);
// return con;
// } catch (ClassNotFoundException | SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// return null;
// }


public static ResultSet executeQuery(String sql,Object[]param){
getConnection2();
try {
ps=con.prepareStatement(sql);
if(param!=null&&param.length>0){
for (int i = 0; i < param.length; i++) {
ps.setObject(i+1, param[i]);
}
}
rs=ps.executeQuery();
return rs;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

}
public static int executeUpdate(String sql,Object[]param){
getConnection2();
try {
ps=con.prepareStatement(sql);
if(param!=null&&param.length>0){
for (int i = 0; i < param.length; i++) {
ps.setObject(i+1, param[i]);
}
}
int row=ps.executeUpdate();
return row;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
//关闭资源
}
public static void close(){

}
/**
* 使用JNDID读取数据源和连接池配置
* 1.在tomact服务器中添加数据库驱动,
* 将ORACLE数据库的ojdbc14.jar包复制到Tomact安装目录的lib文件夹中
* 2.配置Tomact服务器的配置文件
* 在tomact服务器的conf/context.xml文件中添加<Resource/>配置信息
*
*/
public static Connection getConnection2(){
//初始化上下文
Context cxt;
try {
cxt = new InitialContext();
//获取与逻辑名相关联的数据源对象
DataSource ds=(DataSource) cxt.lookup("java:comp/env/jdbc/news");
con=ds.getConnection();
} catch (NamingException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}

}

NEWS-包名-baseTest-类名-BaseDao

标签:

原文地址:http://www.cnblogs.com/JesseCary/p/4507592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!