Java JDBC 插入数据

  • 插入数据

    考虑我们有下面一个MySQL表:
    CREATE TABLE `tutorials_tbl` (
      `tutorial_id` int(11) NOT NULL AUTO_INCREMENT,
      `tutorial_title` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
      `tutorial_author` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL,
      `submission_date` date DEFAULT NULL,
      PRIMARY KEY (`tutorial_id`)
    ) ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    提示:不熟悉MySQL的同学可以到我们的MySQL教程进行学习。
    下面展示向tutorials_tbl表插入三条数据的例子:
    import java.sql.*;
     
    
    public class MyClass {
            //驱动字符串
            final static String driverStr="com.mysql.cj.jdbc.Driver";
            final static String dbHost = "192.168.61.125";  // 主机  可为ip地址
            final static String dbUserName = "root";  // 用户
            final static String dbPassword = "123456"; // 密码
            final static String dbPort = "3306"; // 端口
            final static String dbName = "mydb"; // 数据库名
            final static String connStr="jdbc:mysql://"+ dbHost +":"+ dbPort +"/"+ dbName +"?useSSL=false&serverTimezone=UTC"; 
         
        
        public static void main(String[] args){
            MyClass obj = new MyClass();
            Connection conn = obj.connect();
            try {
                    String tu1 = "Learn PHP";
                    String au1 = "John Poul";
                    String dt1 = "2020-02-27";
                    
                    String tu2 = "Learn MySQL";
                    String au2 = "Abdul S";
                    String dt2 = "2020-02-27";
                    
                    String tu3 = "JAVA Tutorial";
                    String au3 = "Sanjay";
                    String dt3 = "2007-05-06";
                    String sqlString = "INSERT INTO tutorials_tbl (tutorial_title , tutorial_author , submission_date) "+ 
                            "VALUES (?,?,?),(?,?,?),(?,?,?)";
                    PreparedStatement ps = conn.prepareStatement(sqlString);
                    ps.setString(1, tu1);
                    ps.setString(2, au1);
                    ps.setString(3, dt1);
                    ps.setString(4, tu2);
                    ps.setString(5, au2);
                    ps.setString(6, dt2);
                    
                    ps.setString(7, tu3);
                    ps.setString(8, au3);
                    ps.setString(9, dt3);
                    
                    
                            ps.executeUpdate();
                            System.out.println("执行成功");
                    } catch (SQLException e) {
                            System.out.println("执行失败");
                            e.printStackTrace();
                    }
        }
        
        // 链接
        protected Connection connect () {
            Connection conn = null;
            try{
                  Class.forName(MyClass.driverStr);  // 加载驱动
                  conn = DriverManager.getConnection(MyClass.connStr,MyClass.dbUserName,MyClass.dbPassword); // 连接
                  System.out.println("连接成功");
                }catch(Exception ex){
                  System.out.println("连接失败");
                  System.out.println(ex.toString());
            }
            
            return conn;
            }    
        
    }
    通过编译,运行效果如下:
    连接成功
    执行成功
    
    提示:不懂编译的同学查看上一章:数据库连接