Advertisement
Guest User

Untitled

a guest
May 21st, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3.  
  4. public class exercise_2_kyoung27
  5. {
  6.     public static void main(String [] argv) throws Exception
  7.     {
  8.         //scanner object for keyboard input, database/username/password
  9.         Scanner keyboard = new Scanner(System.in);
  10.         System.out.print("database: ");
  11.         String database = keyboard.nextLine();
  12.        
  13.         System.out.print("user: ");
  14.         String user = keyboard.nextLine();
  15.        
  16.         System.out.print("password: ");
  17.         String password = keyboard.nextLine();
  18.        
  19.        
  20.         Connection conn = null;
  21.         try
  22.         {
  23.  
  24.           Class.forName("org.postgresql.Driver");
  25.           String url = "jdbc:postgresql://localhost/"+database;
  26.           conn = DriverManager.getConnection(url +user +password);
  27.  
  28.         }
  29.         catch (ClassNotFoundException e)
  30.         {
  31.           e.printStackTrace();
  32.           System.exit(1);
  33.         }
  34.         catch (SQLException e)
  35.         {
  36.           e.printStackTrace();
  37.           System.exit(2);
  38.         }
  39.         //Now we're connected up lets retrieve a list of employees
  40.  
  41.  
  42.         /*System.out.println("\n****Employees Currently Within the Database*****");
  43.         System.out.println();
  44.         //Print the column headings to the console
  45.        System.out.printf("%-12s %-12s %n", "First Name","Last Name");
  46.         System.out.println("--------------------");
  47.  
  48.         // First we specify our query
  49.         String query = "SELECT fname, lname FROM employee;";
  50.         Statement stmt = null;
  51.         try {
  52.         //Create an sql statement object
  53.         stmt = conn.createStatement();
  54.         //Execute the query
  55.         ResultSet rs = stmt.executeQuery(query);
  56.         //Iterate through the results and print to the console
  57.         while (rs.next()) {
  58.             String fName = rs.getString("fname");
  59.             String lName = rs.getString("lname");
  60.             System.out.printf("%-12s %-12s %n", fName,lName);
  61.         }
  62.         } catch (SQLException e ) {
  63.         System.out.println(e);
  64.         conn.close();
  65.         System.exit(1);
  66.         }
  67.         System.out.println("--------------------");
  68.         System.out.println("\nQuery Executed Successfully...exiting");
  69.         //Close the database connection
  70.         conn.close();*/
  71.         System.out.println("hi");
  72.       }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement