Advertisement
Guest User

Untitled

a guest
May 6th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. package adbms;
  2.  
  3. import java.sql.Clob;
  4. import java.sql.Connection;
  5. import java.sql.Date;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Scanner;
  13.  
  14. import oracle.jdbc.OracleDriver;
  15. import oracle.jdbc.driver.OraclePreparedStatement;
  16. import oracle.jdbc.driver.OracleResultSet;
  17. import oracle.sql.ARRAY;
  18. import oracle.sql.ArrayDescriptor;
  19.  
  20. public class SimpleJdbcApplication {
  21.     public static final String  DRIVER_TYPE     = "thin";
  22.     public static final String  HOST_NAME       = "127.0.0.1";
  23.     public static final int     PORT            = 1521;
  24.     public static final String  DATABASE_SID    = "ralphdb";
  25.     public static final String  USER_NAME       = "scott";
  26.     public static final String  PASSWORD        = "tiger";
  27.     public static final String  URL             = String.format("jdbc:oracle:%s:@%s:%d:%s", DRIVER_TYPE, HOST_NAME, PORT, DATABASE_SID);
  28.  
  29.     private static Connection       connection;
  30.     private static Scanner          read;
  31.     private static PreparedStatement    insertStatement;
  32.     private static PreparedStatement    deleteStatement;
  33.     private static PreparedStatement    updateStatement;
  34.     private static PreparedStatement    displayStatement;
  35.  
  36.     static {
  37.         try {
  38.             read = new Scanner(System.in);
  39.             DriverManager.registerDriver(new OracleDriver());
  40.             connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
  41.             insertStatement = connection.prepareStatement("insert into Coders values( Person(?,?,?) , ? , ?)");
  42.             deleteStatement = connection.prepareStatement("delete from Coders where person_.name=?");
  43.             updateStatement = connection.prepareStatement("update Coders set person_.name=? where person_.name=?");
  44.             displayStatement = connection.prepareStatement("select * from Coders");
  45.         } catch (Exception x) {
  46.             System.out.println(x.getLocalizedMessage());
  47.         }
  48.     }
  49.  
  50.     public static void main(String... args) throws SQLException {
  51.         while (true) {
  52.             System.out.println("Enter your choice.");
  53.             System.out.println("1. Insert");
  54.             System.out.println("2. Delete");
  55.             System.out.println("2. Update");
  56.             System.out.println("4. Display");
  57.             System.out.println("5. Enter a direct query");
  58.             System.out.println("6. Exit");
  59.             switch (read.nextInt()) {
  60.                 case 1:
  61.                     insertCoder();
  62.                     break;
  63.                 case 2:
  64.                     removeCoder();
  65.                     break;
  66.                 case 3:
  67.                     updateCoder();
  68.                     break;
  69.                 case 4:
  70.                     displayAllCoders();
  71.                     break;
  72.                 case 5:
  73.                     processDirectQuerry();
  74.                     break;
  75.                 default:
  76.                     System.exit(0);
  77.             }
  78.         }
  79.     }
  80.  
  81.     private static void processDirectQuerry() throws SQLException {
  82.         System.out.println("Enter the query.");
  83.         String query = read.nextLine();
  84.         connection.createStatement().execute(query);
  85.     }
  86.  
  87.     private static void displayAllCoders() throws SQLException {
  88.         ResultSet resultSet = displayStatement.executeQuery();
  89.         System.out.println("Name\tAddress\tDate of birth\tDescription\tKnown Languages");
  90.         while (resultSet.next()) {
  91.             System.out.println(resultSet.getString("person_.name"));
  92.             System.out.println(resultSet.getString("person_.address"));
  93.             System.out.println(resultSet.getDate("person_.dateOfBirth"));
  94.             Clob description = resultSet.getClob("description");
  95.             System.out.println(description.getSubString(1L, (int) description.length()));
  96.             ARRAY oraArray = ((OracleResultSet) resultSet).getARRAY("knownLanguages_");
  97.             String[] languagesArray = (String[]) oraArray.getArray();
  98.             for (String language : languagesArray) {
  99.                 System.out.print(language + " ");
  100.             }
  101.             System.out.println();
  102.         }
  103.     }
  104.  
  105.     private static void updateCoder() throws SQLException {
  106.         System.out.println("Enter the name of the coder whose name is to be updated.");
  107.         String originalName = read.nextLine();
  108.         System.out.println("ENter the new name.");
  109.         String newName = read.nextLine();
  110.         updateStatement.setString(1, newName);
  111.         updateStatement.setString(2, originalName);
  112.         updateStatement.executeUpdate();
  113.     }
  114.  
  115.     private static void removeCoder() throws SQLException {
  116.         System.out.println("Enter the name of coder to be removed.");
  117.         String name = read.nextLine();
  118.         deleteStatement.setString(1, name);
  119.         deleteStatement.executeUpdate();
  120.     }
  121.  
  122.     private static void insertCoder() throws SQLException {
  123.         System.out.println("Enter the name, address and date of birth.");
  124.         String name = read.nextLine();
  125.         String address = read.nextLine();
  126.         String dateOfBirth = read.nextLine();
  127.         System.out.println("Enter a brief description.");
  128.         String description = read.nextLine();
  129.         List<String> languagesList = new ArrayList<String>();
  130.         System.out.println("Enter names of known languages. (At most 10.) Type 'done' when you're done.");
  131.         String token = null;
  132.         int i = 0;
  133.         while (i < 10 && !(token = read.nextLine()).equalsIgnoreCase("done")) {
  134.             languagesList.add(token);
  135.         }
  136.         insertStatement.setString(1, name);
  137.         insertStatement.setString(2, address);
  138.         insertStatement.setDate(3, Date.valueOf(dateOfBirth));
  139.         insertStatement.setString(4, description);
  140.         String[] languagesArray = (String[]) languagesList.toArray();
  141.         ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("KnownLanguages", connection);
  142.         ARRAY oraArray = new ARRAY(arrayDescriptor, connection, languagesArray);
  143.         ((OraclePreparedStatement) insertStatement).setARRAY(5, oraArray);
  144.         insertStatement.executeUpdate();
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement