1 package oracle;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.Statement;
7
8 //使用JDBC连接Oracle
9
10 public class TestOracle {
11
12 public static void main(String[] args) {
13 // TODO Auto-generated method stub
14
15 try {
16 //1.加载驱动
17 Class.forName("oracle.jdbc.driver.OracleDriver");
18 //2.得到连接
19 Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORACLE12C","sys as SYSDBA","fairy6280");
20
21 Statement sm = (Statement) conn.createStatement();
22 String sql = "select * from student";
23 String sql1 = "select * from (select a1.*,rownum rowIndex from (select * from student order by salary) a1 where rownum <5) where rowIndex > 1";
24 ResultSet rset = sm.executeQuery(sql1);
25 while(rset.next())
26 {
27 System.out.println("姓名:"+ rset.getString(2));
28 }
29
30 //加入事务处理
31 conn.setAutoCommit(false);
32 String sql3="update student set salary=salary -100 where sname=‘Smith‘";
33 String sql4 = "update student set salary = salary +100 where sname=‘Bill‘";
34 sm.executeUpdate(sql3);
35 sm.executeUpdate(sql4);
36 conn.commit();
37
38 //关闭资源
39 rset.close();
40 sm.close();
41 conn.close();
42 } catch (Exception e) {
43 // TODO: handle exception
44 e.printStackTrace();
45 }
46 System.out.println("----------");
47 }
48
49 }