My Study Note of JDBC (1)
时间:2021-07-01 10:21:17
帮助过:3人阅读
myjdbc;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
public class myjdbcutil {
public String dbms;
public String dbName;
public String userName;
public String password;
private String serverName;
private int portNumber;
private Properties prop;
public myjdbcutil(String filename)
throws FileNotFoundException, InvalidPropertiesFormatException, IOException{
this.setProperties(filename);
}
/* get properties from .properties file
* and set these values to Corresponding fields
* */
private void setProperties(String fileName)
throws FileNotFoundException,IOException,InvalidPropertiesFormatException {
this.prop =
new Properties();
InputStream fis =
this.getClass().getResourceAsStream(fileName);
prop.load(fis);
this.dbms =
this.prop.getProperty("dbms"
);
this.dbName =
this.prop.getProperty("database_name"
);
this.userName =
this.prop.getProperty("user_name"
);
this.password =
this.prop.getProperty("password"
);
this.serverName =
this.prop.getProperty("server_name"
);
this.portNumber = Integer.parseInt(
this.prop.getProperty("port_number"
));
System.out.println("Set the following properties:"
);
System.out.println("dbms: " +
dbms);
System.out.println("dbName: " +
dbName);
System.out.println("userName: " +
userName);
System.out.println("password: " +
password);
System.out.println("serverName: " +
serverName);
System.out.println("portNumber: " +
portNumber);
}
/* step 1 to execute query using JDBC
* get connection with connection properties
* */
public Connection getConnection()
throws ClassNotFoundException, SQLException{
Class.forName("oracle.jdbc.driver.OracleDriver"
);
System.out.println("Oracle JDBC Driver Registered!"
);
Connection conn =
null;
Properties connectionProps =
new Properties();
connectionProps.put("user",
this.userName);
connectionProps.put("password",
this.password);
if (
this.dbms.equals("oracle"
)) {
String dburl = "jdbc:"+
this.dbms+":"+dbName+":@"+
this.serverName+":"+
this.portNumber+"/xe"
;
System.out.println("dburl is:"+
dburl);
conn =
DriverManager.getConnection(dburl, connectionProps);
System.out.println("Connected to database successfully!"
);
}
return conn;
}
}
JDBC Connection
Test Code:
package myjdbc;
public class TestJdbc {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
myjdbcutil db = new myjdbcutil("mydb.properties");
db.getConnection();
}catch(Exception e){
e.printStackTrace();
}
}
}
Test
OutPut:
Set the following properties:
dbms: oracle
dbName: thin
userName: test
password: test
serverName: localhost
portNumber: 1522
Oracle JDBC Driver Registered!
dburl is:jdbc:oracle:thin:@localhost:1522/xe
Connected to database successfully!
View Code
pros:
dbms=oracle
database_name=thin
user_name=test
password=test
server_name=localhost
port_number=1522
jdbc:::@localhost:1522/xe
My Study Note of JDBC (1)
标签: