Java Foundation-JDBC Access database

Source: Internet
Author: User
Tags access database

Basic steps:

    1. Load Database Driver
    2. Establish a connection
    3. Creating SQL statements
    4. Execute SQL statement
    5. Working with execution results
    6. Freeing resources

code example:

1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 Importjava.sql.PreparedStatement;4 ImportJava.sql.ResultSet;5 Importjava.sql.Statement;6 7 Importjunit.framework.TestCase;8 9  Public classjdbctestTen     extendsTestCase One { A @org. Junit.test -      Public voidTESTJDBC ()throwsexception{ - //1. Load Driver the         //class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); -         //class.forname ("Com.mysql.jdbc.Driver"); -Class.forName ("Oracle.jdbc.driver.OracleDriver"); - //2. Create a database Connection object +         //Connection conn = drivermanager.getconnection ("Jdbc:sqlserver://localhost:1433;databasename=db "," sa "," sqlpass "); -         //Connection conn = drivermanager.getconnection ("Jdbc:mysql://Localhost:3306/db?useunicode=true&characterencoding=utf-8 "," root "," MySQL "); +Connection conn=drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:orcl", "Scott", "ORCL"); A //3. Create DATABASE Execution Commands atStatement st=conn.createstatement (); -PreparedStatement ps=conn.preparestatement ("select * from EMP ORDER by 8"); - //4. Execute database Commands -ResultSet rs=st.executequery ("select * from EMP ORDER by 8"); -ResultSet prs=ps.executequery (); - //5. Processing execution Results in          while(Rs.next ()) { -             intEmpno=rs.getint ("Empno"); toString ename=rs.getstring (2); +Integer Deptno=rs.getint (8); -System.out.println ("Statement---Work No.:" +empno+ "Name:" +ename+ "Department:" +deptno); the         } *          while(Prs.next ()) { $             intEmpno=prs.getint ("Empno");Panax NotoginsengString ename=prs.getstring (2); -Integer Deptno=prs.getint (8); theSystem.out.println ("PreparedStatement---Work No.:" +empno+ "Name:" +ename+ "Department:" +deptno); +         } A //6. Releasing Database Resources the         if(NULL!=rs| |NULL!=PRS) { + rs.close (); - prs.close (); $         } $ st.close (); - ps.close (); - conn.close (); the     } -}

Execution Result:

Statement---Work No.: 7782 Name: CLARK Department: 10
Statement---Work No.: 7839 Name: KING Department: 10
Statement---No.: 7934 Name: MILLER Department: 10
Statement---Work No.: 7566 Name: JONES Department: 20
Statement---Work No.: 7902 Name: FORD Department: 20
Statement---No.: 7876 Name: ADAMS Department: 20
Statement---Work No.: 7369 Name: SMITH Department: 20
Statement---No.: 7788 Name: SCOTT Department: 20
Statement---No.: 7521 Name: WARD Department: 30
Statement---No.: 7844 Name: TURNER Department: 30
Statement---No.: 7499 Name: ALLEN Department: 30
Statement---No.: 7900 Name: JAMES Department: 30
Statement---Work No.: 7698 Name: BLAKE Department: 30
Statement---No.: 7654 Name: MARTIN Department: 30
PreparedStatement---Work No.: 7782 Name: CLARK Department: 10
PreparedStatement---Work No.: 7839 Name: KING Department: 10
PreparedStatement---No.: 7934 Name: MILLER Department: 10
PreparedStatement---Work No.: 7566 Name: JONES Department: 20
PreparedStatement---Work No.: 7902 Name: FORD Department: 20
PreparedStatement---No.: 7876 Name: ADAMS Department: 20
PreparedStatement---Work No.: 7369 Name: SMITH Department: 20
PreparedStatement---No.: 7788 Name: SCOTT Department: 20
PreparedStatement---No.: 7521 Name: WARD Department: 30
PreparedStatement---No.: 7844 Name: TURNER Department: 30
PreparedStatement---No.: 7499 Name: ALLEN Department: 30
PreparedStatement---No.: 7900 Name: JAMES Department: 30
PreparedStatement---Work No.: 7698 Name: BLAKE Department: 30
PreparedStatement---No.: 7654 Name: MARTIN Department: 30

Java Foundation-JDBC Access database

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.