Java基础-JDBC访问数据库

基本步骤:

  1. 加载数据库驱动
  2. 建立连接
  3. 创建SQL语句
  4. 执行SQL语句
  5. 处理执行结果
  6. 释放资源

代码示例:

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.PreparedStatement;
 4 import java.sql.ResultSet;
 5 import java.sql.Statement;
 6 
 7 import junit.framework.TestCase;
 8 
 9 public class JDBCTest 
10     extends TestCase
11 {
12     @org.junit.Test
13     public void testJDBC() throws Exception{
14 //        1.加载驱动
15         //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
16         //Class.forName("com.mysql.jdbc.Driver");
17         Class.forName("oracle.jdbc.driver.OracleDriver");
18 //        2.创建数据库连接对象
19         //Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=db","sa","sqlpass");
20         //Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8","root","mysql");
21         Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","orcl");
22 //        3.创建数据库执行命令
23         Statement st=conn.createStatement();
24         PreparedStatement ps=conn.prepareStatement("SELECT * FROM EMP ORDER BY 8");
25 //        4.执行数据库命令
26         ResultSet rs=st.executeQuery("SELECT * FROM EMP ORDER BY 8");
27         ResultSet prs=ps.executeQuery();
28 //        5.处理执行结果
29         while(rs.next()){
30             int empno=rs.getInt("empno");
31             String ename=rs.getString(2);
32             Integer deptno=rs.getInt(8);
33             System.out.println("Statement---工号:"+empno+" 姓名:"+ename+" 部门:"+deptno);
34         }
35         while(prs.next()){
36             int empno=prs.getInt("empno");
37             String ename=prs.getString(2);
38             Integer deptno=prs.getInt(8);
39             System.out.println("PreparedStatement---工号:"+empno+" 姓名:"+ename+" 部门:"+deptno);
40         }
41 //        6.释放数据库资源
42         if(null!=rs||null!=prs){
43             rs.close();
44             prs.close();
45         }
46         st.close();
47         ps.close();
48         conn.close();
49     }
50 }

执行结果:

Statement---工号:7782 姓名:CLARK 部门:10
Statement---工号:7839 姓名:KING 部门:10
Statement---工号:7934 姓名:MILLER 部门:10
Statement---工号:7566 姓名:JONES 部门:20
Statement---工号:7902 姓名:FORD 部门:20
Statement---工号:7876 姓名:ADAMS 部门:20
Statement---工号:7369 姓名:SMITH 部门:20
Statement---工号:7788 姓名:SCOTT 部门:20
Statement---工号:7521 姓名:WARD 部门:30
Statement---工号:7844 姓名:TURNER 部门:30
Statement---工号:7499 姓名:ALLEN 部门:30
Statement---工号:7900 姓名:JAMES 部门:30
Statement---工号:7698 姓名:BLAKE 部门:30
Statement---工号:7654 姓名:MARTIN 部门:30
PreparedStatement---工号:7782 姓名:CLARK 部门:10
PreparedStatement---工号:7839 姓名:KING 部门:10
PreparedStatement---工号:7934 姓名:MILLER 部门:10
PreparedStatement---工号:7566 姓名:JONES 部门:20
PreparedStatement---工号:7902 姓名:FORD 部门:20
PreparedStatement---工号:7876 姓名:ADAMS 部门:20
PreparedStatement---工号:7369 姓名:SMITH 部门:20
PreparedStatement---工号:7788 姓名:SCOTT 部门:20
PreparedStatement---工号:7521 姓名:WARD 部门:30
PreparedStatement---工号:7844 姓名:TURNER 部门:30
PreparedStatement---工号:7499 姓名:ALLEN 部门:30
PreparedStatement---工号:7900 姓名:JAMES 部门:30
PreparedStatement---工号:7698 姓名:BLAKE 部门:30
PreparedStatement---工号:7654 姓名:MARTIN 部门:30

原文地址:https://www.cnblogs.com/yw0219/p/6107925.html