Advertisement
Guest User

jesse

a guest
Mar 18th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1.  
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. package cecs.pkg323.java.project;
  8.  
  9. import java.sql.*;
  10. import java.util.Scanner;
  11.  
  12. /**
  13. *
  14. * @author sam
  15. */
  16. public class CECS323JavaProject {
  17. // Database credentials
  18. static String USER;
  19. static String PASS;
  20. static String DBNAME;
  21. //This is the specification for the printout that I'm doing:
  22. //each % denotes the start of a new field.
  23. //The - denotes left justification.
  24. //The number indicates how wide to make the field.
  25. //The "s" denotes that it's a string. All of our output in this test are
  26. //strings, but that won't always be the case.
  27. static final String displayFormat="%-5s%-15s%-15s%-15s\n";
  28. // JDBC driver name and database URL
  29. static final String JDBC_DRIVER = "org.apache.derby.jdbc.ClientDriver";
  30. static String DB_URL = "jdbc:derby://localhost:1527/";
  31. // + "testdb;user=";
  32. /**
  33. * Takes the input string and outputs "N/A" if the string is empty or null.
  34. * @param input The string to be mapped.
  35. * @return Either the input string or "N/A" as appropriate.
  36. */
  37. public static String dispNull (String input) {
  38. //because of short circuiting, if it's null, it never checks the length.
  39. if (input == null || input.length() == 0)
  40. return "N/A";
  41. else
  42. return input;
  43. }
  44.  
  45. public static void main(String[] args) {
  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. //STEP 3: Open a connection
  65. System.out.println("Connecting to database...");
  66. conn = DriverManager.getConnection(DB_URL);
  67.  
  68. //STEP 4: Execute a query
  69. System.out.println("Creating statement...");
  70. stmt = conn.createStatement();
  71. String sql;
  72. sql = "SELECT GroupName, BookTitle, PubName, YearPublished, NumberPages FROM Books";
  73. ResultSet rs = stmt.executeQuery(sql);
  74.  
  75. //STEP 5: Extract data from result set
  76. System.out.printf(displayFormat, "Group Name", "Book Title", "Publisher Name", "Year Published", "Number of Pages");
  77. while (rs.next()) {
  78. //Retrieve by column name
  79. String group = rs.getString("GroupName");
  80. String book = rs.getString("BookTitle");
  81. String pub = rs.getString("PubName");
  82. String year = rs.getString("YearPublished");
  83. String pages = rs.getString("NumberPages");
  84.  
  85. //Display values
  86. System.out.printf(displayFormat,
  87. dispNull(group), dispNull(book), dispNull(pub), dispNull(year), dispNull(pages));
  88. }
  89. //STEP 6: Clean-up environment
  90. rs.close();
  91. stmt.close();
  92. conn.close();
  93. } catch (SQLException se) {
  94. //Handle errors for JDBC
  95. se.printStackTrace();
  96. } catch (Exception e) {
  97. //Handle errors for Class.forName
  98. e.printStackTrace();
  99. } finally {
  100. //finally block used to close resources
  101. try {
  102. if (stmt != null) {
  103. stmt.close();
  104. }
  105. } catch (SQLException se2) {
  106. }// nothing we can do
  107. try {
  108. if (conn != null) {
  109. conn.close();
  110. }
  111. } catch (SQLException se) {
  112. se.printStackTrace();
  113. }//end finally try
  114. }//end try
  115. System.out.println("Goodbye!");
  116. }//end main
  117. }//end FirstExample}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement