JAVA JDBC 连接数据库

package com.lovemin;
import java.sql.*;

public class DataOperation {
    private static final String DRIVER_NAME="com.mysql.cj.jdbc.Driver";
    private static final String URL="jdbc:mysql://192.168.123.132:3306/a?"+
            "useSSL=false&serverTimezone=GMT%2B8";
    private static final String USER_NAME="root";
    private static final String PASSWORD="zhang";
    private static String preStateSql;
    private static ResultSet result;
    private static PreparedStatement preState;
    private static Statement state;
    private static Connection con;
    public static Connection getConnection(){
        if(con==null){
            try {
                Class.forName(DRIVER_NAME);
            } catch (ClassNotFoundException e) {
                System.out.println("未发现驱动");
                return null;
            }
            try{
                con=DriverManager.getConnection(URL,USER_NAME,PASSWORD);
                System.out.println("数据库连接成功");
            }catch (SQLException e){
                System.out.println("配置错误,无法连接数据库");
                e.getMessage();
                con=null;
            }
        }
        return con;
    }

    public static void close(){
        if(con!=null){
            try {
                con.close();
                preState.close();
                state.close();
                result.close();
                state=null;
                result=null;
                preState=null;
                con=null;
            } catch (SQLException e) {
                System.out.println("数据库连接关闭失败");
                e.printStackTrace();
            }
        }
    }
    public void insertOperation(String sql,String[] params){
        if(preStateSql==null||preStateSql.equals(sql)){
            preStateSql=sql;
            try {
                preState=getConnection().prepareStatement(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        for(int i=0;i<params.length;i++){
            try {
                preState.setObject(i,params[i]);
            }catch (SQLException e) {
                e.printStackTrace();
                try {
                    con.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
                return ;
            }
        }
        try {
            preState.executeUpdate();
            con.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static ResultSet getResult(String sql) {
        if(state==null) {
            try {

                state = getConnection().createStatement();
                return state.executeQuery(sql);
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("getResult() 执行sql 失败!");
            }
        }
        return null;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值