首页 > 代码库 > Java 使用JDBC连接Oracle
Java 使用JDBC连接Oracle
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 stub14 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 exception44 e.printStackTrace();45 }46 System.out.println("----------");47 }48 49 }
注:要在项目中添加JDBC Driver -> ojdbc7.jar
Java 使用JDBC连接Oracle
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。