?? connectionfactory.java
字號(hào):
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!!");
}
}*/
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -