Java Connection Orcale Database sample sharing _java

Source: Internet
Author: User

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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.