Pacific-Design.com

    
Home Index

1. Java

2. Oracle

Java / Oracle /

Oracle

import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;

public class Oracle1 {

    /*------------------------------------------------------------------------*/
    public static void main(String[] args) throws SQLException {
        OracleConnection conn = myConnection();  
        myQuery(conn);
        conn.close();
    }

    /*------------------------------------------------------------------------*/    
    private static OracleConnection myConnection() throws SQLException {
        
        OracleDataSource ods = new OracleDataSource();
        ods.setUser("USER");
        ods.setPassword("PASSWORD");
        ods.setURL("jdbc:oracle:thin:@pacific-design:1521:REGISTRATION");
        OracleConnection conn = (OracleConnection) (ods.getConnection());        
        return conn;
    }
    
    /*------------------------------------------------------------------------*/
    public static void myQuery(oracle.jdbc.OracleConnection conn) 
                               throws SQLException 
    {
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM STUDENT");

        ResultSetMetaData rsmd = rs.getMetaData();
        int columns = rsmd.getColumnCount();

        while (rs.next()) {
            for(int i=1; i<=columns; i++) {
                System.out.print(rs.getString(i) + " ");
            }
            System.out.println();
        }
        rs.close();
        st.close();
    }
    /*------------------------------------------------------------------------*/
}