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"));
}
}
URL没填,http://www.cnblogs.com/cy163/archive/2008/08/22/1274413.html
填了呀,我是用配置文件填的url
@努力小码: 直接上面的例子测试下,看和你的程序差别在哪
配置文件路径对吗?