zoukankan      html  css  js  c++  java
  • java operation Oracle database

    /*
      @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;
    		}
    	}
    
    }
      
    

      

  • 相关阅读:
    Java实现 LeetCode 136 只出现一次的数字
    Java实现 LeetCode 136 只出现一次的数字
    Java实现 LeetCode 136 只出现一次的数字
    Java实现 LeetCode 135 分发糖果
    Java实现 LeetCode 135 分发糖果
    Java实现 LeetCode 135 分发糖果
    Java实现 LeetCode 134 加油站
    Java实现 LeetCode 134 加油站
    Java实现 LeetCode 134 加油站
    Java实现 LeetCode 133 克隆图
  • 原文地址:https://www.cnblogs.com/luowen/p/3437378.html
Copyright © 2011-2022 走看看