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

  • 相关阅读:
    Fiddler-常用技巧
    Fiddler-工作原理
    C语言-EOF和feof()判断文件结尾的区别
    C语言-一个fopen函数中未使用二进制模式(b)引发的血案
    VIM-不常用或不知道的技巧
    C语言-srand种子详解
    C语言-字符串操作函数
    C语言-Makefile经典教程(掌握这些足够)
    分布拟合——正态/拉普拉斯/对数高斯/瑞利 分布
    曲线拟合——(2)拉普拉斯/瑞利/对数正态 曲线
  • 原文地址:https://www.cnblogs.com/Earl/p/1865846.html
Copyright © 2011-2022 走看看