JDBC工具类——常规操作封装(可以直接复制代码使用)

JDBC工具类——常规操作封装(可以直接复制代码使用)

  • 制作工具类
  • 封装操作方法,方便操作调用,避免重复操作
  • 了解配置文件

工具类

主方法

package JDBCUtils;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private static String url = null;
    private static String user = null;
    private static String password = null;
    private static String driver = null;

    //静态代码块,在加载类时就加载类的静态代码块儿,随着类的加载一同被加载
    static {
        try {
            //生成 Properties 格式的对象,用于获取 Properties 格式的配置文件
            Properties properties = new Properties();
            //类加载器。获取src路径下的文件的方式--->ClassLoader 类加载器
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            URL res = classLoader.getResource("jdbcproperties.properties");
            String path = res.getPath();

            properties.load(new FileReader(path));
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");
            Class.forName(driver);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void close(Statement statement, Connection connection) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

配置文件

url=jdbc:mysql:///db
user=root
password=root
driver=com.mysql.jdbc.Driver

使用实例


import JDBCUtils.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JDBCDemo4 {
    public static void main(String[] args) {

        String sql = null;
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = JDBCUtils.getConnection();
            //获取执行sql的对象
            statement = connection.createStatement();
            //为sql赋值
            sql = "SELECT * FROM STUDENT3";
            //执行sql,将结果保存在ResultSet类型的结果集里
            resultSet = statement.executeQuery(sql);
            //声明list引用,接受student3Findall类型的值
            List<student3Findall> list = null;
            //声明数据库中表对应的类在循环外部,使引用复用
            student3Findall student3Findalls = null;
            //为list引用创建对象
            list = new ArrayList<student3Findall>();
            //循环遍历结果集
            while (resultSet.next()) {
                //创建  student3Findall 类型的对象,并为其赋值
                student3Findalls = new student3Findall();

                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                int age = resultSet.getInt(3);
                String sex = resultSet.getString(4);
                String address = resultSet.getString(5);
                int math = resultSet.getInt(6);
                int english = resultSet.getInt(7);

                student3Findalls.setId(id);
                student3Findalls.setAge(age);
                student3Findalls.setSex(sex);
                student3Findalls.setEnglish(english);
                student3Findalls.setMath(math);
                student3Findalls.setName(name);
                student3Findalls.setAddress(address);

                //将对象装载进入列表
                list.add(student3Findalls);

            }

            System.out.println(list);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.close(resultSet, statement, connection);
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值