Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3. import java.lang.*;
  4. import oracle.jdbc.driver.*; //make sure this is in classpath
  5. public class Simple {
  6.   public static void main (String args [])
  7.     throws SQLException, IOException {
  8.     try {
  9.       Class.forName("oracle.jdbc.driver.OracleDriver");
  10.       System.out.println("driver loaded");
  11.     } catch (ClassNotFoundException e) {
  12.         System.out.println ("Could not load the driver");
  13.     }
  14.     String servername = "localhost";
  15.     String portnumber = "1521";
  16.     String sid = "xe";
  17.     String url = "jdbc:oracle:thin:@" + servername + ":" + portnumber + ":" + sid;
  18.     String user, pass;
  19.     user = readEntry("userid  : ");
  20.     pass = readEntry("password: ");
  21.     System.out.println(url);
  22.     DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  23.     System.out.println ("before connection");
  24.  
  25.     Connection conn = DriverManager.getConnection(url, user, pass);
  26.     System.out.println ("after connection");
  27.     Statement stmt = conn.createStatement ();
  28.  
  29.     ResultSet rset = stmt.executeQuery
  30.     ("select clientemail, clientid from alan.client");
  31.     while (rset.next()) {
  32.     System.out.println(rset.getString(1) + " " +
  33.                        rset.getString(2));
  34.     }
  35.     stmt.close();
  36.     conn.close();
  37.   }
  38. //readEntry function -- to read input string
  39. static String readEntry(String prompt) {
  40.        try {
  41.          StringBuffer buffer = new StringBuffer();
  42.          System.out.print(prompt);
  43.          System.out.flush();
  44.          int c = System.in.read();
  45.          while (c != '\n' && c != -1) {
  46.            buffer.append((char)c);
  47.            c = System.in.read();
  48.          }
  49.          return buffer.toString().trim();
  50.       }  catch (IOException e) {
  51.          return "";
  52.          }
  53.   }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement