Oracle 数据库的 JDBC工厂封装,信息全写在配置文件中
public class ConnectionFactory {
private static String driver;
private static String url; //XE
private static String username;
private static String password;
private static Properties properties;
private static Connection connection;
static {
properties = new Properties();
try {
FileInputStream fs = new FileInputStream("src/jdbc.properties");
properties.load(fs);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("user");
password = properties.getProperty("password");
//System.out.println(driver+url+username+password);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection (){
try {
Class.forName(driver);
connection = DriverManager.getConnection
(url,properties); //使用两参的时候命名必须规范 user password url driver
//(url,username,password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
}
jdbc.properties 文件内容
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:XE
user=oracle
password=oracle
Mysql JDBC 工厂封装
public class ConnectionMySql {
private static String driver;
private static String url; //XE
private static String user;
private static String password;
private static Connection connection ;
private static Properties properties;
private static PreparedStatement pStatement;
static {
properties = new Properties();
try {
FileInputStream fis = new FileInputStream("src/mysql.properties");
properties.load(fis);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
System.out.println("url,properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getMySqlConnection(){
try {
Class.forName(driver);
connection=DriverManager.getConnection(url,properties);
//(url,user,password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
/* public static void main(String[] args) {
String sql = "select * from message";
Connection mySqlConnection = getMySqlConnection();
try {
pStatement =mySqlConnection.prepareStatement(sql);
ResultSet rs = pStatement.executeQuery();
while(rs.next()){
System.out.println("ID="+rs.getInt("ID")+
" COMMAND="+rs.getString("COMMAND")+
" DESCRIPTION"+rs.getString("DESCRIPTION")
+" CONTENT="+rs.getString("CONTENT"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* String url = "jdbc:mysql://127.0.0.1:3306/message?user=root&useUnicode=true&characterEncoding=UTF8";
try {
Class.forName("com.mysql.jdbc.Driver");
connection= DriverManager.getConnection(url);
String sql = "select * from message";
pStatement= connection.prepareStatement(sql);
ResultSet rs = pStatement.executeQuery();
while (rs.next()) {
System.out.println("ID="+rs.getInt("ID")+
" COMMAND="+rs.getString("COMMAND")+
" DESCRIPTION"+rs.getString("DESCRIPTION")
+" CONTENT="+rs.getString("CONTENT"));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}*/
}
mysql.properties 配置信息
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/message
user=root
password=