Advertisement
Guest User

Example.java

a guest
Jun 28th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. package jdbctest;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.SQLException;
  8. import java.sql.SQLSyntaxErrorException;
  9. import java.sql.Statement;
  10. import java.util.Scanner;
  11.  
  12. public class Example {
  13.     static String username, password;
  14.     static String dbDriver = "oracle.jdbc.driver.OracleDriver";
  15.     static String dbConnection = "jdbc:oracle:thin:@//localhost:1521/xe";
  16.  
  17.     public static void main(String[] args) {
  18.         Scanner input = new Scanner(System.in);
  19.         String line;
  20.         Connection c = null;
  21.         Statement stmt = null;
  22.         int loginAttempts = 0;
  23.  
  24.         // Log in loop
  25.         do {
  26.             try {
  27.                 Class.forName(dbDriver);
  28.                 System.out.print("Enter username: ");
  29.                 line = input.nextLine();
  30.                 if (line.contains("/")) {
  31.                     String[] login = line.split("/");
  32.                     if (login.length != 2) {
  33.                         System.out
  34.                                 .println("Unrecognized information, exiting...");
  35.                         System.exit(0);
  36.                     }
  37.  
  38.                     username = login[0].trim();
  39.                     password = login[1].trim();
  40.                 } else {
  41.                     username = line;
  42.                     System.out.print("Enter password: ");
  43.                     password = input.nextLine();
  44.                 }
  45.  
  46.                 c = DriverManager.getConnection(dbConnection, username,
  47.                         password);
  48.                 stmt = c.createStatement();
  49.                 loginAttempts = -1;
  50.             } catch (ClassNotFoundException e) {
  51.                 System.err.println("Unable to connect to database, exiting...");
  52.                 System.exit(-1);
  53.             } catch (SQLException e) {
  54.                 System.out.println("Username and/or password is incorrect");
  55.                 if (++loginAttempts == 1) {
  56.                     System.out.println("Too many failed login attempts, exiting...");
  57.                     System.exit(0);
  58.                 }
  59.             }
  60.         } while (loginAttempts != -1);
  61.  
  62.         // Input loop
  63.         for (;;) {
  64.             try {
  65.                 // Write out the prompt text and wait for input
  66.                 if(c == null) {
  67.                     throw new IllegalStateException("Connection should not be null");
  68.                 }
  69.                 System.out.print(c.getSchema() + ":> ");
  70.                 String tmp = input.nextLine();
  71.  
  72.                 // Check if the user entered "exit"
  73.                 if (tmp.toLowerCase().equals("exit")) {
  74.                     System.out.println("Exiting...");
  75.                     input.close();
  76.                     System.exit(0);
  77.                 }
  78.  
  79.                 String query;
  80.                 // TODO: For some reason, no semi-colon is allowed
  81.                 if (tmp.charAt(tmp.length() - 1) == ';')
  82.                     query = tmp.split(";")[0];
  83.                 else
  84.                     query = tmp;
  85.                 // System.out.println(query);
  86.                 ResultSet rset = stmt.executeQuery(query);
  87.                 ResultSetMetaData rmd = rset.getMetaData();
  88.                 int colCount = rmd.getColumnCount();
  89.  
  90.                 // Column indices start with 1, print column names
  91.                 for (int i = 1; i <= colCount; i++) {
  92.                     System.out.printf("%-20.20s   ", rmd.getColumnName(i));
  93.                 }
  94.                 System.out.println();
  95.  
  96.                 while (rset.next()) {
  97.                     for (int i = 0; i < colCount; i++) {
  98.                         System.out.printf("%-20.20s | ", rset.getString(i + 1));
  99.                     }
  100.                     System.out.println();
  101.                 }
  102.                 System.out.println();
  103.             } catch (SQLSyntaxErrorException e) {
  104.                 System.out.println("Encountered a syntax error:\n"
  105.                         + e.getMessage());
  106.             } catch (SQLException e) {
  107.                 System.err.println("An unexpected error occurred");
  108.                 e.printStackTrace();
  109.                 input.close();
  110.                 System.exit(-1);
  111.             }
  112.         }
  113.  
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement