设为首页 加入收藏

TOP

数据库之JDBC操作及实例讲解(二)
2018-03-24 09:50:28 】 浏览:360
Tags:数据库 JDBC 操作 实例 讲解
; Statement statement = connection.createStatement(); String sql = "delete from users where id=5"; int row = statement.executeUpdate(sql); System.out.println(row); if (row > 0) { System.out.println("删除成功"); } statement.close(); connection.close(); }
@Test 
public void testSelect() throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/myjdbc?user=root&password=123456";
    Connection connection = DriverManager.getConnection(url);
    Statement statement = connection.createStatement();
    // 查询
    String sql = "select id, name, email from users";
    ResultSet resultSet = statement.executeQuery(sql);
    // 处理结果集
    while (resultSet.next()) {
        // 可以直接填字段名称
        System.out.print(resultSet.getObject("id") + " ");
        System.out.print(resultSet.getObject("name") + " ");
        System.out.println(resultSet.getObject("email") + " ");
    }
    resultSet.close();
    statement.close();
    connection.close();
}

连接数据库时的异常处理

public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/myjdbc";
        connection = DriverManager.getConnection(url, "root", "123456");
        statement = connection.createStatement();
        String sql = "select * from users";
        resultSet = statement.executeQuery(sql);
        // 处理结果集(把数据的记录封装到对象中)
        ArrayList
  
    arrayList = new ArrayList<>();
        while(resultSet.next()) {
            // 创建 User 对象
            User user = new User();
            user.setId(resultSet.getInt("id"));
            user.setName(resultSet.getString("name"));
            user.setPassword(resultSet.getString("password"));
            user.setEmail(resultSet.getString("email"));
            user.setBirthday(resultSet.getDate("birthday"));
            // 放入集合中
            arrayList.add(user);
        }
        // 遍历查看
        for (User user : arrayList) {
            System.out.println(user);
        }
    } catch (ClassNotFoundException e) {
        // 停止程序
        throw new RuntimeException("驱动加载失败");
    } catch (SQLException e) {
        throw new RuntimeException("获取连接失败");
    } finally {
        // 关闭资源前,要做非空判断,防止空指针异常
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException("资源关闭失败");
            }
            // 加快
   系统回收的速度
            resultSet = null;
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException("资源关闭失败");
            }
            // 加快系统回收的速度
            statement = null;
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException("资源关闭失败");
            }
            // 加快系统回收的速度
            connection = null;
        }
    }
}
  
创建 dbinfo.properties 文件,作为配置文件

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/myjdbc
user=root
password=123456
将 JDBC 封装为一个工具类方法使用




public class JDBCUtil {
    private static String driverClass;
    private static String url;
    private static String user;
    private static String password;
    // 使用静态代码块加载驱动,读取配置文件
    static {
        // 第一种读取配置文件的方法:利用集合读取文件
        Properties properti
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java之数据库连接JDBC教程 下一篇MySQL数据库管理、表管理及增删改..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目