import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/**
* JDBC工具类
*
* @author Administrator
*
*/
public class JDBCUtils {
private static Properties props = new Properties();
// 这里的语句都只执行一次,我们让它在JdbcUtils这个类被加载时执行!
static {
try {
// 获取类路径下的资源
InputStream in = JDBCUtils.class
.getResourceAsStream("dbconfig.properties");
// 加载配置文件
props.load(in);
/*
* 注册驱动类
*/
Class.forName(props.getProperty("driverClassName"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 获取连接对象
*
* @return
*/
public static Connection getConnection() {
try {
/*
* 3. 获取连接对象
*/
Connection con = DriverManager.getConnection(
props.getProperty("url"), props.getProperty("username"),
props.getProperty("password"));
return con;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
你的这个答案我在网上看过了,没用