JDBC API访问数据库的基本步骤。

JDBC本质:官方定义了一套操作所有关系型数据库的规则(接口),各个数据库厂商实现这个接口,提供数据库驱动jar包。

  我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类。

任何一个Java应用程序使用JDBC API访问数据库,其基本工作可以分为5个步骤:

(1)加载JDBC驱动程序;

(2)建立数据库连接;

(3)创建操作数据库SQL的Statement、PreparedStatement或CallableStatement对象;

(4)执行语句并分析执行结果;

(5)关闭连接。

 

 

1、加载JDBC驱动程序

    利用Class类的方法forName(String driverName)加载JDBC驱动,不同的数据库的JDBC驱动名称不同,MySql的驱动类名为com.mysql.jdbc.Driver。

Class forName("com.mysql.jdbc.Driver");

    //注意:
      //在Java Web应用开发中,如果要访问数据库,首先应将加载数据库的JDBC驱动程序(jar包)复制到Web应用程序的WEB-INF\lib 目录下,这样Web应用程序才能正常地通过JDBC接口访问数据库。

2、建立数据库的连接

    利用DriverManager类的方法getConnection()获得与特定数据库的连接实例(Connection实例)。例如创建一个连接的本地MySQL数据库userdb Connection对象,假设该数据库的用户名为root、密码为passwd,代码如下:

    //第一种方式:
      String url = "jdbc:mysql://localhost:3306/userdb?user = root & password = passwd";       Connection con = DriverManager.getConnection(url);     //第二种方式:
      private static final String url = "jdbc:mysql://localhost:3306/userdb";       private static final String user = "root";       private static final String password = "111111";       protected static Connection con = null;       con = DriverManager.getConnection(url , user , password);

 

3、进行数据库操作

    对数据库的操作依赖于SQL语句,JDBC依据SQL语句的类型不同提供了三种执行SQL的对象,即Statement,PreparedStatement和CallableStatement。

    1)Statement对象执行静态SQL语句

    Statement对象用于执行不含参数的静态SQL语句,JDBC利用Connection实例的createStatement()方法创建一个Statement实例,通过Statement实例的execute()方法(不常用)、executeQuerry()方法(select)或者executeUpdate()方法(insert、update或 delete)等执行SQL语句。例如查询数据   表tb_users中的所有记录:

  //创建Statement对象,其中con为Connection对象
    Statement statement = con.createStatement();   
  //使用executeQuery()方法执行SELECT语句,该方法的返回值为ResultSet类型  
   ResultSet rs = statement.executeQuerry(select*fron tb_users);

    再如,删除数据表tb_users中的fd_username 为Allen的记录:

  //创建Statement对象,其中con为Connection对象
    Statement statement = con.createStatement();
  
//使用executeUpdate()方法执行INSERT、UPDATE、DELETE等SQL语句,返回 int 类型
   int result = 0;      result = statement.executeUpdate("delete from tb_users where fd_username = 'Allen' ");

    2)PreparedStatement 执行动态SQL语句

    PreparedStatement用于执行含有动态的SQL语句,动态SQL语句是指在程序运行时能动态地为SQL语句的参数赋值,增加了程序的灵活性。PreparedStatement 对象由Connection 实例的 preparedStatement(String sql)方法构建。

      PreparedStatement 接口也有executeQuery()和executeUpdate()方法,但这两个方法都不带参数。该接口提供了setXxx(int paramIndex , xxx  var)方法为动态的SQL语句中的参数赋值。这里仍以上面的SQL操作为例说明PreparedStatement 对象     的创建和相关方法的使用。

  // 声明动态SQL ,参数使用?占位符表示
    String sqlSelect = "select * from tb_users where fd_username = ?";
  
//创建PreparedStatement 对象psQuery,其中con为Connection对象     PreparedStatement PSQuery = con. preparedStatement(sqlSelect);
  
/*为动态SQL语句中的参数赋值,由于fd_username为Sting类型,故使用setString()方法为参数赋值,1代表动态SQL语句中的第一个问题(第一个问号处值)*/     psQuery.setString(1,"Allen");
    String sqlDelete = "delete from tb_users where fd_username = ?";
//创建PreparedStatement 对象 psUpdate ,其中con为Connection对象    PreparedStatement psUpdate = con.preparedStatement(sqlDelete);
  
//使用executeUpdate()方法执行INSERT、UPDATE、DELETE等SQL语句,这里列举的是 删除
  ResultSet result = psUpdate.executeUpdate();

4、对执行结果进行分析处理

    1)分析查询结果集 ResultSet

    在执行SELECT语句后必然产生一个ResultSet结果集实例,对结果集进行分析与处理是应用程序的最终目标,可以使用循环遍历结果集中的每一行记录,使用getXxx()方法获取遍历记录行指定列的数据。

  //查询tb_users表中记录的用户名和性别
    ResultSet rs = statement.executeQuerry("select fd_username , fd_gender from tb_users");
  //使用next() 方法判断结果集是否有下一行,从而遍历结果集中的所有记录行
    while(rs.next()){
    //获取遍历记录行中列名fd_username的对应值
      String username = rs.getString("fd_username");
    //获取遍历记录行中第二列的对应值
      String gender = rs.getString(2);
      System.out.println("用户名:"+ username + ",性别:"+ gender);

    2)分析执行结果

    对于INSERT、UPDATE、DELETE甚至是SQL DLL语句,一般由executeUpdate()方法执行且返回值为int 类型,表示受影响的记录行数,如返回值大于0表示该SQL语句成功执行。

    int result = 0;
    String sql = "delete from tb_users where fd_username = 'Allen' ";
  // execteUpdate()方法执行INSERT、UPDATE、DELETE等SQL语句,返回int类型
    result = statement.executeUpdate(sql);
  //分析执行结果
    if(result > 0){
        System.out.println("删除成功!");
    else
        System.out.println("删除失败!");

5、关闭JDBC相关对象

应用程序在对数据库操作完成以后,要把使用的所有JDBC对象全部关闭,以释放JDBC资源,关闭顺序和声明顺序正好相反。

(1)关闭结果集ResultSet 对象;

(2)关闭Statement、PreparedStatement对象;

(3)关闭连接对象

//关闭结果集rs
if (rs != null){
  try {
       rs.close();
     } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
}
//关闭Statement对象statement
if (statement!= null) {
    try {
       statement.close();
     } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
 }
//关闭连接对象 con
if (con!= null) {
    try {
       con.close();
     } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
}

 

 

 

 

  Java应用程序用过JDBC操作5个基本步骤。首先加载JDBC驱动,使用DriverManage对象创建Connection实例与制定的数据库建立连接;

然后使用Connection实例的createStatement()方法创建Statement实例(也可以创建PreparedStatement和CallableStatement对象),

并利用Statement实例的executeQuery()方法执行INSERT、UPDATE、DELETE等语句,并通过分析与处理执行结果实现应用程序的具体功能,

最后务必关闭与数据库之间的连接。

posted on 2019-07-15 19:33  Hi,Bro  阅读(6140)  评论(0编辑  收藏  举报