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

NEWS-包名-baseTest-类名-baeseDao

时间:2015-05-16 13:08:29      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:

package baseTest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class baeseDao {
private static final String driver="oracle.jdbc.driver.OracleDriver";
private static final String url="jdbc:oracle:thin:@localhost:1521:orcl";
private static final String uid="myoracle";
private static final String pwd="tiger";

private static Connection con;
private static PreparedStatement ps;
private static ResultSet rs;

public static Connection getConnection(){
//加载驱动
try {
Class.forName(driver);
//获取数据库连接
con=DriverManager.getConnection(url,uid,pwd);
return con;
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
return null;
}

//执行查询语句
public static ResultSet ExcuteQuery(String sql,Object[]param){
getConnection();
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 ExcuteUpdate(String sql,Object[]param){
getConnection();

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 rs=ps.executeUpdate();
return rs;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}


//关闭资源
public static void colse(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

NEWS-包名-baseTest-类名-baeseDao

标签:

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

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