猿问

java写mysql 的jdbc连接报了exceptionlnlnitializerError错误

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"));

}
}

跃然一笑
浏览 1114回答 2
2回答

斯蒂芬大帝

配置文件路径对吗?
随时随地看视频慕课网APP

相关分类

Java
我要回答