?? connectionfactory.java
字號:
package com.allanlxf.jdbc.util;
import java.sql.*;
import java.io.*;
import java.util.Properties;
public class ConnectionFactory
{
private static Properties config = new Properties();
static
{
InputStream in = ConnectionFactory.class.getResourceAsStream("db-config.properties");
if(in == null)
{
throw new ExceptionInInitializerError("no file:db-config.properties found error!");
}
try
{
config.load(in);
in.close();
}catch(IOException e)
{
e.printStackTrace();
throw new ExceptionInInitializerError("failed to load file!");
}
}
public static Connection getConnection() throws SQLException
{
try
{
String driverClassName = config.getProperty("driverClassName");
String jdbcURL = config.getProperty("jdbcURL");
String userName = config.getProperty("userName");
String password = config.getProperty("password");
Class.forName(driverClassName);
return DriverManager.getConnection(jdbcURL, userName, password);
}catch(ClassNotFoundException e)
{
e.printStackTrace();
throw new SQLException(e.getMessage());
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -