import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* 将表1中的id列表,以插入新数据的形式添加到表2
* */
public class StoreProcess {
public static void main(String[] args) {
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 获得连接
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/user?autoReconnect=true", "root", "1234");
// 创建存储过程的对象
PreparedStatement ps = conn.prepareStatement("SELECT * FROM biao1");
CallableStatement cs = conn.prepareCall("INSERT INTO biao2(id) VALUES(?)");
// 调用存储过程并获取结果集
ResultSet rs = ps.executeQuery();
// 处理结果集
while (rs.next()) {
cs.setLong(1, rs.getLong("id"));// 设置参数
cs.execute();// 调用存储过程
}
// 释放资源
rs.close();
cs.close();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}