connectionfactory.java

来自「这是花卉管理的一个小型系统」· Java 代码 · 共 87 行

JAVA
87
字号
package lib.nanjing.jdbc.util;

import java.sql.*;
import java.io.*;
import java.util.*;


public class ConnectionFactory {
	public static Connection getConnection() {
		// step 0 load the property file

		Properties prop = null;
		FileInputStream in = null;
		try {
			prop = new Properties();
			in = new FileInputStream("configuration/connect_info.conf");
			prop.load(in);
			in.close();
		} catch (FileNotFoundException fnf) {
			System.out.println("connect_info.conf file not found!!");
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}

		// step 1 load(register) the driver

		String jdbcDriver = prop.getProperty("jdbc.drivers");
		try {
			Class.forName(jdbcDriver);
		} catch (ClassNotFoundException e) {
			System.out.println("load driver failure!!");
		}

		// step 2 establish connection to the database

		String url = prop.getProperty("jdbc.url");
		String user = prop.getProperty("jdbc.user");
		String password = prop.getProperty("jdbc.password");
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, user, password);
			return con;
		} catch (SQLException e) {
			System.out.println("connect failure!!");
			return null;
		}
	}

	
	/*public static void main(String[] args){
		Connection con=ConnectionFactory.getConnection();
		Statement stat=null;
		
		try{
			stat=con.createStatement();
			System.out.println("create statement success!!");	
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("create statement failure!!");
		}
		
		//step 4 execute the SQL statement
		
		//step 5 process the result
		ResultSet results=null;
		try{
			results=stat.executeQuery("select * from salary");
			System.out.println("exceuteQuery success!!\nbegin to process the result >>>>>>>>>>>>>>>>");
			while(results.next()){
				System.out.print(results.getString("name")+'\t');
				System.out.print(results.getString("lev")+'\t');
				System.out.print(results.getString("salary")+'\t');
				System.out.println(results.getString("welfware"));
			}
		}catch(SQLException e){
			System.out.println("executeQuery failure!!");
		}
		//step 6 close the connection
		try{
			JdbcUtil.close(results,stat,con);
			System.out.println("connection closed success!!");
		}catch(Exception e){
			System.out.println("connection closed failure!!");
		}
	}*/
}

⌨️ 快捷键说明

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