JDBC 提取封装简单工具类


prtyaa
prtyaa 2023-12-27 16:35:02 53334 赞同 0 反对 0
分类: 资源
在两个操作中,大量的代码重复,那这个时候我们就可以进行提取,将重复多的代码提取出来,简化代码,也解决我们编写相同代码的时间
package com.lin;

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

/**
 * jdbc 工具类
 */
public class jdbcUtil {
    private static String driver = "com.mysql.jdbc.Driver";
    private static String jdbcUrl = "com.mysql.jdbc.Driver";
    private static String userName = "root";
    private static String password = "chaoge";
//静态代码块,程序加载时执行,且执行一次
    static {
        try {
            Class.forName("driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //获取Connection 对象
    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(jdbcUrl, userName, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //关闭Statement
    public static void closeStatement(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //关闭Connection
    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //关闭所有资源
    public static void closeResource(Statement statement, Connection connection) {
        closeStatement(statement);
        closeConnection(connection);
    }

}

有了上面的工具类,那我们就可以精简我们的代码了

package com.lin;

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

/**
 * 使用工具类
 */
public class jdbcTest1 {


        //向Departments 表中添加一个数据
        public void insertDepartments(String department_name,int location_id) {
            Connection connection = null;
            Statement statement = null;
            try {
                //1.注册驱动 获取connection 对象
                connection = jdbcUtil.getConnection();
                //编辑sql语句
                String sql = "insert into departments values(default,'" + department_name + "','" + location_id + "')";
                //创建Statement对象用于发送数据
                statement = connection.createStatement();
                int flag = statement.executeUpdate(sql);
                System.out.println(flag);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                jdbcUtil.closeStatement(statement);
                jdbcUtil.closeConnection(connection);
            }



        }

        //更新departments 表中的department_di 为6 的数据 部门名称修改为java研发部 location_id改为6
        public void updateDepartments( String department_id,String department_name, int location_id){
            Connection connection = null;
            Statement statement = null;

            try {
              connection= jdbcUtil.getConnection();
                //创建Statement对象用于发送数据
                statement =connection.createStatement();
                //创建SQL语句
                String sql =  "update departments d set d.department_name = '"+department_name+"',d.location_id = "+location_id+" where d.department_id ="+department_id;
                int falg = statement.executeUpdate(sql);
                if (falg ==1){
                    System.out.println("修改成功");
                }else {
                    System.out.println("修改失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {

                jdbcUtil.closeResource(statement,connection);
            }
        }


        public static void main(String[] args) {
            com.lin.jdbcTest jdbcTest = new com.lin.jdbcTest();
          //  jdbcTest.insertDepartments("事业部",11);
            jdbcTest.updateDepartments("7","推广销售部",7);

        }
 }

如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!

评价 0 条
prtyaaL2
粉丝 1 资源 1949 + 关注 私信
最近热门资源
银河麒麟桌面操作系统备份用户数据  125
统信桌面专业版【全盘安装UOS系统】介绍  120
银河麒麟桌面操作系统安装佳能打印机驱动方法  112
银河麒麟桌面操作系统 V10-SP1用户密码修改  105
最近下载排行榜
银河麒麟桌面操作系统备份用户数据 0
统信桌面专业版【全盘安装UOS系统】介绍 0
银河麒麟桌面操作系统安装佳能打印机驱动方法 0
银河麒麟桌面操作系统 V10-SP1用户密码修改 0
作者收入月榜
1

prtyaa 收益393.62元

2

zlj141319 收益218元

3

1843880570 收益214.2元

4

IT-feng 收益209.03元

5

风晓 收益208.24元

6

777 收益172.71元

7

Fhawking 收益106.6元

8

信创来了 收益105.84元

9

克里斯蒂亚诺诺 收益91.08元

10

技术-小陈 收益79.5元

请使用微信扫码

加入交流群

请使用微信扫一扫!