Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.91 KB | None | 0 0
  1.  
  2.  
  3. import java.sql.*;
  4. import java.util.*;
  5.  
  6. /**
  7. * @author Kyle Nakano & Connor Beckett-Lemus
  8. */
  9. public class JDBCProject {
  10. static String USER;
  11. static String PASS;
  12. static String DBNAME;
  13. static final String JDBC_DRIVER = "org.apache.derby.jdbc.ClientDriver";
  14. static String DB_URL = "jdbc:derby://localhost:1527/";
  15. /**
  16. * Takes the input string and outputs "N/A" if the string is empty or null.
  17. * @param input The string to be mapped.
  18. * @return Either the input string or "N/A" as appropriate.
  19. */
  20. public static String dispNull (String input) {
  21. //because of short circuiting, if it's null, it never checks the length.
  22. if (input == null || input.length() == 0)
  23. return "N/A";
  24. else
  25. return input;
  26. }
  27.  
  28. public static void main(String[] args) {
  29. Scanner in = new Scanner(System.in);
  30. DB_URL = DB_URL + "jdbc_project" + ";user="+ "ck" + ";password=" + "ck";
  31. Connection conn = null; //initialize the connection
  32. Statement stmt = null; //initialize the statement that we're using
  33. PreparedStatement pstmt = null;
  34. try {
  35. //STEP 2: Register JDBC driver
  36. Class.forName("org.apache.derby.jdbc.ClientDriver");
  37.  
  38. //STEP 3: Open a connection
  39. System.out.println("Connecting to database...");
  40. conn = DriverManager.getConnection(DB_URL);
  41.  
  42. //STEP 4: Execute a query
  43. System.out.println("Creating statement...");
  44. stmt = conn.createStatement();
  45. String sql;
  46. int userChoice;
  47.  
  48. do {
  49. Scanner menu = new Scanner(System.in);
  50. System.out.println("1. List writing groups\n"
  51. + "2. List data for a group\n"
  52. + "3. List publishers\n"
  53. + "4. List data for publisher\n"
  54. + "5. List book titles\n"
  55. + "6. List data for a book\n"
  56. + "7. Insert new book\n"
  57. + "8. Insert a new publisher\n"
  58. + "9. Remove a book\n"
  59. + "0. Exit\n");
  60. System.out.printf("Select Output: ");
  61. userChoice = menu.nextInt();
  62. // INPUT VALIDATION HERE
  63.  
  64. if ( userChoice == 1 ) {
  65. sql = "SELECT GroupName FROM WritingGroup";
  66. ResultSet rs = stmt.executeQuery(sql);
  67. System.out.println("Group Names:");
  68. while (rs.next()) {
  69. String gname = rs.getString("GroupName");
  70. System.out.println(dispNull(gname));
  71. }
  72. rs.close();
  73. }
  74.  
  75. else if ( userChoice == 2 ) {
  76. sql = "SELECT GroupName FROM WritingGroup";
  77. ResultSet rs = stmt.executeQuery(sql);
  78. while (rs.next()) {
  79. String gname = rs.getString("GroupName");
  80. System.out.println(dispNull(gname));
  81. }
  82. System.out.println("Type group name: ");
  83. String sqlGroup = "SELECT * FROM WritingGroup WHERE GroupName = ?";
  84. String userGroup = in.nextLine();
  85. try {
  86. pstmt = conn.prepareStatement(sqlGroup);
  87. pstmt.setString(1, userGroup);
  88. ResultSet rsNew = pstmt.executeQuery();
  89. rsNew.next();
  90. String gname = rsNew.getString("GroupName");
  91. String hwriter = rsNew.getString("HeadWriter");
  92. String year = rsNew.getString("YearFormed");
  93. String subject = rsNew.getString("Subject");
  94. System.out.println("Group Name: " + gname + "\n"
  95. + "Head Writer: " + hwriter + "\n"
  96. + "Year Formed: " + year + "\n"
  97. + "Subject: " + subject + "\n");
  98. rsNew.close();
  99. } catch (SQLException e) {
  100. e.printStackTrace();
  101. }
  102. rs.close();
  103. }
  104.  
  105. else if ( userChoice == 3 ) {
  106. sql = "SELECT PublisherName FROM Publisher";
  107. ResultSet rs = stmt.executeQuery(sql);
  108. System.out.println("Publishers: ");
  109. while (rs.next()) {
  110. String pname = rs.getString("PublisherName");
  111. System.out.println(dispNull(pname));
  112. }
  113. rs.close();
  114. }
  115.  
  116. else if ( userChoice == 4 ) {
  117. sql = "SELECT PublisherName FROM Publisher";
  118. ResultSet rs = stmt.executeQuery(sql);
  119. while (rs.next()) {
  120. String gname = rs.getString("PublisherName");
  121. System.out.println(dispNull(gname));
  122. }
  123. System.out.println("Type publisher name: ");
  124. String sqlGroup = "SELECT * FROM Publisher WHERE PublisherName = ?";
  125. String userGroup = in.nextLine();
  126. try {
  127. pstmt = conn.prepareStatement(sqlGroup);
  128. pstmt.setString(1, userGroup);
  129. ResultSet rsNew = pstmt.executeQuery();
  130. rsNew.next();
  131. String pname = rsNew.getString("PublisherName");
  132. String padd = rsNew.getString("PublisherAddress");
  133. String pphone = rsNew.getString("PublisherPhone");
  134. String pemail = rsNew.getString("PublishEmail");
  135. System.out.println("Publisher Name: " + pname + "\n"
  136. + "Publisher Address: " + padd + "\n"
  137. + "Publisher Phone: " + pphone + "\n"
  138. + "Publisher Email: " + pemail + "\n");
  139. rsNew.close();
  140. } catch (SQLException e) {
  141. e.printStackTrace();
  142. }
  143. rs.close();
  144. }
  145.  
  146. else if ( userChoice == 5 ) {
  147. sql = "SELECT BookTitle FROM Book";
  148. ResultSet rs = stmt.executeQuery(sql);
  149. System.out.println("Book Titles: ");
  150. while (rs.next()) {
  151. String bname = rs.getString("BookTitle");
  152. System.out.println(dispNull(bname));
  153. }
  154. rs.close();
  155. }
  156.  
  157. else if ( userChoice == 6 ) {
  158. sql = "SELECT BookTitle FROM Book";
  159. ResultSet rs = stmt.executeQuery(sql);
  160. while (rs.next()) {
  161. String gname = rs.getString("BookTitle");
  162. System.out.println(dispNull(gname));
  163. }
  164. System.out.println("Type book title: ");
  165. String sqlGroup = "SELECT * FROM Book WHERE BookTitle = ?";
  166. String userGroup = in.nextLine();
  167. try {
  168. pstmt = conn.prepareStatement(sqlGroup);
  169. pstmt.setString(1, userGroup);
  170. ResultSet rsNew = pstmt.executeQuery();
  171. rsNew.next();
  172. String btitle = rsNew.getString("BookTitle");
  173. String gname = rsNew.getString("GroupName");
  174. String pname = rsNew.getString("PublisherName");
  175. String yearpub = rsNew.getString("YearPublished");
  176. String numpage = rsNew.getString("NumberPages");
  177. System.out.println("Book Title: " + btitle + "\n"
  178. + "Group Name: " + gname + "\n"
  179. + "Publisher Name: " + pname + "\n"
  180. + "Year Published: " + yearpub + "\n"
  181. + "Number of pages: " + numpage + "\n");
  182. rsNew.close();
  183. } catch (SQLException e) {
  184. e.printStackTrace();
  185. }
  186. rs.close();
  187. }
  188.  
  189. else if ( userChoice == 7 ) {
  190. System.out.println("Creating new book entry..");
  191.  
  192. String sqlGroup = "INSERT INTO Book VALUES(?,?,?,?,?)";
  193.  
  194. System.out.printf("Book title: ");
  195. String bookTitle = in.nextLine();
  196. System.out.printf("Group Name: ");
  197. String gName = in.nextLine();
  198. System.out.printf("Publisher Name: ");
  199. String pName = in.nextLine();
  200. System.out.printf("Year Published: ");
  201. String year = in.nextLine();
  202. System.out.printf("Number of Pages: ");
  203. String pages = in.nextLine();
  204. try {
  205. pstmt = conn.prepareStatement(sqlGroup);
  206. pstmt.setString(1, bookTitle);
  207. pstmt.setString(2, year);
  208. pstmt.setString(3, pages);
  209. pstmt.setString(4, gName);
  210. pstmt.setString(5, pName);
  211. pstmt.executeUpdate();
  212. System.out.println("Entry added.");
  213. } catch (SQLException e) {
  214. e.printStackTrace();
  215. }
  216. }
  217.  
  218. else if ( userChoice == 8 ) {
  219. System.out.println("Creating new publisher entry..");
  220.  
  221. String sqlGroup = "INSERT INTO Publisher VALUES(?,?,?,?)";
  222.  
  223. System.out.printf("Publisher Name: ");
  224. String pName = in.nextLine();
  225. System.out.printf("Publisher Address: ");
  226. String pAdd = in.nextLine();
  227. System.out.printf("Publisher Phone: ");
  228. String pPhone = in.nextLine();
  229. System.out.printf("Publisher Email: ");
  230. String pEmail = in.nextLine();
  231. try {
  232. pstmt = conn.prepareStatement(sqlGroup);
  233. pstmt.setString(1, pName);
  234. pstmt.setString(2, pAdd);
  235. pstmt.setString(3, pPhone);
  236. pstmt.setString(4, pEmail);
  237. pstmt.executeUpdate();
  238. System.out.println("Entry added.");
  239. } catch (SQLException e) {
  240. e.printStackTrace();
  241. }
  242.  
  243. System.out.printf("Would you like new entry to replace an old entry? Y to replace: ");
  244. String replace = in.nextLine();
  245. if ( replace.equalsIgnoreCase("y")) {
  246. sql = "SELECT PublisherName FROM Publisher";
  247. ResultSet rs = stmt.executeQuery(sql);
  248.  
  249.  
  250. while (rs.next()) {
  251. String pname = rs.getString("PublisherName");
  252. System.out.println(dispNull(pname));
  253. }
  254.  
  255. System.out.printf("Type publisher name to replace: ");
  256. String oldPub = in.nextLine();
  257. String updatePub = "UPDATE Book SET PublisherName = ? WHERE PublisherName = ?";
  258. try {
  259. pstmt = conn.prepareStatement(updatePub);
  260. pstmt.setString(1, pName);
  261. pstmt.setString(2, oldPub);
  262. pstmt.executeUpdate();
  263. System.out.println("Publisher Updated.");
  264. } catch (SQLException e) {
  265. e.printStackTrace();
  266. }
  267.  
  268. rs.close();
  269. }
  270. }
  271.  
  272. else if ( userChoice == 9 ) {
  273. sql = "SELECT BookTitle,GroupName FROM Book";
  274. ResultSet rs = stmt.executeQuery(sql);
  275.  
  276. while (rs.next()) {
  277. String bname = rs.getString("BookTitle");
  278. String gname = rs.getString("GroupName");
  279. System.out.println(dispNull(bname) + " | " + dispNull(gname));
  280. }
  281.  
  282. System.out.printf("Enter book title to delete: ");
  283. String userBook = in.nextLine();
  284. System.out.printf("Enter group name: ");
  285. String userGName = in.nextLine();
  286.  
  287. String delBook = "DELETE FROM Book WHERE BookTitle = ? AND GroupName = ?";
  288. try {
  289. pstmt = conn.prepareStatement(delBook);
  290. pstmt.setString(1, userBook);
  291. pstmt.setString(2, userGName);
  292. pstmt.executeUpdate();
  293. System.out.println("Book Removed.");
  294. } catch (SQLException e) {
  295. e.printStackTrace();
  296. }
  297.  
  298. rs.close();
  299. }
  300. } while ( userChoice != 0 );
  301.  
  302. stmt.close();
  303. conn.close();
  304. } catch (SQLException se) {
  305. //Handle errors for JDBC
  306. se.printStackTrace();
  307. } catch (Exception e) {
  308. //Handle errors for Class.forName
  309. e.printStackTrace();
  310. } finally {
  311. //finally block used to close resources
  312. try {
  313. if (stmt != null) {
  314. stmt.close();
  315. }
  316. } catch (SQLException se2) {
  317. }// nothing we can do
  318. try {
  319. if (conn != null) {
  320. conn.close();
  321. }
  322. } catch (SQLException se) {
  323. se.printStackTrace();
  324. }//end finally try
  325. }//end try
  326. System.out.println("Goodbye!");
  327. }//end main
  328. }//end FirstExample}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement