conn.java

来自「用J2EE开发的网站,可以应用到图书馆,图书超市」· Java 代码 · 共 47 行

JAVA
47
字号
package ksnb;

import java.util.*;
import java.sql.*;
import java.net.*;

public class conn {
   Connection con=null;
   public conn() throws connException{
       String driver = null;
       String user = null;
       String pass = null;
       String dbURL = null;
       //生成Properties对象,用来获取配置文件(config)内容
       java.util.Properties properties = null;
       try {
           properties = PropertiesManager.getProperties("config");
           driver = properties.getProperty("jdbc.driver");
           user = properties.getProperty("jdbc.user");
           pass = properties.getProperty("jdbc.password");
           dbURL = properties.getProperty("jdbc.dbURL");
           //连接数据库
           Class.forName(driver);
           con= DriverManager.getConnection(dbURL, user, pass);
           System.out.println("连接数据库成功!");
       }
       catch (java.io.FileNotFoundException e) {
            throw new connException("JDBC Properties File Read Error!");
        } catch (MissingResourceException e) {
            throw new connException("JDBC Properties File Not Found!");
        } catch (ClassNotFoundException e) {
            throw new connException("No Driver Available!");
        } catch (SQLException se) {
            throw new connException(se.getMessage());
        }
   }

    public Connection getConncetion() throws connException {
        return con;
    }
    public void conClose(){
        try{
        con.close();
        }catch(Exception e){}
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?