java operation Oracle database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | /* @author:luowen @time:2013-11-22 @desc:java operation oracle */ package com.luowen.OracleTest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class TestOrl { public static void main(String[] args){ // TODO Auto-generated method stub Connection ct = null ; PreparedStatement ps = null ; ResultSet rs = null ; try { //load driver Class.forName( "oracle.jdbc.driver.OracleDriver" ); /* get connection * jdbc:oracle:thin:@ip:port * user * password */ ct = DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:orcl" , "scott" , "tiger" ); //create prepareStatement ps = ct.prepareStatement( "select count(*) cnt from emp" ); //executeQuery sql return resultSet rs = ps.executeQuery(); while (rs.next()) { //print result System.out.println(rs.getString( "cnt" )); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { doException(ct, ps, rs); } } private static void doException(Connection ct, PreparedStatement ps, ResultSet rs) { if (rs != null ) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } rs = null ; } if (ps != null ){ try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } ps = null ; } if (ct != null ) { try { ct.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } ct = null ; } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步