The JDBC Connection database process is:
1, load driver, establish connection
2, creating the SQL statement object
3, Execute SQL statement
4, processing result sets
5, close the connection
These five steps are about 4 major points of knowledge:
1, Drive management DriverManager
Classforname ("Oracle.jdbc.driver.OracleDriver")
2, Connection object
Connection interface: Responsible for the application to the database connection, after loading the driver, using Url,username,password three parameters to create a specific database connection 1
3,sql Statement Object Interface
The statement interface is used to process the SQL statement object sent to the database
4, Result set interface
ResultSet interface: A collection of results returned after executing a query SQL statement.
public void FindAll () {
Connection Con=null;
Statement Stmt=null;
ResultSet Rs=null;
try {
Class.forName ("Oracle.jdbc.OracleDriver");
Con=drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:xe";, "username", "password");
Stmt=con.createstatement ();
String sql= "Select Empno, ename, Sal, hiredate from EMP";
Rs=stmt.executequery (SQL);
while (Rs.next ()) {
System.out.println (Rs.getint ("empno") + ","
+ rs.getstring ("ename") + ","
+ rs.getdouble ("sal") + "," + rs.getdate ("HireDate"));
}
} catch (ClassNotFoundException e) {
System.out.println ("Driver class cannot be found!");
throw new RuntimeException (e);
} catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("Database access Exception!");
throw new RuntimeException (e);
}finally {
try {
if (rs! = null) {
Rs.close ();
}
if (stmt! = null) {
Stmt.close ();
}
if (con! = null) {
Con.close ();
}
} catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("Exception occurred while closing the connection");
}
}
}
JDBC Connect Oracle