zoukankan      html  css  js  c++  java
  • JDBC

    1,JDBC架构:

    1. JDBC API支持两层和三层处理模型进行数据库访问,但在一般的JDBC体系结构由两层组成:
    2. JDBC API: 提供了应用程序对JDBC的管理连接。
    3. JDBC Driver API: 支持JDBC管理到驱动器连接。
    4. JDBC API的使用驱动程序管理器和数据库特定的驱动程序提供透明的连接到异构数据库。
    5. JDBC驱动程序管理器可确保正确的驱动程序来访问每个数据源。该驱动程序管理器能够支持连接到多个异构数据库的多个并发的驱动程序。

    以下是结构图,它显示了驱动程序管理器方面的JDBC驱动程序和Java应用程序的位置:

    2,常见的JDBC组件:

    1. JDBC API提供了以下接口和类:
    2. DriverManager: 这个类管理数据库驱动程序的列表。内容是否符合从Java应用程序使用的通信子协议正确的数据库驱动程序的连接请求。识别JDBC在一定子协议的第一个驱动器将被用来建立数据库连接。
    3. Driver: 此接口处理与数据库服务器通信。很少直接与驱动程序对象。相反,使用DriverManager中的对象,它管理此类型的对象。它也抽象与驱动程序对象工作相关的详细信息
    4. Connection : 此接口与接触数据库的所有方法。连接对象表示通信上下文,即,与数据库中的所有的通信是通过唯一的连接对象。
    5. Statement : 可以使用这个接口创建的对象的SQL语句提交到数据库。一些派生的接口接受除执行存储过程的参数。
    6. ResultSet: 这些对象保存从数据库后,执行使用Statement对象的SQL查询中检索数据。它作为一个迭代器,让您可以通过移动它的数据。
    7. SQLException: 这个类处理发生在一个数据库应用程序的任何错误。

    3,创建JDBC应用程序

    1. 导入数据报(需要包括含有须有进行数据库编程的JDBC类的包,大多数情况下,使用import java.sql.*就可以了)
    2. 注册JDBC驱动程序(需要初始化驱动程序,可以与数据库打开一个通信通道)
    3. 打开连接(需要使用DriverManager.getConnection()方法创建一个Connection对象,它代表与数据库的物理连接);
    4. 执行查询(需要使用类型声明的对象建立并提交一个SQL语句到数据库)
    5. 从结果集中提取数据(要求使用适当的关于ResultSet.getXXX()方法来检索结果集的数据
    6. 需要明确地关闭所有的数据库资源相对依靠JVM的垃圾收集
    import java.sql.*;
    
    public class FirstExample {
       // JDBC driver name and database URL
       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
       static final String DB_URL = "jdbc:mysql://localhost:3306/EMP";
    
       //  Database credentials
       static final String USER = "root";
       static final String PASSWORD = "";
       
       public static void main(String[] args) {
       Connection conn = null;
       Statement stmt = null;
       try{
          //STEP 2: Register JDBC driver
          Class.forName("com.mysql.jdbc.Driver");
    
          //STEP 3: Open a connection
          System.out.println("Connecting to database...");
          conn = DriverManager.getConnection(DB_URL,USER,PASSWORD);
    
          //STEP 4: Execute a query
          System.out.println("Creating statement...");
          stmt = conn.createStatement();
          String sql;
          sql = "SELECT id, first, last, age FROM Employees";
          ResultSet rs = stmt.executeQuery(sql);
    
          //STEP 5: Extract data from result set
          while(rs.next()){
             //Retrieve by column name
             int id  = rs.getInt("id");
             int age = rs.getInt("age");
             String first = rs.getString("first");
             String last = rs.getString("last");
    
             //Display values
             System.out.print("ID: " + id);
             System.out.print(", Age: " + age);
             System.out.print(", First: " + first);
             System.out.println(", Last: " + last);
          }
          //STEP 6: Clean-up environment
          rs.close();
          stmt.close();
          conn.close();
       }catch(SQLException se){
          //Handle errors for JDBC
          se.printStackTrace();
       }catch(Exception e){
          //Handle errors for Class.forName
          e.printStackTrace();
       }finally{
          //finally block used to close resources
          try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing we can do
          try{
             if(conn!=null)
                conn.close();
          }catch(SQLException se){
             se.printStackTrace();
          }//end finally try
       }//end try
       System.out.println("Goodbye!");
    }//end main
    }//end FirstExample
    
    
    output:
    Connecting to database...
    Creating statement...
    ID: 100, Age: 18, First: Zara, Last: Ali
    ID: 101, Age: 25, First: Mahnaz, Last: Fatma
    ID: 102, Age: 30, First: Zaid, Last: Khan
    ID: 103, Age: 28, First: Sumit, Last: Mittal
  • 相关阅读:
    Socket通信的理解
    wpf listbox 内的内容显示问题,需要设置里面的itemsPresenter
    C#的两个大方向
    QT的安装和配置及helloqt程序的编写时遇到的问题
    华为交换机基础命令
    华为创建VLAN及VLAN间通讯
    powershell查询AD域账号详细信息
    Powershell从EXCEL导入大量用户
    映射网络驱动器
    域策略更新及导出
  • 原文地址:https://www.cnblogs.com/lemon-now/p/5530043.html
Copyright © 2011-2022 走看看