package com.example.jdbc.origin; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; public class Test { public static void main(String[] args) throws SQLException { try { Class.forName("com.mysql.cj.jdbc.Driver");//加载并注册驱动程序 }catch(ClassNotFoundException e)//加载错误,捕获异常 { System.out.println("加载驱动失败"); } Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/user?Unicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=UTC", "root","123456"); //创建Connection连接对象 String name="batch insert test name100"; String sql0="update t_person set job=(select count(*) from t_user where user_name='"+name+"') where name='"+name+"'"; Statement sta = con.createStatement(); System.out.println("sql0:"+sql0); sta.execute(sql0); String sql="update t_person set job=(select count(*) from t_user where user_name=?)as job where name=?"; sql="update t_person set job=? where name= ?"; System.out.println("sql:"+sql); PreparedStatement stmt = con.prepareStatement(sql); //创建语句对象 stmt.setString(1, name); stmt.setString(2, name); //stmt.execute(); stmt.executeUpdate(); con.close(); } }