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
|
import java.sql.*;
class Submit{ Statement stmt; Connection con; String strUrl; String strUserName; String strPassword;
public Submit() { //the DSN for the Db connection strUrl = "jdbc:oracle:thin:@pisang:1521:car"; strUserName = "plestrange"; strPassword = "plcae4"; }
public void Open(){
try{ //Register Driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//Attempt to connect to a driver con = DriverManager.getConnection(strUrl, strUserName, strPassword);
//Create a statement object so we can submit //SQL statements to the driver stmt = con.createStatement(); } catch (SQLException ex){ while (ex != null){ System.out.println ("SQL Exception: " + ex.getMessage() ); ex = ex.getNextException(); } } catch (java.lang.Exception ex){ ex.printStackTrace(); } }
public void Select (String strQuery){ try{ //Submit a query, creating a ResultSet object ResultSet rs = stmt.executeQuery (strQuery);
//Display all columns and rows from the result set printResultSet (rs);
rs.close(); } catch (SQLException ex){ while (ex != null){ System.out.println ("SQL Exception: " + ex.getMessage () ); ex = ex.getNextException (); } } }
public void Close(){ try{ stmt.close(); con.close(); } catch (SQLException ex){ while (ex !=null){ System.out.println ("SQL Exception: " + ex.getMessage () ); ex = ex.getNextException (); } } } private static void printResultSet (ResultSet rs) throws SQLException{ int numCols = rs.getMetaData().getColumnCount(); while (rs.next()){ for (int i=1; i<numCols; i++){ System.out.print(rs.getString(i) + " | "); } System.out.println(); } } }
|