Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4. public class DatabaseProgram
  5. {  
  6.     static final String USER = "STU21";
  7.     static final String PASS = "teampontto";
  8.        
  9.     public static void main(String[] args)
  10.     {
  11.         Scanner reader = new Scanner(System.in);
  12.         Connection conn = null;
  13.         Statement stmt = null;
  14.        
  15.         try
  16.         {
  17.             Class.forName("oracle.jdbc.driver.OracleDriver");
  18.             System.out.println("JDBC Driver found!");
  19.         }
  20.         catch(ClassNotFoundException e)
  21.         {
  22.             System.out.println("JDBC Driver not found!");
  23.             e.printStackTrace();
  24.             return;
  25.         }
  26.        
  27.         try
  28.         {
  29.             conn = DriverManager.getConnection("jdbc:oracle:thin:@toldb.oulu.fi:1521:toldb11", USER, PASS);
  30.         }
  31.         catch(SQLException e)
  32.         {
  33.             System.out.println("Database connection failed!");
  34.             e.printStackTrace();
  35.             return;
  36.         }
  37.        
  38.         if (conn != null)
  39.         {
  40.             System.out.println("Database connection successful!");
  41.             System.out.println("Available commands are -> 1: Show Items / 2: Show Customers:");
  42.             int choice = reader.nextInt();
  43.             System.out.println();
  44.            
  45.             if (choice == 1)
  46.             {
  47.                 printItems(conn, stmt);
  48.             }
  49.             else if (choice == 2)
  50.             {
  51.                 printCustomers(conn, stmt);
  52.             }
  53.             else
  54.             {
  55.                 System.out.println("Invalid input!");
  56.             }
  57.         }
  58.         else
  59.         {
  60.             System.out.println("Failed to make connection!");
  61.         }
  62.  
  63.     }
  64.    
  65.     public static void printItems(Connection conn, Statement stmt)
  66.     {
  67.         try
  68.         {      
  69.             stmt = conn.createStatement();
  70.             String sql;
  71.             sql = "SELECT title, status FROM T_item";
  72.             ResultSet rs = stmt.executeQuery(sql);
  73.        
  74.             while(rs.next())
  75.             {
  76.                 String title = rs.getString("title");
  77.                 String status = rs.getString("status");
  78.                 System.out.println("Title: " + title);
  79.                 System.out.println("Status: " + status);
  80.                 System.out.println("--------------------");
  81.             }
  82.        
  83.             rs.close();
  84.             stmt.close();
  85.             conn.close();
  86.         }
  87.         catch(SQLException se)
  88.         {
  89.             se.printStackTrace();
  90.         }
  91.     }
  92.    
  93.     public static void printCustomers(Connection conn, Statement stmt)
  94.     {
  95.         try
  96.         {      
  97.             stmt = conn.createStatement();
  98.             String sql;
  99.             sql = "SELECT name_fname, name_lname, ssn FROM T_customer";
  100.             ResultSet rs = stmt.executeQuery(sql);
  101.        
  102.             while(rs.next())
  103.             {
  104.                 String firstname = rs.getString("name_fname");
  105.                 String lastname = rs.getString("name_lname");
  106.                 String ssn = rs.getString("ssn");
  107.                 System.out.println("Name: " + firstname + " " + lastname);
  108.                 System.out.println("SSN: " + ssn);
  109.                 System.out.println("--------------------");
  110.             }
  111.        
  112.             rs.close();
  113.             stmt.close();
  114.             conn.close();
  115.         }
  116.         catch(SQLException se)
  117.         {
  118.             se.printStackTrace();
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement