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

  • 相关阅读:
    我的第一个网络爬虫
    python中os模块的walk函数
    <Think Python>中统计文献单词的处理代码
    <Think Python>中斐波那契使用memo实现的章节
    Python中bisect的使用
    vim中使用系统粘贴板
    linux下的重命名
    HTML Dog 初级教程中关于 forms 的翻译
    unittest:1 用例编写
    python UI自动化实战记录十一: 总结
  • 原文地址:https://www.cnblogs.com/Earl/p/1865846.html
Copyright © 2011-2022 走看看