has been using MySQL, has recently switched to Oracle, Oracle's first step is the JDBC connection
The first step: Download the jar package, I use the oracle11g is Oracle6.jar
Step two: Load the jar package
Then find the downloaded Jar location.
Step Three: Code section
PackageCom.lc.jdbc;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.util.ArrayList;Importjava.util.List; Public classORACLE_JDBC {StaticString Driver = "Oracle.jdbc.driver.OracleDriver";//database-driven classes StaticString username = "ATM";//Database user name StaticString password = "ATM";//Database Password StaticString url = "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL";//the address of the connection database//Traverse PublicList<userinfo>Select () {Connection con; PreparedStatement PS; String SQL= "SELECT * from UserInfo"; List<Userinfo> userinfolist =NewArraylist<userinfo>(); Try{class.forname (driver);//Load Drivercon = drivermanager.getconnection (URL, username, password);//Create a connectionPS = con.preparestatement (SQL);//to create a precompiled SQL DeclarationResultSet rs =Ps.executequery (); while(Rs.next ()) {Userinfo U=NewUserinfo (); U.setcustomerid (Rs.getstring ("CustomerID")); U.setcustomername (Rs.getstring ("CustomerName")); U.setpid (Rs.getstring ("pid")); U.settelephone (Rs.getstring ("Telephone")); U.setaddress ("Address"); Userinfolist.add (U); } rs.close (); Ps.close (); Con.close ();//Close Connection}Catch(Exception e) {e.printstacktrace (); } returnuserinfolist; }}
The above is just a simple traversal, like adding and removing changes almost like, can not
Entity class table: corresponds to Oracle table one by one
Package Com.lc.jdbc;public class Userinfo {@Overridepublic String toString () {return "Userinfo [customerid=" + CustomerID + ", customername=" + CustomerName + ", pid=" + pid + ", telephone=" + telephone + ", address=" + address + "]";} public string Customerid;public string Customername;public string Pid;public string telephone;public string address; Public String Getcustomerid () {return customerid;} public void Setcustomerid (String customerid) {This.customerid = CustomerID;} Public String Getcustomername () {return customername;} public void Setcustomername (String customername) {this.customername = CustomerName;} Public String Getpid () {return PID;} public void Setpid (String pid) {this.pid = pid;} Public String Gettelephone () {return telephone;} public void Settelephone (String telephone) {this.telephone = telephone;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;}}
This is a success,
Here is the test code and the results of Oracle's operation
Packagecom.lc.test;Importjava.util.List;ImportCom.lc.jdbc.Oracle_jdbc;ImportCom.lc.jdbc.Userinfo; Public classTest { Public voidTest () {System.out.println ("Oracle Query ..."); ORACLE_JDBC OJ=NewOracle_jdbc (); List<Userinfo> list =OJ. Select (); for(Userinfo u:list) {System.out.println (u.tostring ()); } } Public Static voidMain (string[] args) {Newtest (). Test (); }}
The above is the JDBC connection for Oracle!
JDBC Connection for Oracle