Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jdbctest;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.SQLSyntaxErrorException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class Example {
- static String username, password;
- static String dbDriver = "oracle.jdbc.driver.OracleDriver";
- static String dbConnection = "jdbc:oracle:thin:@//localhost:1521/xe";
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String line;
- Connection c = null;
- Statement stmt = null;
- int loginAttempts = 0;
- // Log in loop
- do {
- try {
- Class.forName(dbDriver);
- System.out.print("Enter username: ");
- line = input.nextLine();
- if (line.contains("/")) {
- String[] login = line.split("/");
- if (login.length != 2) {
- System.out
- .println("Unrecognized information, exiting...");
- System.exit(0);
- }
- username = login[0].trim();
- password = login[1].trim();
- } else {
- username = line;
- System.out.print("Enter password: ");
- password = input.nextLine();
- }
- c = DriverManager.getConnection(dbConnection, username,
- password);
- stmt = c.createStatement();
- loginAttempts = -1;
- } catch (ClassNotFoundException e) {
- System.err.println("Unable to connect to database, exiting...");
- System.exit(-1);
- } catch (SQLException e) {
- System.out.println("Username and/or password is incorrect");
- if (++loginAttempts == 1) {
- System.out.println("Too many failed login attempts, exiting...");
- System.exit(0);
- }
- }
- } while (loginAttempts != -1);
- // Input loop
- for (;;) {
- try {
- // Write out the prompt text and wait for input
- if(c == null) {
- throw new IllegalStateException("Connection should not be null");
- }
- System.out.print(c.getSchema() + ":> ");
- String tmp = input.nextLine();
- // Check if the user entered "exit"
- if (tmp.toLowerCase().equals("exit")) {
- System.out.println("Exiting...");
- input.close();
- System.exit(0);
- }
- String query;
- // TODO: For some reason, no semi-colon is allowed
- if (tmp.charAt(tmp.length() - 1) == ';')
- query = tmp.split(";")[0];
- else
- query = tmp;
- // System.out.println(query);
- ResultSet rset = stmt.executeQuery(query);
- ResultSetMetaData rmd = rset.getMetaData();
- int colCount = rmd.getColumnCount();
- // Column indices start with 1, print column names
- for (int i = 1; i <= colCount; i++) {
- System.out.printf("%-20.20s ", rmd.getColumnName(i));
- }
- System.out.println();
- while (rset.next()) {
- for (int i = 0; i < colCount; i++) {
- System.out.printf("%-20.20s | ", rset.getString(i + 1));
- }
- System.out.println();
- }
- System.out.println();
- } catch (SQLSyntaxErrorException e) {
- System.out.println("Encountered a syntax error:\n"
- + e.getMessage());
- } catch (SQLException e) {
- System.err.println("An unexpected error occurred");
- e.printStackTrace();
- input.close();
- System.exit(-1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement