1. New class Oraclepersiondao in DAO layer
PackageCom.test.dao;ImportJava.sql.*;/*** Created by WDW on 2017/9/16.*/ Public classOraclepersiondao {//database-driven classes PrivateString dbdriver = "Oracle.jdbc.driver.OracleDriver"; //Connection Database URL PrivateString Dburl = "JDBC:ORACLE:THIN:@192.168.31.128:1521:ORCL"; //Connect to database user name PrivateString dbUser = "PHIP"; //Connect Database Password PrivateString dbpwd = "PHIP"; //Gets the database connection method that returns the Connection object PrivateConnection con =NULL; //Data Execution Statements PrivateStatement stat =NULL; PrivateString sql =NULL; PrivateResultSet rs =NULL; //Create a database connection PublicConnection Getdbconnect () {Try {//Load Database driverClass.forName (Dbdriver); Con=drivermanager.getconnection (Dburl, DbUser, dbpwd); } Catch(Exception e) {System.out.println (e); } returncon; } //Increase Public voidAdd () {con=Getdbconnect (); SQL= "INSERT into Sys_users (id,code,name)" + "VALUES (' 1 ', ' Lucy ', ' W ')"; Try{stat=con.createstatement (); Stat.executeupdate (SQL); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //Delete Public voidDelete () {con=Getdbconnect (); SQL= "Delete from sys_users" + "where id=1"; Try{stat=con.createstatement (); Stat.executeupdate (SQL); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //Modify Public voidUpdate () {con=Getdbconnect (); SQL= "Update sys_users set code= ' C2 ' where id= ' 1 '"; Try{stat=con.createstatement (); } Catch(SQLException e) {e.printstacktrace (); } } //Enquiry Public voidSearch () {con=Getdbconnect ();//to create a table's SQL statementsql = "SELECT * FROM Sys_users"; Try{stat=con.createstatement (); RS=stat.executequery (SQL); while(Rs.next ()) {String ID= rs.getstring (1); String Stuname= Rs.getstring (2); String Stusex= Rs.getstring (3); System.out.println (ID+ "," + Stuname + "," +stusex); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }//function: Close the connection of the database Public voidClose () {//6. Releasing Resources Try{//Catching exceptions Try { if(rs! =NULL) {//when an instance of the ResultSet object RS is not emptyrs.close ();//close the ResultSet object } } finally { Try { if(Stat! =NULL) {//When an instance of the statement object stmt is not emptystat.close ();//Close the Statement object } } finally { if(Con! =NULL) {//When an instance of the connection object conn is not emptyCon.close ();//Close the Connection object } } } } Catch(Exception e) {e.printstacktrace (system.err);//Output Exception Information } }}
View Code
2. Main
Private Static void Mysqloperator () { new Oraclepersiondao (); Oracl. Add (); Oracl. Search (); Oracl. Update (); Oracl. Delete (); }
View Code
3. Increase the Oracle drive to this project
Locate the directory where Oracle is installed X:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar (note that different versions will have different names)
Copy the file, click Web-info,ctrl+v Paste file, point Okokok
Right-click to paste the file, Add as library always click OK. ,
3. Testing
Java Connection Oracle for additional pruning and checking