本篇文章主要介绍了"JDBC-Oracle",主要涉及到Exception,连接数据库,JDBC方面的内容,对于Oracle感兴趣的同学可以参考一下:
例子: 1publicclassTestJDBC {2publicstaticvoid main(String[] args)throwsException {...
例子:
1publicclassTestJDBC {
2publicstaticvoid 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中 7while(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,包括了Exception,连接数据库,JDBC方面的内容,希望对Oracle有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_237178.html