jdbc操作数据库的工具类(java)(MySQL数据库)

一、准备工作(定义变量)

1、定义驱动

//创建驱动名称
    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";

2、定义连接数据库的URL链接(解决了jdbc的connector版本警告的问题,中文乱码的问题)

//创建数据库连接的URL
    private static final String DB_URL = "jdbc:mysql://localhost/jdbctest?" +
            "useUnicode&characterEncodiing=utf-8&useSSL=true&rewriteBatchedStatements=true";

3、定义连接数据库的用户名和密码(根据自己的用户名和密码进行设置)

//定义连接数据库的用户名和密码
    private static final String userName = "root";
    private static final String password = "123456";

4、定义连接数据库对象的上下文环境的Connection对象;用于存储查询后的结果集的集合ResultSet对象;创建一个静态的Statement通道对象;创建一个预处理的PrepareStatement通道对象(preparestatement相对于statement的优点是preparestatement只编译一次,可以防sql注入的问题,提供了大量的方法比较方便)。

    //定义一个来接数据库对象的上下文环境变量
    private static Connection connection;
    //定义一个用于存储查询后的结果集的一个集合
    private static ResultSet resultSet;
    //创建一个statement通道
    private static Statement statement;
    //创建一个prepareStatement通道
    private static PreparedStatement preparedStatement;

二、加载数据库驱动

1、方法一(最常用的方法):

它会自动将其注册。这种方法更优越一些,因为它允许你对驱动程序的注册信息进行配置,便于移植。数据库的驱动只需要加载一次,所以此处应该将其包含在一个静态代码块中,这样就会在加载工具类的时候直接加载数据库的驱动,也避免了反复的加载,从而减少了资源的浪费。

static{
        try {
            System.out.println(new Date() +"加载数据库驱动中。。。");
            Class.forName(JDBC_DRIVER).newInstance();
        } catch (ClassNotFoundException e) {
           System.out.println(new Date() +"方法1加载JDBC驱动失败(没有找到驱动程序)。。。");
           System.exit(1);
        } catch (IllegalAccessException e) {
            System.out.println(new Date() +"方法1加载JDBC驱动失败(找不到路径)。。。");
            System.exit(2);
        } catch (InstantiationException e) {
           System.out.println(new Date() +"方法1加载JDBC驱动失败(不知道驱动类型)");
            System.exit(3);
        }
    }

2、方法二:

你注册一个驱动程序的第二种方法是使用静态 staticDriverManager.registerDriver() 方法。 * 如果你使用的是不兼容 JVM 的非 JDK,比如微软提供的,你必须使用 registerDriver() 方法。 * 下面是使用 registerDriver() 来注册 Oracle 驱动程序的示例:

    static{
        try {
            Driver driver = new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(driver);
        } catch (SQLException e) {
            System.out.println(new Date() +"方法2加载数据库驱动失败");
        }
    }

三、获取数据库的连接

获取数据库的连接主要采用的是DriverManager类中的getConnection()方法,方法中需要填写数据库连接的URL,数据库用户名、用户的登录数据库的密码。

   private static void getConnection(){
        try {
            System.out.println(new Date()+"创建数据库连接中。。。");
            connection = DriverManager.getConnection(DB_URL,userName,password);
        } catch (SQLException e) {
            System.out.println(new Date() +"连接数据库失败");
            e.printStackTrace();
        }
    }

四、绑定参数

绑定参数方法就是讲需要传入的参数与prepareStatement预处理通道对象进行绑定。

private static void bundle(String[] parm){
        //判断数组Parm是否为空
        if (parm != null){
            //通过for循环将参数绑定起来
            for (int i = 0; i < parm.length; i++) {
                try {
                    System.out.println(new Date()+"进行参数的绑定。。。");
                    preparedStatement.setString(i+1,parm[i]);
                } catch (SQLException e) {
                    System.out.println(new Date() +"绑定参数失败。。。");
                    e.printStackTrace();
                }
            }
        }
    }

五、创建一个statement静态处理通道对象

private static void createStatement(){
        getConnection();
        try {
            System.out.println(new Date()+"创建statement通道对象。。。");
            statement = connection.createStatement();
        } catch (SQLException e) {
            System.out.println(new Date() +"创建statement通道对象失败。。。");
            e.printStackTrace();
        }
    }

六、创建一个prepareStatement预处理通道对象

private static void createPsStatement(String sql){
        getConnection();
        try {
            System.out.println(new Date()+"创建PrepareStatement通道对象。。。");
            preparedStatement = connection.prepareStatement(sql);
        } catch (SQLException e) {
            System.out.println(new Date() +"创建PrepareStatement通道对象失败。。。");
            e.printStackTrace();
        }
    }

七、修改数据库总的数据函数

数据库数据库修改分两种①添加数据insert into②修改数据update

public static Boolean UpdateData(String sql,String[] parm){
        //创建通道
        createPsStatement(sql);
        //绑定参数
        bundle(parm);
        int row = 0;
        try {
            System.out.println(new Date()+"修改数据中。。。");
            row = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            System.out.println(new Date() +"修改数据失败。。。");
            e.printStackTrace();
        }
        boolean res = false;
        if (row > 0){
            res = true;
        }
        return res;
    }

八、使用statement查询数据

public static ResultSet queryByStatement(String sql){
        createStatement();
        try {
            System.out.println(new Date()+"采用Statement方法执行sql查询语句。。。");
            resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            System.out.println(new Date() +"采用Statement方法执行sql查询语句失败");
            e.printStackTrace();
        }
        return resultSet;
    }

九、采用prepareStatement查询数据

 public static ResultSet queryByPsStatement(String sql,String[] pram){
        createPsStatement(sql);
        bundle(pram);
        try {
            System.out.println(new Date()+"采用PrepareStatement方法执行sql查询语句。。。");
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            System.out.println(new Date() +"采用PrepareStatement方法执行sql查询语句失败");
            e.printStackTrace();
        }
        return resultSet;
    }

十、释放资源

public static void closeAll(){
        if (resultSet != null){
            try {
                System.out.println(new Date()+"关闭resultSet。。。");
                resultSet.close();
            } catch (SQLException e) {
                System.out.println(new Date()+"关闭resultSet异常。。。");
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                System.out.println(new Date() + "关闭statement。。。");
                statement.close();
            } catch (SQLException e) {
                System.out.println(new Date() + "关闭statement异常。。。");
                e.printStackTrace();
            }
        }
        if (preparedStatement != null){
            try {
                System.out.println(new Date()+"关闭preparestatement。。。");
                preparedStatement.close();
            } catch (SQLException e) {
                System.out.println(new Date()+"关闭preparestatement异常。。。");
                e.printStackTrace();
            }
        }
        if (connection != null){
            try {
                System.out.println(new Date()+"关闭connection。。。");
                connection.close();
            } catch (SQLException e) {
                System.out.println(new Date()+"关闭connection异常。。。");
                e.printStackTrace();
            }
        }


    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值