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

  • 相关阅读:
    Mybatis全局配置文件
    Mybatis简介及入门
    数据库小结(1)
    Java提高——Java的内存回收(2)
    关于引入文件错误
    Java提高——JUC原子类
    swagger
    Java提高——多线程(五)生产消费者问题
    Java提高——多线程(四)等待、唤醒、线程中断、优先级和守护线程
    战略与战术
  • 原文地址:https://www.cnblogs.com/Earl/p/1865846.html
Copyright © 2020-2023  润新知