Database.properties
Copy Code code as follows:
Jdbc.driver_class=oracle.jdbc.driver.oracledriver
Jdbc.connection.url=jdbc:oracle:thin: @localhost: 1521:ORCL
Jdbc.connection.username=scott
Jdbc.connection.password=tiger
Configmanager.java
Copy Code code as follows:
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Properties;
Tool class for reading configuration file (property file)
public class ConfigManager {
private static ConfigManager ConfigManager;
Properties.load (InputStream); Reading property file
private static properties properties;
To read a configuration file while constructing a tool class
Private ConfigManager () {
String configfile = "Database.properties";
Properties = new properties ();
InputStream is = ConfigManager.class.getClassLoader ()
. getResourceAsStream (ConfigFile);
try {
Reading configuration Files
Properties.load (IS);
Is.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
To set the number of instantiations through a single case mode
public static ConfigManager getinstance () {
if (ConfigManager = = null) {
ConfigManager = new ConfigManager ();
}
return ConfigManager;
}
Get the corresponding value by key
public string getString (string key) {
return Properties.getproperty (key);
}
}
Basedao.java
Copy Code code as follows:
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Com.book.util.ConfigManager;
Base class: Database Operations General Class
public class Basedao {
protected Connection Conn;
protected PreparedStatement PS;
protected Statement stmt;
protected ResultSet RS;
Getting a database connection
public Boolean getconnection () {
Read configuration information
String Driver = Configmanager.getinstance (). GetString (
"Jdbc.driver_class");
String URL = configmanager.getinstance (). GetString (
"Jdbc.connection.url");
String username = configmanager.getinstance (). GetString (
"Jdbc.connection.username");
String password = configmanager.getinstance (). GetString (
"Jdbc.connection.password");
Load JDBC Driver
try {
Class.forName (driver);
Establishing a connection to the database
conn = drivermanager.getconnection (URL, username, password);
catch (ClassNotFoundException e) {
return false;
catch (SQLException e) {
return false;
}
return true;
}
The general method of adding and deleting and modifying
public int executeupdate (String sql, object[] params) {
int updaterows = 0;
Getconnection ();
try {
PS = conn.preparestatement (SQL);
Fill placeholder
for (int i = 0; i < params.length; i++) {
Ps.setobject (i + 1, params[i]);
SYSTEM.OUT.PRINTLN (i + 1 + "---" + params[i]);
}
SYSTEM.OUT.PRINTLN (SQL);
Updaterows = Ps.executeupdate ();
catch (SQLException e) {
E.printstacktrace ();
}
return updaterows;
}
Inquire
Public ResultSet ExecuteSQL (String sql, object[] params) {
Getconnection ();
try {
PS = conn.preparestatement (SQL);
Fill placeholder
for (int i = 0; i < params.length; i++) {
Ps.setobject (i + 1, params[i]);
}
rs = Ps.executequery ();
catch (SQLException e) {
E.printstacktrace ();
}
Return RS;
}
Close Resource
public void CloseAll () {
try {
if (rs!=null) {
Rs.close ();
}
if (stmt!=null) {
Stmt.close ();
}
if (ps!=null) {
Ps.close ();
}
if (conn!=null) {
Conn.close ();
}
catch (SQLException e) {
E.printstacktrace ();
}
}
}