zoukankan      html  css  js  c++  java
  • JDBC-Oracle

    例子:
     1 publicclassTestJdbc {
     2       public static void main(String[] args)throwsException {           //程序入口,并抛出异常
     3             Class.forName("oracle.jdbc.driver.OracleDriver");    //使用类装载器创建一个OracleDriver对象并自动在DriverManager中进行注册
     4             Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:horizon","scott","tiger"); //创建一个接口用于连接数据库
     5             Statement stmt = conn.createStatement(); //创建一个语句对象装在sql语句
     6             ResultSet rs = stmt.executeQuery("select * from dept");  //将返回的结果装在返回集rs中
     7             while(rs.next()) {
     8                   System.out.println(rs.getString("deptno"));   //当返回集的游标向下移动的时候打印字段内容
     9             }
    10             rs.close();
    11             stmt.close();
    12             conn.close();       //关闭接口
    13       }
    14 }
    运用JDBC连接数据库的方法:
    1.load the Driver
       1.Class.forName() | Class.forName().newInstance()| new DriverName()
       2.实例化时自动向DriverManager注册,不需显式调用DriverManager.registerDriver方法
     
    2.Connect to the DataBase
       1.DriverManager.getConnection()
     
    3.Execute the SQL
       1.Connection.CreateStatement()
       2.Statement.executeQuery()
       3.Statement.executeUpdate()
     
    4.Retrieve the result data
       1.循环取得结果while(rs.next())
     
    5.show the result data
       1.将数据库中的各种类型转换为java中的类型(getXXX)方法
     
    6.Close
       1.close the resultset / close the statement /close the connection
     
     
     
    请尊重原创知识,本人非常愿意与大家分享 转载请注明出处:http://www.cnblogs.com/horizonli/p/5020037.html
  • 相关阅读:
    POJ 1062 坑爹的聘礼(枚举等级差选择性找边)
    c++ string函数详细返回值及用法!
    POJ 2240 利率变权值 Floyd变乘法
    POJ 1797 最大运载量
    API code
    编程题目的讨论
    C语言位运算符:与、或、异或、取反、左移和右移
    &与&& C语言
    反思
    CreateWindow的出错解决
  • 原文地址:https://www.cnblogs.com/horizonli/p/5020037.html
Copyright © 2011-2022 走看看