📄 connectionfactory.java
字号:
package com.tarena.bank.util;
import java.util.Properties;
import java.sql.*;
import java.io.*;
public class ConnectionFactory
{
private static Properties config = new Properties();
static
{
try
{
InputStream in = ConnectionFactory.class.getClassLoader().getResourceAsStream("database-config.properties");
config.load(in);
in.close();
}catch(Exception e)
{
e.printStackTrace();
throw new ExceptionInInitializerError();
}
}
public static Connection getConnection()
{
try
{
Class.forName(config.getProperty("driver"));
return DriverManager.getConnection(config.getProperty("url")
, config.getProperty("user")
, config.getProperty("password"));
}catch(ClassNotFoundException e)
{
e.printStackTrace();
throw new RuntimeException("unable to find class " + config.getProperty("driver"));
}catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("unable to connect database");
}
}
public static void main(String[] args) throws Exception
{
Connection con = ConnectionFactory.getConnection();
System.out.println(con);
con.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -