zoukankan      html  css  js  c++  java
  • java jdbc

    数据库连接的一般过程

    1载入JDBC驱动程序(jar包)

    2 定义连接URL,每个数据库都有一个URL

    3 建立连接(connection对象)

    4创建Statement对象

    5 执行查询或更新

    6结果处理

    7关闭连接

    加载驱动程序

       1使用JDBC-ODBC桥驱动程序:Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//java反射

       2使用oracle的jdbc驱动程序: Class.forName("oracle.jdbc.driver.OracleDriver");//oracle提供的驱动

    建立JDBC连接

       DriverManager类的getConnection():  public static Connection getConnection(String url,String           user,String password) throws SQLException

      oracle   JDBC Thin 驱动程序  Connection con = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:db","用户名","密码");

           JDBC OCI驱动程序   Connection con = DriverManager.getConnection("jdbc:oracle:oci8:@db","用户名","密码");

      关闭Connection:  close();

    Statement对象

      Statement对象用于将SQL语句发送到数据库中

      三种Statement对象(statement ,preparedStatement(预编译的SQL语句,继承Statement),CallableStatement(继承PrepareStatement)

      由Connection的createStatement()创建:public Statement createStatement() throws SQLException

      两种方法:public ResultSet executeQuery(String sql) throws SQLException

           public int executeUpdate(String sql) throws SQLException

      关闭:close();

    处理返回结果

      ResultSet:  int  getInt(String columnName) throws SQLException ;  getInt(int columnIndex)

            String  getString(String columnName)   getString(int columnIndex)

        移动到下一行:boolean next() throws SQLException

      关闭:close();

  • 相关阅读:
    poli-java开源BI软件
    Spring Boot 教程
    微信小程序支持windows PC版了
    Java-JDK-windows和linux版-百度云下载
    ssh -i 密钥文件无法登陆问题
    锐捷交换机18010-X端口假死现象
    zabbix4.4安装
    yum只下载不安装
    openstack迁移计算节点所有云主机
    ceph SSD HDD分离与openstack调用
  • 原文地址:https://www.cnblogs.com/Earl/p/1865846.html
Copyright © 2011-2022 走看看