import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/*
* 本类记录了常用的对数据库操作的代码
*/
public class SqlHelper {
//定义常用的变量
private static Connection ct = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
//连接数据库需要的数据
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String username = "";
private static String password = "";
private static String driver = "oracle.jdbc.driver.OracleDriver";
//加载驱动,因为只做一次,所以我们这里使用静态代码块来做
static{
try{
Class.forName(driver); //加载驱动 :oracle.jdbc.driver.OracleDriver
}catch(Exception e){
}
}
//创建一个静态方式,用来得到Oracle的连接
public static Connection getConnection(){
try{
ct = DriverManager.getConnection(url, username, password);//获取连接,将url和用户名密码传递进去进行连接
}catch(Exception e){
}
return null;
}
//关闭资源的函数
public static void close(ResultSet rs, PreparedStatement ps, Connection ct){
if(rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(ps != null)
try {
ps.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
if(ct != null)
try {
ct.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// =======================================下面我们需要提供一些借用户操作数据库的方法 ,CRUD常用方法
//修改数据的方法,用户需要传递一个sql语句进来,和传递一个String类型的数组进来,该数组里面存放的是参数,可以不写
public static void executeUpdate(String sql, String[] parameters){
try{
//调用getConnection方法 ,获取到一个连接
ct = getConnection();
ps = ct.prepareStatement(sql);
//首先判断
if(parameters != null){ //如果传递进来的参数不为空,我们就对传递进行赋值到sql语句中
for(int i=0; i<parameters.length; i++){
ps.setObject(i+1, parameters[i]);
}
}
//执行语句
ps.executeUpdate();
}catch(Exception e){
//如果我们在修改数据的时候执行失败了,则抛出下面的异常
throw new RuntimeException(e.getMessage());
}finally{
//关闭资源
if(ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(ct != null)
try {
ct.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//如果用户需要执行多条SQL语句,我们可以依照下面的方法来写
public static void executeUpdate2(String sql[], String [][]parament){
try{
//获取连接
ct = getConnection();
//因为用户传入的可能是多条sql语句,
ct.setAutoCommit(false);//先设置ct不要自动提交
//判断有几条数据,因为用户传递进来的是二维数组,所以这里可能会复杂一些
for(int i=0; i<sql.length; i++){ //外层循环将一维数据取出
if(parament[i] != null){
ps = ct.prepareStatement(sql[i]);
for(int j=0; j<parament[i].length; j++){ //内循环在二维数组取出数据
ps.setObject((j+1), parament[j]);
}
ps.executeUpdate();
}
}
ct.commit(); //提交数据
}catch(Exception e){
e.printStackTrace();
// =========我在这里加一个回滚超做,如果发生了异常,就自动 回滚,注意,
try {
ct.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally{
if(ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(ct != null)
try {
ct.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 下面我们编写一个关于查询的语句,注意该查询方法没有写关闭资源,所以调用都需要手动关闭
public static ResultSet executeQuery(String sql,String[] paramters){
try{
//得到连接
ct = getConnection();
ps = ct.prepareStatement(sql); //拿到需要执行的语句
//之里判断传递进来的参数是否为空
if(paramters != null && paramters.equals("")){
//循环取出参数的信息,并且将其值赋值给SQL语句
for(int i=0; i<paramters.length; i++){
ps.setObject(i+1, paramters[i]);
}
}
rs = ps.executeQuery(); //执行语句,
}catch(Exception e){
throw new RuntimeException(e.getMessage());
}finally{
}
return rs;
}
}