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