Demo1:
package cn.wzl.demo1;
import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import org.junit.Test;
public class Demo1 { @Test public void fun1() throws SQLException{ Connection con=JdbcUtil.getConnection(); String sql="insert into music2 values(?,?)"; PreparedStatement pstmt=con.prepareStatement(sql); for(int i=0;i<100000;i++) { pstmt.setInt(1, i+1); pstmt.setString(2, i%2==0?"男":"女"); pstmt.addBatch(); } pstmt.executeBatch(); System.out.println("OK"); }
}
JdbcUtil:
package cn.wzl.demo1;
import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;
import org.junit.Test;
public class JdbcUtil { private static Properties props=null; static{ try { InputStream in=JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.con.properties"); props=new Properties(); props.load(in); } catch (IOException e) { throw new RuntimeException(e); } try { Class.forName(props.getProperty("driverClassName")); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } public static Connection getConnection() throws SQLException{ return DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password")); } }
添加回答
舉報
0/150
提交
取消