1. Import the configuration file into the project
(1) Search classes12 (or other) under Oracle installation path
(2) Create a Lib folder in the project, copy the classes12 you just copied to the directory
(3) Right button, add path into project
Write code:
2. Configuration file:
Class.forName ("Oracle.jdbc.driver.OracleDriver");
3. Create the connection:
String url = "Jdbc:oracle:thin: @localhost: 1521:orcl"; = Drivermanager.getconnection (URL, "Scott", "123");
4. Obtain the application:
STA = conn.createstatement (); = Sta.executequery ("SELECT * from emp");
5. Operating data:
(1) Create a class with the same database structure
Public classEMP {Private intempno; PrivateString ename; PrivateString Job; Private intMgr; PrivateDate hiredate; Private DoubleSal; Private DoubleComm; Private intDeptno; PublicEmp () {} Public intGetempno () {returnempno;} Public voidSetempno (intempno) { This. empno =empno;} PublicString Getename () {returnename;} Public voidsetename (String ename) { This. ename =ename;} PublicString getjob () {returnjob;} Public voidsetjob (String job) { This. Job =job;} Public intgetmgr () {returnMgr;} Public voidSetmgr (intMgr) { This. Mgr =Mgr;} PublicDate gethiredate () {returnhiredate;} Public voidsethiredate (Date hiredate) { This. HireDate =hiredate;} Public Doublegetsal () {returnSal;} Public voidSetsal (DoubleSal) { This. Sal =Sal;} Public DoubleGetcomm () {returnComm;} Public voidSetcomm (DoubleComm) { This. Comm =Comm;} Public intGetdeptno () {returnDeptno;} Public voidSetdeptno (intdeptno) { This. Deptno =Deptno;} @Override PublicString toString () {return"Emp [empno=" + Empno + ", ename=" + ename + ", job=" + Job + ", mgr=" + Mgr + ", hiredate=" +HireDate+ ", sal=" + Sal + ", comm=" + Comm + ", deptno=" + Deptno + "]";} }
(2) Set up a ArrayList collection to store the queried data
list<emp> Emps =NewArraylist<emp>(); while(Set.next ()) {EMP emp=NewEmp (); Emp.setempno (Set.getint ("Empno")); Emp.setename (Set.getstring ("Ename")); Emp.setjob (Set.getstring ("Job")); Emp.setmgr (Set.getint ("Mgr")); Emp.sethiredate (Set.getdate ("HireDate")); Emp.setsal (Set.getdouble ("Sal")); Emp.setcomm (Set.getdouble ("Comm")); Emp.setdeptno (Set.getint ("Deptno")); Emps.add (EMP); }
(3) Output inspection
for (Emp emp:emps) { System.out.println (emp.tostring ()); }
6. Close the resource:
finally { Try { if(Set! =NULL) {set.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try { if(STA! =NULL) {sta.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try { if(Conn! =NULL) {conn.close (); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }
JDBC Operations Summary