Guest User

Untitled

a guest
Nov 15th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. sql1: create table if not exists Games(Gamesupc integer, name varchar(10), type varchar(10), release integer, rating varchar(3))
  2. Exception in thread "main" java.lang.NumberFormatException: For input string: "Sports"
  3.  
  4. at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  5.  
  6. at java.lang.Integer.parseInt(Integer.java:580)
  7.  
  8. at java.lang.Integer.parseInt(Integer.java:615)
  9.  
  10. at LoadGames.main(LoadGames.java:50)
  11.  
  12. import java.io.File;
  13. import java.io.FileNotFoundException;
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.SQLException;
  17. import java.sql.Statement;
  18. import java.util.Scanner;
  19. public class LoadGames {
  20.  
  21. public static void main(String[] args) {
  22. Connection c = null;
  23. Statement s = null;
  24. Scanner fromFile = null;
  25. String sql1 = null, sql2 = null;
  26. String line = null, name = null, type = null, rating = null;
  27. String[ ] fields;
  28. int release = 0;
  29.  
  30. try {
  31. // Define Connection and Statement objects.
  32. Class.forName("org.sqlite.JDBC");
  33. c = DriverManager.getConnection("jdbc:sqlite:Games.db");
  34. s = c.createStatement();
  35.  
  36. // Instantiate scanner to read from file.
  37. fromFile = new Scanner(new File ("Games.csv"));
  38.  
  39.  
  40. // Create Games table.
  41. sql1 = "create table if not exists " +
  42. "Games(Gamesupc integer, " +
  43. "name varchar(10), " +
  44. "type varchar(10), " +
  45. "release integer, " +
  46. "rating varchar(3))";
  47. System.out.println("sql1: " + sql1);
  48. s.executeUpdate(sql1);
  49.  
  50. // Populate Games table.
  51. for (int upc = 1001; fromFile.hasNextLine( ); upc++) {
  52. line = fromFile.nextLine( );
  53. fields = line.split(",");
  54. name = fields[0].trim( );
  55. type = fields[1].trim( );
  56. release = Integer.parseInt(fields[2].trim( ));
  57. rating = fields[3].trim();
  58. sql2 = String.format(
  59. "insert into Games (Gamesupc, name, type, release, rating) " +
  60. "values (%d, '%s', '%s', %d, '%s');",
  61. upc, name, type, release, rating);
  62. System.out.println(sql2);
  63. s.executeUpdate(sql2);
  64. }
  65. c.close( );
  66. }
  67. catch (FileNotFoundException e) {
  68. System.out.println("File queries.sql not found.");
  69. System.err.println( e.getClass().getName() +
  70. ": " + e.getMessage() );
  71. }
  72. catch(SQLException e) {
  73. System.out.println("SQLException.");
  74. System.err.println( e.getClass().getName() +
  75. ": " + e.getMessage() );
  76. }
  77. catch (ClassNotFoundException e ) {
  78. System.err.println( e.getClass().getName() +
  79. ": " + e.getMessage() );
  80. }
  81. finally {
  82. fromFile.close( );
  83. }
  84. }
  85. }
  86.  
  87. import java.sql.Connection;
  88. import java.sql.DriverManager;
  89. import java.sql.ResultSet;
  90. import java.sql.SQLException;
  91. import java.sql.Statement;
  92. import java.util.Scanner;
  93. public class DisplayGames {
  94.  
  95. public static void main(String[] args) {
  96. Connection c = null;
  97. Statement s = null;
  98. ResultSet rs = null;
  99. int upc = 0, release = 0;
  100. String sql = null, name = null, type= null, rating = null;
  101. Scanner fromKeyboard = new Scanner(System.in);
  102.  
  103. try {
  104. // Define Connection and Statement objects.
  105. Class.forName("org.sqlite.JDBC");
  106. c = DriverManager.getConnection("jdbc:sqlite:Games.db");
  107. s = c.createStatement();
  108.  
  109. while (upc != -1) {
  110. // Read id and display corresponding table row.
  111. System.out.print("Enter Product UPC: ");
  112. upc = fromKeyboard.nextInt( );
  113. sql = "select name, type, release, rating from Games " +
  114. "where Gamesupc = " + upc + ";";
  115. System.out.println(sql);
  116. rs = s.executeQuery(sql);
  117. while (rs.next( )) {
  118. name = rs.getString("name");
  119. type = rs.getString("type");
  120. release = rs.getInt("release");
  121. rating = rs.getString("rating");
  122. System.out.println("Name: " + name);
  123. System.out.println("Type: " + type);
  124. System.out.println("Release: " + release);
  125. System.out.println("Rating: " + rating);
  126. }
  127. }
  128. }
  129. catch(SQLException e) {
  130. System.out.println("SQLException.");
  131. System.err.println( e.getClass().getName() +
  132. ": " + e.getMessage() );
  133. }
  134. catch (ClassNotFoundException e ) {
  135. System.err.println( e.getClass().getName() +
  136. ": " + e.getMessage() );
  137. }
  138. finally {
  139. fromKeyboard.close( );
  140. }
  141.  
  142. }
  143.  
  144. }
Add Comment
Please, Sign In to add comment