JDBC 工具类优化


prtyaa
prtyaa 2023-12-27 16:34:16 50362 赞同 0 反对 0
分类: 资源
jdbc 是支持properties文件的 所以在src目录下创建jdbc.properties文件

读取properties文件

工具类代码

package com.lin;

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

/**
 * jdbc 工具类
 */
public class jdbcUtil {
    private static String driver ;
    private static String jdbcUrl ;
    private static String userName ;
    private static String password ;

    static {
        //读取properties文件
        ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
        driver = jdbc.getString("driver");
        jdbcUrl =jdbc.getString("jdbcUrl");
        userName=jdbc.getString("userName");
        password=jdbc.getString("userName");

        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;

/**
 * 使用工具类进行jdbc操作
 */
public class jdbcTest1 {


        //向Departments 表中添加一个数据
        public void insertDepartments(String department_name,int location_id) {
            Connection connection = null;
            Statement statement = null;
            try {
                //1.注册驱动
                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("采购部",12);
          //  jdbcTest.updateDepartments("7","推广销售部",7);

        }
 }

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

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

prtyaa 收益393.62元

2

zlj141319 收益218元

3

1843880570 收益214.2元

4

IT-feng 收益210.13元

5

风晓 收益208.24元

6

777 收益172.71元

7

Fhawking 收益106.6元

8

信创来了 收益105.84元

9

克里斯蒂亚诺诺 收益91.08元

10

技术-小陈 收益79.5元

请使用微信扫码

加入交流群

请使用微信扫一扫!