JDBC工具类示例

package com.utils;

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

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class JDBC {
    private static Connection connection;//连接
    private PreparedStatement statement;//
    private ResultSet rs;//结果集

    public  static Connection  getConn() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost/ls?useSSL = false&serverTimezone = UTC";
            //String url = "jdbc:mysql://localhost:3306/useSSL";        //
            String user = "root";
            String password = "123456";
            connection = DriverManager.getConnection(url, user, password);

            return connection;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    //用户数据库连接的方法
    public boolean getConnection() {
        boolean flag = false;
        try {
            //加载链接
            Class.forName("com.mysql.cj.jdbc.Driver");
            //我用的数据库链接固定语句
            String url = "jdbc:mysql://localhost/ls?useSSL = false&serverTimezone = UTC";        //连接数据库
            //ls指连接的数据库
            //String url = "jdbc:mysql://localhost:3306/hngy";
            String user = "root";
            String password = "123456";
            connection = DriverManager.getConnection(url, user, password);

            if (connection != null) {
                flag = true;
                System.out.println("连接数据库成功");
            } else {
                System.out.println("数据库连接失败");
            }

        } catch (Exception e) {

            e.printStackTrace();
        }

        return flag;

    }

    // 所有查询的方法(模板),不改变数据库的数据
    public ResultSet query(String sql, Object params[]) {
        try {
            if (getConnection()) {
                // 获取PreparedStatement对象
                statement = connection.prepareStatement(sql);
                // 给占位符赋值
                for (int i = 0; i < params.length; i++) {
                    statement.setObject(i + 1, params[i]);
                }
                // 执行查询操作
                rs = statement.executeQuery();
            }
        } catch (SQLException e) {

            e.printStackTrace();
        }

        return rs;

    }
    
    //添加,修改,删除(模板),要改变数据库的数据
    public void update(String sql,Object params[]){
        if(getConnection()){
            try {
                statement=connection.prepareStatement(sql);
                for(int i=0;i<params.length;i++){
                    statement.setObject(i+1, params[i]);
                }
                //执行sql语句
                statement.executeUpdate();
            } catch (SQLException e) {            
                e.printStackTrace();
            }
        }
    }
    

    // 关闭数据库连接的方法
    public void close() {
        try {
            if (rs != null) {
                rs.close();
            }
            if(statement!=null){
                statement.close();                
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {        
            e.printStackTrace();
        }
    }
    //关闭数据库连接的方法
    public static void closeall(Connection cn,    PreparedStatement ps,    Statement st ,    ResultSet rs){
        if(cn!=null){
             try {
                cn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(st!=null){
             try {
                st.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(rs!=null){
             try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    }
    }


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值