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&¶m.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();
}
}
}
}