Java Connection Oracle for additional pruning and checking

Source: Internet
Author: User

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

Related Article

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.