connectionfactory.java

来自「这是一本描述JDBC数据库的书籍」· Java 代码 · 共 67 行

JAVA
67
字号
/* * ConnectionFactory.java * * Created on June 9, 2005, 2:11 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package common;import java.io.*;import java.sql.*;import java.util.*;import javax.sql.*;/** * * @author kevin */public class ConnectionFactory {    private static ConnectionFactory factory = new ConnectionFactory();    private String driverName = null;    private String dbURL = null;    private String userName = null;    private String password = null;    /** Creates a new instance of ConnectionFactory */    private ConnectionFactory() {        init();    }        private void init(){        InputStream is = getDriverInfo();        Properties props = new Properties();        try{            props.load(is);            driverName = (String)props.getProperty("driver");            dbURL = (String)props.getProperty("dburl");            userName = (String)props.getProperty("username");            password = (String)props.getProperty("password");        }catch(Exception e){            e.printStackTrace();        }    }    private InputStream getDriverInfo(){        return getClass()            .getResourceAsStream("properties/jdbcinfo.properties");    }        public Connection getConnection()throws SQLException,ClassNotFoundException{        Class.forName(driverName);        Connection conn =             DriverManager.getConnection(dbURL, userName, password);        return conn;    }        public static ConnectionFactory getConnectionFactory(){        return factory;    }}

⌨️ 快捷键说明

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