JDBC回忆

本文展示了如何使用Java的JDBC进行数据库操作,包括加载MySQL驱动、建立连接、预编译SQL语句以及执行增、删、改、查操作。示例代码详细演示了查询所有学生信息、添加新学生、删除指定ID学生和更新学生信息的过程。

  代码:

@Test
    public void testSelectAll() {
        try {
            //        1、加载驱动Class.forName("");
            Class.forName("com.mysql.jdbc.Driver");
            //        2、获得连接对象Connection
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/java211202?userUnicode=true&characterEncoding=UTF-8", "root", "1234");
            //        3、写sql语句
            String sql = "SELECT id,`name`,age,gender FROM student";
            //        4、预编译Statement
            PreparedStatement statement = connection.prepareStatement(sql);
            //        5、执行sql语句
//                (1) 更新类(更改了表里面数据):delete/update/insert    executeUpdate()
//        返回值:int,表示你影响的行数
//                (2)查询(没有改变表里面数据):  select                  executeQuery()
//        返回值:结果集ResultSet
            ResultSet resultSet = statement.executeQuery(sql);
            ArrayList<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

查询结果:

增:
@Test
public void testAdd() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java211202?useUnicode=true&characterEncoding=UTF-8","root","1234");
        String sql = "INSERT INTO student(`name`,age,gender) VALUES(?,?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,"小八");
        statement.setInt(2,25);
        statement.setString(3,"女");
        int count = statement.executeUpdate();
        System.out.println("count: " + count);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
}

删除:

@Test
public void testDelete() {
    try {
        Connection connection = JDBCUtil.getConnection();
        String sql = "DELETE FROM student WHERE id=? ";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, 27);
        System.out.println(statement);
        int count = statement.executeUpdate();
        System.out.println("count" + count);
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }

改:

Test
public void testUpdate() {
    try {
        Connection connection = JDBCUtil.getConnection();
        String sql = "UPDATE student set name=?,age=?,gender=? WHERE id=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, "小张");
        statement.setInt(2, 23);
        statement.setString(3, "男");
        statement.setInt(4, 4);
        System.out.println(statement);
        int count = statement.executeUpdate();
        System.out.println("count" + count);
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值