Connection

Statement

ResultSet

DataSource

001 /**
002 * ConnOracle 使用Oracle提供类库
003 */
004 package com.testjdbc.jdbc;
005
006 import java.sql.Connection;
007 import java.sql.DriverManager;
008 import java.sql.ResultSet;
009 import java.sql.ResultSetMetaData;
010 import java.sql.SQLException;
011 import java.sql.Statement;
012
013 /**
014 * create date: 2011-3-26 上午11:40:55
015 *
016 * @author Tao
017 *
018 */
019 public class ConnOracle {
020    
021     private Connection conn;
022     private Statement stmt;
023    
024     private String driver = “oracle.jdbc.OracleDriver”;
025     private String url = “jdbc:oracle:thin:@127.0.0.1:1521:orcl”;
026
027     /**
028      *
029      */
030     public ConnOracle() {
031         // TODO Auto-generated constructor stub
032         try {
033             Class.forName(driver);
034             conn = DriverManager.getConnection(url,”ty”,”ty”);
035             conn.setAutoCommit(false);
036             stmt = conn.createStatement();
037         } catch (ClassNotFoundException e) {
038             // TODO Auto-generated catch block
039             e.printStackTrace();
040         } catch (SQLException e) {
041             // TODO Auto-generated catch block
042             e.printStackTrace();
043         }
044     }
045    
046     public void getAll(){
047         try {
048             ResultSet rs = stmt.executeQuery(“select * from scott.emp”);
049             while(rs.next()){
050                 System.out.println(rs.getString(“ename”)+”   “+rs.getDouble(“sal”));
051             }
052             ResultSetMetaData rsmd = rs.getMetaData();
053             System.out.println(rsmd.getColumnClassName(2));
054             System.out.println(rsmd.getColumnName(2));
055             System.out.println(rsmd.getColumnCount());
056            
057             rs.close();
058         } catch (SQLException e) {
059             // TODO Auto-generated catch block
060             e.printStackTrace();
061         }
062     }
063    
064     public void insertData(int empno,String ename,double sal,int deptno){
065         String sql = “insert into emp(empno,ename,sal,deptno,hiredate) “ +
066                 “values (“+empno+”,’“+ename+”’,”+sal+”,”+deptno+”,sysdate)”;
067         System.out.println(sql);
068         try {
069             int i = stmt.executeUpdate(sql);
070             if(i != 0){
071                 conn.commit();
072                 System.out.println(“数据添加成功”);
073             }else{
074                 conn.rollback();
075             }
076         } catch (SQLException e) {
077             // TODO Auto-generated catch block
078             try {
079                 conn.rollback();
080             } catch (SQLException e1) {
081                 // TODO Auto-generated catch block
082                 e1.printStackTrace();
083             }
084             e.printStackTrace();
085         }
086     }
087    
088     /**
089      * 资源释放
090      */
091     public void close(){
092         if(conn != null){
093             try {
094                 conn.close();
095             } catch (SQLException e) {
096                 // TODO Auto-generated catch block
097                 e.printStackTrace();
098             }
099         }   
100     }
101
102     /**
103      * @param args
104      */
105     public static void main(String[] args) {
106         // TODO Auto-generated method stub
107         ConnOracle co = new ConnOracle();
108         co.getAll();
109         //co.insertData(102, “tony”, 2000, 30);
110         co.close();
111     }
112
113 }