利用idea对mysql进行增删查改

2022/2/4 19:25:41

本文主要是介绍利用idea对mysql进行增删查改,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

导入jar包模式

import java.sql.*;
public class jdbcdeno1 {
    public static void main(String[] args){
      //  ("链接url+/数据库名","用户名","密码");
        try(Connection connection= DriverManager.getConnection
            ("jdbc:mysql://localhost:3306/ooo","root","123456");
            Statement statement =connection.createStatement())
            {
             //执行sql
          
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}

下面操作直接在//执行sql 下面那行复制粘贴就行
增:
向teacher表插入数据15号、名字为15,性别为女。

   statement.executeUpdate("insert  into teacher value (15,'十五','2')");

这里用“2”表示女是因为我直接写女的话报了一个“Data truncated for column ‘sex’ at row 1”的错。
插入多行(批处理):

 statement.addBatch("insert  into teacher value (15,'十五','2')");
 statement.addBatch("insert  into teacher value (16,'16','2')");
 statement.addBatch("insert  into teacher value (17,'17','1')");
                statement.executeBatch();

删:
把刚插入的第15行数据删除

  statement.executeUpdate("delete  from  teacher where tid='15'");

修:
把第14号的名字修改成14

statement.executeUpdate("update  teacher set name='14' where tid='14'");


这篇关于利用idea对mysql进行增删查改的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程