How JDBC Works
-Load Driver
-Create Statement Object
-Execute SQL statement
-Working with result sets
-Close Connection
Drive Load Mode
Class.forName ("Oracle.jdbc.driver.OracleDriver") (note: The Load driver class driver class implements autoenrollment in DriverManager via a static block)
Connection con=drivermanager.getcommection ("Address parameter", "Database Account", "Database Password"), note: Connection is only the interface is actually implemented by the database vendor provided by the driver package is completed
Statement interface (not recommended for performing static block drives in general projects)
Statement stmt = Con.createstatement ();
Oracle implementation to download the corresponding Ojdbc6.jar/ojdbc14.jar
Below we use Factory mode to establish data connection
Import java.io.IOExecption;
Import java.sql.SQLException;
Import java.sql.Commection;
Import java.util.Properties;
First, you need to have a db.properties file that stores the linked data information
The file saves the following data (note that there is no space in the stored file)
Driver=oracle ....... Database-driven
url=localhost//Database Address
User=root//Database name
pwd=root//Database Password
Data loading
Class Trean () {
private static driver; Database-driven
private static URL//database address
Prvate static user//database name
private static PWD//Database Password
Because this data needs to be loaded once, it's OK.
static{
Need to instantiate object properties
try{
Properties P = new properties ();
P.load (Trean.class.getClassLoader (). getResourceAsStream ())
Driver = P.getproperty ("Driver");
url = p.getproperty ("url");
user = P.getproperty ("user");
PWD = P.getproperty ("pwd");
Class.forName (driver);//Load Drive
}catch (exeption e) {
E.printstacktrace ();
throw new Runexception ("Load resource failed", E);
}
}
Public Connection getconnection () throws sqlexception{
Return drivermanager.getcommection (URL,USER,PWD);
}
public void Colse (Connection con) {
if (con!=null) {
try{
Con.close
}catch (exeption e) {
E.printstacktrace ();
throw new Runexception ("Close connection Failed", e);
}
}
}
}
Java JDBC Link Database