例子:
 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
 
JDBC-Oracle
 
 
请尊重原创知识,本人非常愿意与大家分享 转载请注明出处:http://www.cnblogs.com/horizonli/p/5020037.html

相关文章:

  • 2021-11-26
  • 2021-12-22
  • 2022-01-21
  • 2021-10-12
  • 2021-07-23
  • 2021-06-16
猜你喜欢
  • 2021-08-17
  • 2022-12-23
  • 2021-07-14
  • 2021-06-22
  • 2021-11-30
相关资源
相似解决方案