Basic steps:
- Load Database Driver
- Establish a connection
- Creating SQL statements
- Execute SQL statement
- Working with execution results
- 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