MySQL学习总结(5)--- JDBC 实现对数据库的增删改查

MySQL学习总结(5)— JDBC 实现对数据库的增删改查

首先,数据库的创建和连接,以及JDBC工具类的编写同上一篇文章,此篇文章只写核心代码。

1. 增

 @Test
    public void insert() {
    
        Connection connection = null;
        Statement statement = null;

        try {
            //1.获取数据库连接
            connection = JDBCUtils.getConnection();
            //2.创建statement对象
            statement = connection.createStatement();
            //3.编写Sql语句
            String sql = "INSERT INTO users(id,NAME,PASSWORD,email,birthday) VALUES(4,'liuhu','234567','liuhu@sina.com','1980-10-05');";
            //4.执行sql语句
            int i = statement.executeUpdate(sql); //返回受影响的行数

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //5.释放资源
            JDBCUtils.closeAll(null,statement,connection);
        }
    }

2. 删

@Test
    public void delete() {

        Connection connection = null;
        Statement statement = null;

        try {
            //1.获取数据库连接
            connection = JDBCUtils.getConnection();
            //2.创建statement对象
            statement = connection.createStatement();
            //3.编写Sql语句
            String sql = "delete from users where id = 5";
            //4.执行sql语句
            int i = statement.executeUpdate(sql); //返回受影响的行数

            if (i>0){
                System.out.println("删除成功");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //5.释放资源
            JDBCUtils.closeAll(null,statement,connection);
        }
    }

3. 改

@Test
    public void update() {

        Connection connection = null;
        Statement statement = null;

        try {
            //1.获取数据库连接
            connection = JDBCUtils.getConnection();
            //2.创建statement对象
            statement = connection.createStatement();
            //3.编写Sql语句
            String sql = "update users set name = 'qinjiang' where id = 4";
            //4.执行sql语句
            int i = statement.executeUpdate(sql); //返回受影响的行数

            if (i>0){
                System.out.println("修改成功");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //5.释放资源
            JDBCUtils.closeAll(null,statement,connection);
        }
    }

4. 查

 public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

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

        try {
            //1.获取数据库连接
            connection = JDBCUtils.getConnection();
            //2.创建statement对象
            statement = connection.createStatement();

            //3.编写Sql语句

            System.out.print("请输入你要查询的用户名:");
            String username = scanner.nextLine();


            String sql = "select * from users where name = " +  username;

            System.out.println(sql);

            //select * from users where name = qinjiang or 1=1;


            resultSet = statement.executeQuery(sql);

            while (resultSet.next()){
                System.out.println(resultSet.getString("name"));
                System.out.println(resultSet.getString("password"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //5.释放资源
            JDBCUtils.closeAll(resultSet,statement,connection);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用JDBC实现数据库增删改查的示例代码: 1. 连接数据库 ``` String jdbcUrl = "jdbc:mysql://localhost:3306/myDatabase"; String username = "myUsername"; String password = "myPassword"; try { Connection connection = DriverManager.getConnection(jdbcUrl, username, password); // 连接成功 } catch (SQLException e) { // 连接失败 } ``` 2. 插入数据 ``` try { String sql = "INSERT INTO myTable (column1, column2, column3) VALUES (?, ?, ?)"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, "value1"); statement.setInt(2, 2); statement.setDouble(3, 3.14); int rowsInserted = statement.executeUpdate(); if (rowsInserted > 0) { System.out.println("数据插入成功"); } } catch (SQLException e) { System.out.println("数据插入失败:" + e.getMessage()); } ``` 3. 更新数据 ``` try { String sql = "UPDATE myTable SET column1 = ? WHERE column2 = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, "new value"); statement.setInt(2, 2); int rowsUpdated = statement.executeUpdate(); if (rowsUpdated > 0) { System.out.println("数据更新成功"); } } catch (SQLException e) { System.out.println("数据更新失败:" + e.getMessage()); } ``` 4. 删除数据 ``` try { String sql = "DELETE FROM myTable WHERE column2 = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setInt(1, 2); int rowsDeleted = statement.executeUpdate(); if (rowsDeleted > 0) { System.out.println("数据删除成功"); } } catch (SQLException e) { System.out.println("数据删除失败:" + e.getMessage()); } ``` 5. 查询数据 ``` try { String sql = "SELECT * FROM myTable WHERE column2 = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setInt(1, 2); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { String column1Value = resultSet.getString("column1"); int column2Value = resultSet.getInt("column2"); double column3Value = resultSet.getDouble("column3"); System.out.println(column1Value + ", " + column2Value + ", " + column3Value); } } catch (SQLException e) { System.out.println("数据查询失败:" + e.getMessage()); } ``` 希望这可以帮助您使用JDBC实现数据库增删改查

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值