zoukankan      html  css  js  c++  java
  • JDBC:Java连接数据库的桥梁

    JDBCJava DataBase Connection),java数据库连接,是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,同时JDBC也是个商标名。

    简单地说,JDBC可做三件事:

    1、与数据库建立连接;

    2、发送操作数据库的语句;

    3、处理结果。

    JDBC开发的六个基本步骤:(Mysql数据库为例)

    1、加载Driver

    2、获得数据库连接(Connection接口)

    3、创建Statement(接口,传送sql命令的工具)

    4、执行sql();

    5、如果执行了select,处理ResultSet(接口,结果集)

    6、释放资源

    第一步:注册Driver,加载Driver的实现类

    Class.forName(“com.mysql.jdbc.Driver”);

    第二步:获得Connection

            Connection connection = DriverManger.getConnection( url , username, password);

            其中String url =" jdbc:mysql:localhost:8081/db01";

            DriverManagerDriver的管理者,实际连接db的是Driver

    第三步:创建Statement

            Statement stm = conn.createStatement();

    第四步:执行sql命令

         返回boolean值,表示是否有ResultSet返回。true-->有  false-->没有

                   if( resultSet.next()){    ResultSet rs = stm.getResultSet();  }

         2.  executeQuery(sql) : 专门执行select(查询语句所用),返回ResultSet

         3.  executeUpdate(sql) : 专门执行insert update delete(添加,删除,修改操作所用),返回一个int值,表示命令,执行后,表里受到影响的行数

    第五步:处理ResultSet

          1. next() : 向下一行移动指针,同时返回一个boolean,用来表明当前行是否有数据

          2. get**()系列:获取当前的某一列数据。getInt(列的下标)   getString   getDate  getDouble

          3. 结果集里的指针开始处于”第一行的上一行“,结束时处于”最后一行的下一行“

    第六步:释放资源

          按照产生对象逆序释放相关资源  rs --> stm --> conn

    下面给出查询操作和添加操作的代码(修改,删除雷同):

    查询代码:

    public UserBean findById(int id){

      Connection connection = null;

      PreparedStatement preparedStatement = null;

      ResultSet resultSet = null;

      try {

      connection = getConnection(true);

      String sql = "select id,phone,pwd from user where id=?";

      preparedStatement = connection.prepareStatement(sql);

      preparedStatement.setInt(1, id);

      resultSet = preparedStatement.executeQuery();

      if(resultSet.next()){

      UserBean userBean = new UserBean();

      userBean.setId(resultSet.getInt(1));

      userBean.setPhone(resultSet.getString(2));

      userBean.setPwd(resultSet.getString(3));

      return userBean;

    }

    } catch (Exception e) {

      e.printStackTrace();

      } finally {

      try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}

      try {preparedStatement.close();} catch (SQLException e) {e.printStackTrace();}

      try {connection.close();} catch (SQLException e) {e.printStackTrace();}

      }

      return null;

    }

    添加代码:

    public boolean add(UserBean userBean){

      Connection connection = null;

      PreparedStatement preparedStatement = null;

       

      try {

      connection = getConnection(false);

      String sql = "insert into user(phone,pwd) value(?,?)";

      preparedStatement = connection.prepareStatement(sql);

      preparedStatement.setString(1, userBean.getPhone());

      preparedStatement.setString(2, MD5Util.getMD5Str(userBean.getPwd()));

      preparedStatement.executeUpdate();

      connection.commit();

      return true;

      } catch (Exception e) {

      try {connection.rollback();} catch (SQLException e1) {e1.printStackTrace();}

      e.printStackTrace();

      } finally {

      try {preparedStatement.close();} catch (SQLException e) {e.printStackTrace();}

      try {connection.close();} catch (SQLException e) {e.printStackTrace();}

      }

      return false;

      }

    其中涉及到的connection.commit();属于事件,将在之后的章节中介绍。 

  • 相关阅读:
    升级windows 11小工具
    windows 10更新升级方法
    您需要了解的有关 Oracle 数据库修补的所有信息
    Step by Step Apply Rolling PSU Patch In Oracle Database 12c RAC Environment
    Upgrade Oracle Database Manually from 12.2.0.1 to 19c
    如何应用版本更新 12.2.0.1.210420(补丁 32507738 – 2021 年 4 月 RU)
    xtrabackup 安装、备份和恢复
    Centos_Lvm expand capacity without restarting CentOS
    Centos_Lvm_Create pv vg lv and mount
    通过全备+relaylog同步恢复被drop的库或表
  • 原文地址:https://www.cnblogs.com/huskyking/p/5565196.html
Copyright © 2011-2022 走看看