JDBC基础实例

来源:互联网 发布:软件版权局 编辑:程序博客网 时间:2024/06/05 17:11

步骤

实现此案例需要按照如下步骤进行。

步骤一:导入连接Oracle数据库所需的jar包

创建工程,在当前工程下导入连接Oracle数据库对应的驱动程序jar包。

步骤二:新建类EmpDAO及方法findAll

代码如下所示:

  1. public class EmpDAO {
  2.     public static void main(String[] args) {
  3.     }
  4.     public void findAll() {
  5.     }
  6. }

步骤三:构建连接数据所需的对象以及相应的异常处理

在findAll方法中,构建连接数据所需的对象以及相应的异常处理,代码如下所示:

  1. public class EmpDAO {
  2.     public static void main(String[] args) {
  3.     }
  4.     public void findAll() {
  5.         Connection con = null;
  6.         Statement stmt = null;
  7.         ResultSet rs = null;
  8.         try {
  9.             Class.forName("oracle.jdbc.OracleDriver");
  10.             
  11.         } catch (ClassNotFoundException e) {
  12.             System.out.println("驱动类无法找到!");
  13.             throw new RuntimeException(e);
  14.         } catch (SQLException e) {
  15.             System.out.println("数据库访问异常!");
  16.             throw new RuntimeException(e);
  17.         }
  18.     }
  19. }

步骤四:装载驱动程序

代码如下所示:

  1. public class EmpDAO {
  2.     public static void main(String[] args) {
  3.     }
  4.     public void findAll() {
  5.         Connection con = null;
  6.         Statement stmt = null;
  7.         ResultSet rs = null;
  8.         try {
  9.             Class.forName("oracle.jdbc.OracleDriver");
  10.         } catch (ClassNotFoundException e) {
  11.             System.out.println("驱动类无法找到!");
  12.             throw new RuntimeException(e);
  13.         } catch (SQLException e) {
  14.             System.out.println("数据库访问异常!");
  15.             throw new RuntimeException(e);
  16.         }
  17.     }
  18. }

步骤五:建立连接。

通过调用DriverManager的getConnection方法,获取Connection类的对象,建立连接。代码如下所示:

  1. public class EmpDAO {
  2.     public static void main(String[] args) {
  3.     }
  4.     public void findAll() {
  5.         Connection con = null;
  6.         Statement stmt = null;
  7.         ResultSet rs = null;
  8.         try {
  9.             Class.forName("oracle.jdbc.OracleDriver");
  10.             con = DriverManager.getConnection(
  11.                     "jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");
  12.         } catch (ClassNotFoundException e) {
  13.             System.out.println("驱动类无法找到!");
  14.             throw new RuntimeException(e);
  15.         } catch (SQLException e) {
  16.             System.out.println("数据库访问异常!");
  17.             throw new RuntimeException(e);
  18.         }
  19.     }
  20. }

步骤六:发送和执行SQL语句

首先,通过Connection的createStatement()方法获取数据库操作对象Statement。通过调用Statement对象的executeQuery方法来执行SQL语句。代码如下所示:

  1. public class EmpDAO {
  2.     public static void main(String[] args) {
  3.     }
  4.     public void findAll() {
  5.         Connection con = null;
  6.         Statement stmt = null;
  7.         ResultSet rs = null;
  8.         try {
  9.             Class.forName("oracle.jdbc.OracleDriver");
  10.             con = DriverManager.getConnection(
  11.                     "jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");
  12.             stmt = con.createStatement();
  13.             rs = stmt
  14.                     .executeQuery("select empno, ename, sal, hiredate from emp");
  15.         } catch (ClassNotFoundException e) {
  16.             System.out.println("驱动类无法找到!");
  17.             throw new RuntimeException(e);
  18.         } catch (SQLException e) {
  19.             System.out.println("数据库访问异常!");
  20.             throw new RuntimeException(e);
  21.         }
  22.     }
  23. }

步骤七:处理查询结果

Statement的executeQuery方法的返回值为ResultSet对象。ResultSet表示数据库查询操作的结果集。它具有指向其当前数据行的光标。最初,光标被置于第一行之前,调用其next 方法将光标移动到下一行,该方法在 ResultSet 对象没有下一行时返回 false,因此可以在 while 循环中使用它来迭代结果集。代码如下所示:

  1. public class EmpDAO {
  2.     public static void main(String[] args) {
  3.     }
  4.     public void findAll() {
  5.         Connection con = null;
  6.         Statement stmt = null;
  7.         ResultSet rs = null;
  8.         try {
  9.             Class.forName("oracle.jdbc.OracleDriver");
  10.             con = DriverManager.getConnection(
  11.                     "jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");
  12.             stmt = con.createStatement();
  13.             rs = stmt
  14.                     .executeQuery("select empno, ename, sal, hiredate from emp");
  15.             while (rs.next()) {
  16.                 System.out.println(rs.getInt("empno") + ","
  17.                         + rs.getString("ename") + ","
  18.                         + rs.getDouble("sal") + "," + rs.getDate("hiredate"));
  19.             }
  20.         } catch (ClassNotFoundException e) {
  21.             System.out.println("驱动类无法找到!");
  22.             throw new RuntimeException(e);
  23.         } catch (SQLException e) {
  24.             System.out.println("数据库访问异常!");
  25.             throw new RuntimeException(e);
  26.         }
  27.     }
  28. }

从上述代码中看出ResultSet提供了getXXX(String column)方法,例如:getInt(String column)等,获取当前ResultSet对象的当前行中指定列名的值,其中参数column表示数据库表中的列名字。

步骤八:释放资源

在finally块中,依次关闭ResultSet对象、Statement对象以及Connection对象。代码如下:

  1. public class EmpDAO {
  2.     public static void main(String[] args) {
  3.     }
  4.     public void findAll() {
  5.         Connection con = null;
  6.         Statement stmt = null;
  7.         ResultSet rs = null;
  8.         try {
  9.             Class.forName("oracle.jdbc.OracleDriver");
  10.             con = DriverManager.getConnection(
  11.                     "jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");
  12.             stmt = con.createStatement();
  13.             rs = stmt
  14.                     .executeQuery("select empno, ename, sal, hiredate from emp");
  15.             while (rs.next()) {
  16.                 System.out.println(rs.getInt("empno") + ","
  17.                         + rs.getString("ename") + ","
  18.                         + rs.getDouble("sal") + "," + rs.getDate("hiredate"));
  19.             }
  20.         } catch (ClassNotFoundException e) {
  21.             System.out.println("驱动类无法找到!");
  22.             throw new RuntimeException(e);
  23.         } catch (SQLException e) {
  24.             System.out.println("数据库访问异常!");
  25.             throw new RuntimeException(e);
  26.         } finally {
  27.             try {
  28.                 if (rs != null) {
  29.                     rs.close();
  30.                 }
  31.                 if (stmt != null) {
  32.                     stmt.close();
  33.                 }
  34.                 if (con != null) {
  35.                     con.close();
  36.                 }
  37.             } catch (SQLException e) {
  38.                 System.out.println("关闭连接时发生异常");
  39.             }
  40.         }
  41.     }
  42. }

步骤九:测试

在main方法中,调用findAll方法,代码如下所示:

  1. public class EmpDAO {
  2.     public static void main(String[] args) {
  3.         EmpDAO dao = new EmpDAO();
  4.         dao.findAll();
  5.     }
  6.     public void findAll() {
  7.         Connection con = null;
  8.         Statement stmt = null;
  9.         ResultSet rs = null;
  10.         try {
  11.             Class.forName("oracle.jdbc.OracleDriver");
  12.             con = DriverManager.getConnection(
  13.                     "jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");
  14.             stmt = con.createStatement();
  15.             rs = stmt
  16.                     .executeQuery("select empno, ename, sal, hiredate from emp");
  17.             while (rs.next()) {
  18.                 System.out.println(rs.getInt("empno") + ","
  19.                         + rs.getString("ename") + ","
  20.                         + rs.getDouble("sal") + "," + rs.getDate("hiredate"));
  21.             }
  22.         } catch (ClassNotFoundException e) {
  23.             System.out.println("驱动类无法找到!");
  24.             throw new RuntimeException(e);
  25.         } catch (SQLException e) {
  26.             System.out.println("数据库访问异常!");
  27.             throw new RuntimeException(e);
  28.         } finally {
  29.             try {
  30.                 if (rs != null) {
  31.                     rs.close();
  32.                 }
  33.                 if (stmt != null) {
  34.                     stmt.close();
  35.                 }
  36.                 if (con != null) {
  37.                     con.close();
  38.                 }
  39.             } catch (SQLException e) {
  40.                 System.out.println("关闭连接时发生异常");
  41.             }
  42.         }
  43.     }
  44. }

运行EmpDAO类,控制台输出结果如下所示:

  1. 7369,SMITH,,800.0,1980-12-17
  2. 7499,ALLEN,,1600.0,1981-02-20
  3. 7521,WARD,,1250.0,1981-02-22
  4. 7566,JONES,,2975.0,1981-04-02
  5. 7654,MARTIN,,1250.0,1981-09-28
  6. 7698,BLAKE,,2850.0,1981-05-01
  7. 7782,CLARK,,2450.0,1981-06-09
  8. 7788,SCOTT,,3000.0,1987-04-19
  9. 7839,KING,,5000.0,1981-11-17
  10. 7844,TURNER,,1500.0,1981-09-08
  11. 7876,ADAMS,,1100.0,1987-05-23
  12. 7900,JAMES,,950.0,1981-12-03
  13. 7902,FORD,,3000.0,1981-12-03
  14. 7934,MILLER,,1300.0,1982-01-23

从输出结果可以看出,已经查询到emp表的所有员工的ID、姓名、薪资以及入职时间。

0 0