import java.net.URL;
import java.sql.*;

class JDBCTest {


    public static void main (String args[]) {
	String url = "jdbc:oracle:thin:@pgrc.ipk-gatersleben.de:1521:pdw";
	//String query = "select * from go_term";//alle GO-Terme + IDs
	String query = "select rpad('-',2*level-1,'-')||name2||'('||acc2||')' from (select t2.is_root, t2.name name1, t2.acc acc1, t3.name name2, t3.acc acc2, term1_id, term2_id from go_term2term t1, go_term t2, go_term t3 where t1.term1_id = t2.id and t1.term2_id = t3.id)  connect by prior term2_id = term1_id start with is_root=1";//Nachbarschaftsrelation, um aus den GO-Termen Bäume zu berechnen

            try {

                
                    Class.forName ("oracle.jdbc.driver.OracleDriver");

                    //DriverManager.setLogStream(System.out);

                    // Attempt to connect to a driver.  Each one
                    // of the registered drivers will be loaded until
                    // one is found that can process this URL

                    Connection con = DriverManager.getConnection (
                            url, "pdw_all", "pdw_all");

                    // If we were unable to connect, an exception
                    // would have been thrown.  So, if we get here,
                    // we are successfully connected to the URL

                    // Check for, and display and warnings generated
                    // by the connect.

                    checkForWarning (con.getWarnings ());

                    // Get the DatabaseMetaData object and display
                    // some information about the connection

                  DatabaseMetaData dma = con.getMetaData ();

                    System.out.println("\nConnected to " + dma.getURL());
                     System.out.println("Driver       " + 
                             dma.getDriverName());
                     System.out.println("Version      " +
                             dma.getDriverVersion());
                     System.out.println("");
		     
		     
		     
                    // Create a Statement object so we can submit
                    // SQL statements to the driver

                    Statement stmt = con.createStatement ();

                    // Submit a query, creating a ResultSet object

                    ResultSet rs = stmt.executeQuery (query);

                    // Display all columns and rows from the result set

                    dispResultSet (rs);

                    // Close the result set

                    rs.close();

                    // Close the statement

                    stmt.close();

                    // Close the connection

                    con.close();
            }
            catch (SQLException ex) {

                    // A SQLException was generated.  Catch it and
                    // display the error information.  Note that there
                    // could be multiple error objects chained
                    // together

            System.out.println ("\n*** SQLException caught ***\n");

            while (ex != null) {
                    System.out.println ("SQLState: " +
                                    ex.getSQLState ());
                    System.out.println ("Message:  " + ex.getMessage ());
                    System.out.println ("Vendor:   " +
                                    ex.getErrorCode ());
                    ex = ex.getNextException ();
                    System.out.println ("");
                    }
            }
            catch (java.lang.Exception ex) {

                    // Got some other type of exception.  Dump it.

                    ex.printStackTrace ();
            }
    }

    //-------------------------------------------------------------------
    // checkForWarning
    // Checks for and displays warnings.  Returns true if a warning
    // existed
    //-------------------------------------------------------------------

    private static boolean checkForWarning (SQLWarning warn)        
                    throws SQLException  {
            boolean rc = false;

            // If a SQLWarning object was given, display the
            // warning messages.  Note that there could be
            // multiple warnings chained together

            if (warn != null) {
                    System.out.println ("\n *** Warning ***\n");
                    rc = true;
                    while (warn != null) {
                            System.out.println ("SQLState: " +
                                    warn.getSQLState ());
                            System.out.println ("Message:  " +
                                    warn.getMessage ());
                            System.out.println ("Vendor:   " +
                                    warn.getErrorCode ());
                            System.out.println ("");
                            warn = warn.getNextWarning ();
                    }
            }
            return rc;
    }

    //-------------------------------------------------------------------
    // dispResultSet
    // Displays all columns and rows in the given result set
    //-------------------------------------------------------------------

    private static void dispResultSet (ResultSet rs)
            throws SQLException
    {
            int i;

            // Get the ResultSetMetaData.  This will be used for
            // the column headings

            ResultSetMetaData rsmd = rs.getMetaData ();

            // Get the number of columns in the result set

	    int numCols = rsmd.getColumnCount ();

            // Display column headings

	    for (i=1; i<=numCols; i++) {
		if (i > 1) System.out.print(",");
		System.out.print(rsmd.getColumnLabel(i));
	    }
	    System.out.println("");
            
            // Display data, fetching until end of the result set

            boolean more = rs.next ();
            while (more) {

                    // Loop through each column, getting the
                    // column data and displaying
		System.out.println("");
		for (i=1; i<=numCols; i++) {
		    if (i > 1) System.out.print(",");
		    System.out.print(rs.getString(rsmd.getColumnName(i)));
		}
		// Fetch the next result set row
		
		more = rs.next ();
            }
	    System.out.println("");
    }

}
