Advertisement
Guest User

Untitled

a guest
Oct 15th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package jdbc_project;
  7. import java.sql.*;
  8. import java.util.Scanner;
  9. /**
  10. *
  11. * @author djbla
  12. */
  13. public class JDBC_Project
  14. {
  15. // Database credentials
  16. static String USER;
  17. static String PASS;
  18. static String DBNAME;
  19. //This is the specification for the printout that I'm doing:
  20. //each % denotes the start of a new field.
  21. //The - denotes left justification.
  22. //The number indicates how wide to make the field.
  23. //The "s" denotes that it's a string. All of our output in this test are
  24. //strings, but that won't always be the case.
  25. static final String displayFormat="%-5s%-15s%-15s%-15s\n";
  26. // JDBC driver name and database URL
  27. static final String JDBC_DRIVER = "org.apache.derby.jdbc.ClientDriver";
  28. static String DB_URL = "jdbc:derby://localhost:1527/";
  29. // + "testdb;user=";
  30. /**
  31. * Takes the input string and outputs "N/A" if the string is empty or null.
  32. * @param input The string to be mapped.
  33. * @return Either the input string or "N/A" as appropriate.
  34. */
  35. public static String dispNull (String input)
  36. {
  37. //because of short circuiting, if it's null, it never checks the length.
  38. if (input == null || input.length() == 0)
  39. return "N/A";
  40. else
  41. return input;
  42. }
  43.  
  44. public static void main(String[] args)
  45. {
  46. //Prompt the user for the database name, and the credentials.
  47. //If your database has no credentials, you can update this code to
  48. //remove that from the connection string.
  49. Scanner in = new Scanner(System.in);
  50. System.out.print("Name of the database (not the user account): ");
  51. DBNAME = in.nextLine();
  52. System.out.print("Database user name: ");
  53. USER = in.nextLine();
  54. System.out.print("Database password: ");
  55. PASS = in.nextLine();
  56. //Constructing the database URL connection string
  57. DB_URL = DB_URL + DBNAME + ";user="+ USER + ";password=" + PASS;
  58. Connection conn = null; //initialize the connection
  59. Statement stmt = null; //initialize the statement that we're using
  60. try {
  61. //STEP 2: Register JDBC driver
  62. Class.forName("org.apache.derby.jdbc.ClientDriver");
  63.  
  64.  
  65.  
  66. Scanner input=new Scanner(System.in);
  67. int option=input.nextInt();
  68. //string value=input.nextString();
  69. //STEP 3: Open a connection
  70. System.out.println("Connecting to database...");
  71. conn = DriverManager.getConnection(DB_URL);
  72. System.out.println("Enter a number to pick an option.");
  73. System.out.println("1. List all writing groups");
  74. System.out.println("2. List all publishers");
  75. System.out.println("3. List all book titles");
  76. System.out.println("4. List data for a group specified by user");
  77. System.out.println("5. List data for a publisher specified by user");
  78. System.out.println("6. List data for a book specified by user");
  79. System.out.println("7. Insert a new book");
  80. System.out.println("8. Insert a new publisher and update all book "
  81. + "published by one publisher to be the new publisher");
  82. System.out.println("9.Remove a book specified by user.");
  83. System.out.println("10. Exit");
  84.  
  85. //STEP 4: Execute a query
  86. //System.out.println("Creating statement...");
  87. // stmt = conn.createStatement();
  88. ResultSet rs = "";
  89. String sql,sql2,sql3,sql4,sql5,sql6,sql7,sql8,sql9;
  90. switch(option)
  91. {
  92. case 1:
  93. sql = "SELECT * FROM WritingGroups";
  94. rs = stmt.executeQuery(sql);
  95. break;
  96. case 2:
  97. sql2 = "SELECT * FROM Publishers";
  98. rs = stmt.executeQuery(sql2);
  99. break;
  100. case 3:
  101. sql3 = "SELECT * FROM Books";
  102. break;
  103. case 4:
  104. PreparedStatement stmt4 = conn.prepareStatement("SELECT * FROM WritingGroups WHERE GroupName = ?");
  105. sql4 = input.next();
  106. break;
  107. case 5:
  108. PreparedStatement stmt5 = conn.prepareStatement("SELECT * FROM Publishers WHERE PublisherName = ?");
  109. sql5 =input.next();
  110. case 6:
  111. PreparedStatement stmt6 = conn.prepareStatement("SELECT * FROM Books WHERE BookTitle = ?");
  112. sql6 = input.next();
  113. case 7:
  114. PreparedStatement stmt7 = conn.prepareStatement("INSERT INTO Books(BookTitle, YearPublished, NumberPages, GroupName VALUES BookTitle = (?,?,?,?)");
  115. sql7 = input.next();
  116. case 8:
  117. PreparedStatement stmt8 = conn.prepareStatement("");
  118. sql8 = "";
  119. case 9:
  120. System.out.println("Enter book title for deletion");
  121. PreparedStatement stmt9 = conn.prepareStatement("DELETE FROM Books WHERE BookTitle=?");
  122. sql9 = input.next();
  123. case 10:
  124. default:
  125.  
  126.  
  127.  
  128. //STEP 5: Extract data from result set
  129. System.out.printf(displayFormat, "ID", "First Name", "Last Name", "Phone #");
  130. while (rs.next())
  131. {
  132. //Retrieve by column name
  133. String id = rs.getString("au_id");
  134. String phone = rs.getString("phone");
  135. String first = rs.getString("au_fname");
  136. String last = rs.getString("au_lname");
  137.  
  138. //Display values
  139. System.out.printf(displayFormat,
  140. dispNull(id), dispNull(first), dispNull(last), dispNull(phone));
  141. }
  142. //STEP 6: Clean-up environment
  143. rs.close();
  144. stmt.close();
  145. conn.close();
  146. } catch (SQLException se) {
  147. //Handle errors for JDBC
  148. se.printStackTrace();
  149. } catch (Exception e) {
  150. //Handle errors for Class.forName
  151. e.printStackTrace();
  152. } finally {
  153. //finally block used to close resources
  154. try {
  155. if (stmt != null) {
  156. stmt.close();
  157. }
  158. } catch (SQLException se2) {
  159. }// nothing we can do
  160. try {
  161. if (conn != null) {
  162. conn.close();
  163. }
  164. } catch (SQLException se) {
  165. se.printStackTrace();
  166. }//end finally try
  167. }//end try
  168. System.out.println("Goodbye!");
  169. }//end main
  170.  
  171. }//end project
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement