JDBC Operations Summary

Source: Internet
Author: User

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

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.