简例 一次执行多条mysql insert语句
package com.demo.kafka;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class ConnectToMysql {
public static void main(String[] args) throws Exception {
Connection c = getconnection();
String insert31 = "insert into student values (?,?,?)";
PreparedStatement ps = c.prepareStatement(insert31);
ps.setInt(1, 1);
ps.setString(2, "ww1");
ps.setString(3, "123456");
ps.addBatch();
ps.setInt(1, 2);
ps.setString(2, "ww2");
ps.setString(3, "123456");
ps.addBatch();
ps.setInt(1, 3);
ps.setString(2, "ww3");
ps.setString(3, "123456");
ps.addBatch();
ps.executeBatch();
ps.clearBatch();
ps.close();
c.close();
}
private static Connection getconnection() throws Exception {
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://192.168.1.155:8066/ms?user=root&password=123456";
conn = DriverManager.getConnection(url);
return conn;
}
}