⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionfactory.java

📁 基于struts框架的银行帐户实例
💻 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 + -