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();

  • 相关阅读:
    luogu 2478 [SDOI2010]城市规划 仙人掌上dp.
    bzoj 3782 上学路线 卢卡斯定理 容斥 中国剩余定理 dp
    bzoj 3790 神奇项链 回文串 manacher|PAM
    4.4 相交弧 容斥 平衡规划 二维数点
    4.4 省选模拟赛 拉格朗日计数 树状数组+容斥.
    4.4 省选模拟赛 修路 斯坦纳树
    带修改线性基
    CF R 630 div2 1332 F Independent Set
    4.3 省选模拟赛 石子游戏 树上博弈
    机器C盘临时区
  • 原文地址:https://www.cnblogs.com/Earl/p/1865846.html
Copyright © 2011-2022 走看看