Subject | Hash | Author | Date (UTC) |
---|---|---|---|
Java code comments organized | f71fb23b2cff7b9c03af9be657625270bbb67ec9 | guest | 2020-04-01 13:51:42 |
test | 67c92914975dc11d760c4ac7d93f71cc69518036 | guest | 2020-04-01 13:41:28 |
test | a1b816039991c592773504913580cc26bccd8b6e | guest | 2020-03-31 23:28:00 |
test | 7c76c49888a3c59d60af21a858160086ad72e4f9 | guest | 2020-03-31 23:27:07 |
test | 0bd95ca9f9cc6f8e47b88b4de23a74b28ebea065 | guest | 2020-03-31 23:23:32 |
test | 50f0c49d1c5027b146770b06a4af7a21c2ad5adb | guest | 2020-03-31 23:19:32 |
Java code organized | 5d1c573bf54cb0ba080c420b59d4d6e30f05601f | guest | 2020-03-31 22:23:22 |
Organized java code | a6672c2167ffde333fb442d814fb113208738b1d | guest | 2020-03-31 22:02:36 |
File notes/code/java/FirstProgBis.java changed (mode: 100644) (index fc7e792..a16536e) | |||
... | ... | public class FirstProgBis { | |
10 | 10 | "testuser", | "testuser", |
11 | 11 | "password" | "password" |
12 | 12 | ); | ); |
13 | /* | ||
13 | /* | ||
14 | 14 | * If at execution time you receive an error that starts with | * If at execution time you receive an error that starts with |
15 | 15 | * "java.sql.SQLException: The server time zone value 'EDT' is unrecognized or | * "java.sql.SQLException: The server time zone value 'EDT' is unrecognized or |
16 | 16 | * represents more than one time zone. You must configure either the server ..." | * represents more than one time zone. You must configure either the server ..." |
File notes/code/java/GuestProgram.java changed (mode: 100644) (index 5535bd0..b9b7934) | |||
1 | // code/java/GuestProgram.java | ||
1 | // code/java/GuestProgram.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; // Importing a java API to read from the keyboard. | import java.util.Scanner; // Importing a java API to read from the keyboard. |
... | ... | public class GuestProgram { | |
32 | 32 | /* | /* |
33 | 33 | * INSERT HERE Solution to exercises 1, 2 and 3. | * INSERT HERE Solution to exercises 1, 2 and 3. |
34 | 34 | * Tip for Exercise 1, this solves the first item. | * Tip for Exercise 1, this solves the first item. |
35 | */ | ||
35 | */ | ||
36 | 36 | System.out.print("How many guests do you have?\n"); | System.out.print("How many guests do you have?\n"); |
37 | 37 | Scanner key = new Scanner(System.in); | Scanner key = new Scanner(System.in); |
38 | 38 | int guest_total = key.nextInt(); | int guest_total = key.nextInt(); |
File notes/code/java/GuestProgram_Solution.java changed (mode: 100644) (index aa7d039..1c8f2d1) | |||
1 | // code/java/GuestProgram_Solution.java | ||
1 | // code/java/GuestProgram_Solution.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; // Importing a java API to read from the keyboard. | import java.util.Scanner; // Importing a java API to read from the keyboard. |
5 | 5 | ||
6 | /* | ||
6 | /* | ||
7 | 7 | * This first part is "standard". Just note that we allow multiple statements and that | * This first part is "standard". Just note that we allow multiple statements and that |
8 | 8 | * the ResultsSet we will construct with our conn objects will be scrollables. | * the ResultsSet we will construct with our conn objects will be scrollables. |
9 | 9 | */ | */ |
File notes/code/java/InsecureProgram.java changed (mode: 100644) (index 11b185c..3e5c8f5) | |||
1 | // code/java/InsecureProgram.java | ||
1 | // code/java/InsecureProgram.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; | import java.util.Scanner; |
File notes/code/java/MongoTest.java changed (mode: 100644) (index e29cb8c..247b32e) | |||
1 | // code/java/MongoTest.java | ||
1 | // code/java/MongoTest.java | ||
2 | 2 | ||
3 | 3 | /* | /* |
4 | * javac -cp .:mongo-java-driver-3.7.0-rc0.jar MongoTest.java | ||
4 | * javac -cp .:mongo-java-driver-3.7.0-rc0.jar MongoTest.java | ||
5 | 5 | * java -cp .:mongo-java-driver-3.7.0-rc0.jar MongoTest | * java -cp .:mongo-java-driver-3.7.0-rc0.jar MongoTest |
6 | 6 | */ | */ |
7 | 7 | ||
... | ... | public class MongoTest { | |
34 | 34 | /* | /* |
35 | 35 | * To create a document, we use the Document class: https://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/ | * To create a document, we use the Document class: https://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/ |
36 | 36 | * We want to insert the following document: | * We want to insert the following document: |
37 | |||
37 | |||
38 | 38 | * { | * { |
39 | 39 | * "name" : "MongoDB", | * "name" : "MongoDB", |
40 | 40 | * "type" : "database", | * "type" : "database", |
... | ... | public class MongoTest { | |
42 | 42 | * "versions": [ "v3.2", "v3.0", "v2.6" ], | * "versions": [ "v3.2", "v3.0", "v2.6" ], |
43 | 43 | * "info" : { "level" : "easy", "used" : "yes" } | * "info" : { "level" : "easy", "used" : "yes" } |
44 | 44 | * } | * } |
45 | |||
45 | |||
46 | 46 | * Remember that the order does not matter. | * Remember that the order does not matter. |
47 | 47 | */ | */ |
48 | 48 |
File notes/code/java/ProgWithErrors.java changed (mode: 100644) (index 195bc3b..4fb33b0) | |||
1 | // code/java/ProgWithErrors.java | ||
1 | // code/java/ProgWithErrors.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 |
File notes/code/java/ScrollingProgram.java changed (mode: 100644) (index 5cb2766..ea880cf) | |||
1 | // code/java/ScrollingProgram.java | ||
1 | // code/java/ScrollingProgram.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | ||
... | ... | public class ScrollingProgram { | |
21 | 21 | ); | ); |
22 | 22 | Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); | Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); |
23 | 23 | /* | /* |
24 | * Finally, we want to be able to move back and forth in our | ||
24 | * Finally, we want to be able to move back and forth in our | ||
25 | 25 | * ResultSets. This implies that we have to also chose if the | * ResultSets. This implies that we have to also chose if the |
26 | * ResultSets will be updatable or not: we chose to have them | ||
26 | * ResultSets will be updatable or not: we chose to have them | ||
27 | 27 | * to be "read-only". | * to be "read-only". |
28 | 28 | */ | */ |
29 | 29 | ) { | ) { |
30 | 30 | ||
31 | 31 | /* | /* |
32 | * Before you ask: no, there are no "simple" way of | ||
32 | * Before you ask: no, there are no "simple" way of | ||
33 | 33 | * constructing a string over multiple lines, | * constructing a string over multiple lines, |
34 | 34 | * besides concatenating them, | * besides concatenating them, |
35 | 35 | * cf. e.g. https://stackoverflow.com/q/878573 | * cf. e.g. https://stackoverflow.com/q/878573 |
... | ... | public class ScrollingProgram { | |
51 | 51 | ");" | ");" |
52 | 52 | // The schema contains only one very simple table. | // The schema contains only one very simple table. |
53 | 53 | ); | ); |
54 | /* | ||
54 | /* | ||
55 | 55 | * We can execute all those queries at once | * We can execute all those queries at once |
56 | 56 | * because we passed the "allowMultiQueries=true" | * because we passed the "allowMultiQueries=true" |
57 | 57 | * token when we created the Connection object. | * token when we created the Connection object. |
... | ... | public class ScrollingProgram { | |
61 | 61 | for (int i = 0; i < 10; i++) | for (int i = 0; i < 10; i++) |
62 | 62 | stmt.addBatch("INSERT INTO TEST VALUES (" + i + ")"); | stmt.addBatch("INSERT INTO TEST VALUES (" + i + ")"); |
63 | 63 | /* | /* |
64 | * no ";" in the statements that we add | ||
64 | * no ";" in the statements that we add | ||
65 | 65 | * to the batch! | * to the batch! |
66 | 66 | */ | */ |
67 | 67 | stmt.executeBatch(); | stmt.executeBatch(); |
... | ... | public class ScrollingProgram { | |
70 | 70 | // Now, let us write a simple query, and navigate in the result: | // Now, let us write a simple query, and navigate in the result: |
71 | 71 | ||
72 | 72 | ResultSet rs = stmt.executeQuery("SELECT * FROM TEST"); | ResultSet rs = stmt.executeQuery("SELECT * FROM TEST"); |
73 | /* | ||
73 | /* | ||
74 | 74 | * We select all the tuples in the table. | * We select all the tuples in the table. |
75 | * If we were to execute this instruction on the | ||
75 | * If we were to execute this instruction on the | ||
76 | 76 | * command-line interface, we would get: | * command-line interface, we would get: |
77 | 77 | ||
78 | 78 | * MariaDB [HW_SCROLLABLE_DEMO]> SELECT * FROM TEST; | * MariaDB [HW_SCROLLABLE_DEMO]> SELECT * FROM TEST; |
... | ... | public class ScrollingProgram { | |
96 | 96 | // We can "jump" to the 8th result in the set: | // We can "jump" to the 8th result in the set: |
97 | 97 | rs.absolute(8); | rs.absolute(8); |
98 | 98 | System.out.printf("%-22s %s %d.\n", "After absolute(8),", "we are at Id", rs.getInt(1)); | System.out.printf("%-22s %s %d.\n", "After absolute(8),", "we are at Id", rs.getInt(1)); |
99 | /* Note that this would display "7" since the | ||
99 | /* Note that this would display "7" since the | ||
100 | 100 | * 8th result contains the value 7 (sql starts | * 8th result contains the value 7 (sql starts |
101 | 101 | * counting at 1. | * counting at 1. |
102 | 102 | */ | */ |
File notes/code/java/SimpleInjection_1.java changed (mode: 100644) (index 5e1237e..abd1e93) | |||
1 | // code/java/SimpleInjection_1.java | ||
1 | // code/java/SimpleInjection_1.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; // Importing a java API to read from the keyboard. | import java.util.Scanner; // Importing a java API to read from the keyboard. |
File notes/code/java/SimpleInjection_2.java changed (mode: 100644) (index 16ef0b0..3a5b179) | |||
1 | // code/java/SimpleInjection_2.java | ||
1 | // code/java/SimpleInjection_2.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; // Importing a java API to read from the keyboard. | import java.util.Scanner; // Importing a java API to read from the keyboard. |
File notes/code/java/SimpleInjection_3.java changed (mode: 100644) (index 2876144..85d65ce) | |||
1 | // code/java/SimpleInjection_3.java | ||
1 | // code/java/SimpleInjection_3.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; // Importing a java API to read from the keyboard. | import java.util.Scanner; // Importing a java API to read from the keyboard. |
File notes/code/java/TestForNull.java changed (mode: 100644) (index 7dbdfda..e0e523a) | |||
1 | // code/java/TestForNull.java | ||
1 | // code/java/TestForNull.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 |
File | Lines added | Lines deleted |
---|---|---|
notes/code/java/FirstProg.java | 1 | 1 |
File notes/code/java/FirstProg.java changed (mode: 100644) (index 9bcf9bf..8376c85) | |||
... | ... | public class FirstProg { | |
10 | 10 | "testuser", | "testuser", |
11 | 11 | "password" | "password" |
12 | 12 | ); | ); |
13 | /* | ||
13 | /* | ||
14 | 14 | * If at execution time you receive an error that starts with | * If at execution time you receive an error that starts with |
15 | 15 | * "java.sql.SQLException: The server time zone value 'EDT' is unrecognized or | * "java.sql.SQLException: The server time zone value 'EDT' is unrecognized or |
16 | 16 | * represents more than one time zone. You must configure either the server ..." | * represents more than one time zone. You must configure either the server ..." |
File | Lines added | Lines deleted |
---|---|---|
notes/code/java/AdvancedProg.java | 2 | 2 |
File notes/code/java/AdvancedProg.java changed (mode: 100644) (index 21fb3f4..9737c7e) | |||
... | ... | public class AdvancedProg { | |
165 | 165 | preparedStatement.executeUpdate(); | preparedStatement.executeUpdate(); |
166 | 166 | } | } |
167 | 167 | ||
168 | |||
168 | |||
169 | 169 | // V. Reading backward and writing in ResultSets | // V. Reading backward and writing in ResultSets |
170 | 170 | ||
171 | 171 | ||
... | ... | public class AdvancedProg { | |
186 | 186 | * CONCUR_UPDATABLE: we can change the database without issuing SQL statement. | * CONCUR_UPDATABLE: we can change the database without issuing SQL statement. |
187 | 187 | */ | */ |
188 | 188 | ||
189 | |||
189 | |||
190 | 190 | // Reading backward | // Reading backward |
191 | 191 | ||
192 | 192 |
File | Lines added | Lines deleted |
---|---|---|
notes/code/java/AdvancedProg.java | 3 | 4 |
File notes/code/java/AdvancedProg.java changed (mode: 100644) (index 23ab789..21fb3f4) | |||
... | ... | public class AdvancedProg { | |
53 | 53 | "Minutes INTEGER, " + | "Minutes INTEGER, " + |
54 | 54 | "Price DOUBLE)"); | "Price DOUBLE)"); |
55 | 55 | ||
56 | /* | ||
56 | /* | ||
57 | 57 | * If we were to execute | * If we were to execute |
58 | 58 | * SHOW TABLES | * SHOW TABLES |
59 | 59 | * directly in the MySQL interpreter, this would display at the screen | * directly in the MySQL interpreter, this would display at the screen |
... | ... | public class AdvancedProg { | |
104 | 104 | String insert4 = "INSERT INTO DVD VALUES ('DD', 129, 0.2)"; | String insert4 = "INSERT INTO DVD VALUES ('DD', 129, 0.2)"; |
105 | 105 | ||
106 | 106 | /* | /* |
107 | * Method 1: Using executeUpdate, if the option allowMultiQueries=true was passed in the url given to getConnection | ||
107 | * Method 1: Using executeUpdate, if the option allowMultiQueries=true was passed in the url given to getConnection | ||
108 | 108 | * and your DBMS supports it. | * and your DBMS supports it. |
109 | 109 | */ | */ |
110 | 110 | stmt.executeUpdate(insert1 + ";" + insert2); | stmt.executeUpdate(insert1 + ";" + insert2); |
... | ... | public class AdvancedProg { | |
115 | 115 | stmt.executeBatch(); | stmt.executeBatch(); |
116 | 116 | ||
117 | 117 | ||
118 | |||
119 | 118 | // IV. Prepared Statements | // IV. Prepared Statements |
120 | 119 | ||
121 | 120 | ||
... | ... | public class AdvancedProg { | |
188 | 187 | */ | */ |
189 | 188 | ||
190 | 189 | ||
191 | // Reading backward | ||
190 | // Reading backward | ||
192 | 191 | ||
193 | 192 | ||
194 | 193 | sqlStatement = "SELECT title FROM DVD WHERE Price < 1;"; | sqlStatement = "SELECT title FROM DVD WHERE Price < 1;"; |
File | Lines added | Lines deleted |
---|---|---|
notes/code/java/AdvancedProg.java | 11 | 11 |
File notes/code/java/AdvancedProg.java changed (mode: 100644) (index 0282a2a..23ab789) | |||
... | ... | public class AdvancedProg { | |
47 | 47 | ) { | ) { |
48 | 48 | ||
49 | 49 | // II. Creating a table | // II. Creating a table |
50 | |||
50 | |||
51 | 51 | stmt.execute("CREATE TABLE DVD (" + | stmt.execute("CREATE TABLE DVD (" + |
52 | 52 | "Title CHAR(25) PRIMARY KEY, " + | "Title CHAR(25) PRIMARY KEY, " + |
53 | 53 | "Minutes INTEGER, " + | "Minutes INTEGER, " + |
... | ... | public class AdvancedProg { | |
94 | 94 | System.out.print(sqlStatement + " changed " + rowsAffected + " row(s).\n"); | System.out.print(sqlStatement + " changed " + rowsAffected + " row(s).\n"); |
95 | 95 | ||
96 | 96 | ||
97 | |||
98 | // Batch Insertion | ||
97 | |||
98 | // Batch Insertion | ||
99 | 99 | ||
100 | 100 | ||
101 | 101 | String insert1 = "INSERT INTO DVD VALUES ('Aa', 129, 0.2)"; | String insert1 = "INSERT INTO DVD VALUES ('Aa', 129, 0.2)"; |
... | ... | public class AdvancedProg { | |
117 | 117 | ||
118 | 118 | ||
119 | 119 | // IV. Prepared Statements | // IV. Prepared Statements |
120 | |||
120 | |||
121 | 121 | ||
122 | 122 | // Example 1 | // Example 1 |
123 | 123 | sqlStatement = "SELECT title FROM DVD WHERE Price <= ?"; // We have a string with an empty slot, represented by "?". | sqlStatement = "SELECT title FROM DVD WHERE Price <= ?"; // We have a string with an empty slot, represented by "?". |
... | ... | public class AdvancedProg { | |
153 | 153 | preparedStatement.setString(2, "The Great Dictator"); | preparedStatement.setString(2, "The Great Dictator"); |
154 | 154 | preparedStatement.setString(3, "The Great Dictator"); | preparedStatement.setString(3, "The Great Dictator"); |
155 | 155 | ||
156 | /* | ||
156 | /* | ||
157 | 157 | * Java compiler will be ok, but we'll have an error at execution time when executing the query. You can uncomment the line below to see for yourself. | * Java compiler will be ok, but we'll have an error at execution time when executing the query. You can uncomment the line below to see for yourself. |
158 | 158 | * rowsAffected = preparedStatement.executeUpdate(); | * rowsAffected = preparedStatement.executeUpdate(); |
159 | 159 | */ | */ |
... | ... | public class AdvancedProg { | |
166 | 166 | preparedStatement.executeUpdate(); | preparedStatement.executeUpdate(); |
167 | 167 | } | } |
168 | 168 | ||
169 | |||
169 | |||
170 | 170 | // V. Reading backward and writing in ResultSets | // V. Reading backward and writing in ResultSets |
171 | |||
171 | |||
172 | 172 | ||
173 | 173 | // To read backward and write in ResultSets, you need to have a statement with certain options: | // To read backward and write in ResultSets, you need to have a statement with certain options: |
174 | 174 | ||
... | ... | public class AdvancedProg { | |
187 | 187 | * CONCUR_UPDATABLE: we can change the database without issuing SQL statement. | * CONCUR_UPDATABLE: we can change the database without issuing SQL statement. |
188 | 188 | */ | */ |
189 | 189 | ||
190 | |||
190 | |||
191 | 191 | // Reading backward | // Reading backward |
192 | |||
192 | |||
193 | 193 | ||
194 | 194 | sqlStatement = "SELECT title FROM DVD WHERE Price < 1;"; | sqlStatement = "SELECT title FROM DVD WHERE Price < 1;"; |
195 | 195 | result = stmtNew.executeQuery(sqlStatement); | result = stmtNew.executeQuery(sqlStatement); |
... | ... | public class AdvancedProg { | |
216 | 216 | * absolute(x): move to the row number x. 1 is the first. | * absolute(x): move to the row number x. 1 is the first. |
217 | 217 | */ | */ |
218 | 218 | ||
219 | |||
219 | |||
220 | 220 | // Changing the values | // Changing the values |
221 | |||
221 | |||
222 | 222 | ||
223 | 223 | System.out.print("\n\nLet us apply a 50% discount. Currently, the prices are:\n"); | System.out.print("\n\nLet us apply a 50% discount. Currently, the prices are:\n"); |
224 | 224 |
File | Lines added | Lines deleted |
---|---|---|
notes/code/java/AdvancedProg.java | 3 | 5 |
File notes/code/java/AdvancedProg.java changed (mode: 100644) (index 1296715..0282a2a) | |||
... | ... | public class AdvancedProg { | |
29 | 29 | try ( | try ( |
30 | 30 | ||
31 | 31 | // I. Passing options to the dababse | // I. Passing options to the dababse |
32 | |||
32 | |||
33 | 33 | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DBPROG" + | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DBPROG" + |
34 | 34 | "?user=testuser" + | "?user=testuser" + |
35 | 35 | "&password=password" + | "&password=password" + |
... | ... | public class AdvancedProg { | |
37 | 37 | "&createDatabaseIfNotExist=true" + | "&createDatabaseIfNotExist=true" + |
38 | 38 | "&useSSL=true"); | "&useSSL=true"); |
39 | 39 | ||
40 | /* | ||
41 | * Read about other options at | ||
40 | /* | ||
41 | * Read about other options at | ||
42 | 42 | * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html | * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html |
43 | 43 | * https://jdbc.postgresql.org/documentation/head/connect.html | * https://jdbc.postgresql.org/documentation/head/connect.html |
44 | 44 | */ | */ |
... | ... | public class AdvancedProg { | |
46 | 46 | Statement stmt = conn.createStatement(); | Statement stmt = conn.createStatement(); |
47 | 47 | ) { | ) { |
48 | 48 | ||
49 | |||
50 | 49 | // II. Creating a table | // II. Creating a table |
51 | 50 | ||
52 | |||
53 | 51 | stmt.execute("CREATE TABLE DVD (" + | stmt.execute("CREATE TABLE DVD (" + |
54 | 52 | "Title CHAR(25) PRIMARY KEY, " + | "Title CHAR(25) PRIMARY KEY, " + |
55 | 53 | "Minutes INTEGER, " + | "Minutes INTEGER, " + |
File | Lines added | Lines deleted |
---|---|---|
notes/code/java/AdvancedProg.java | 6 | 8 |
File notes/code/java/AdvancedProg.java changed (mode: 100644) (index 8a64ef4..1296715) | |||
1 | // code/java/AdvancedProg.java | ||
1 | // code/java/AdvancedProg.java | ||
2 | 2 | ||
3 | 3 | /** | /** |
4 | 4 | * This is a long program, introducing: | * This is a long program, introducing: |
17 | 17 | * DROP TABLE DVD; | * DROP TABLE DVD; |
18 | 18 | * Or do it from within your program! | * Or do it from within your program! |
19 | 19 | ||
20 | * If you use option 1, you will keep inserting tuples in your table: cleaning it with | ||
20 | * If you use option 1, you will keep inserting tuples in your table: cleaning it with | ||
21 | 21 | * DELETE FROM DVD; | * DELETE FROM DVD; |
22 | 22 | * can help. You can do it from within the program! | * can help. You can do it from within the program! |
23 | 23 | */ | */ |
... | ... | public class AdvancedProg { | |
28 | 28 | public static void main(String[] args) { | public static void main(String[] args) { |
29 | 29 | try ( | try ( |
30 | 30 | ||
31 | |||
32 | 31 | // I. Passing options to the dababse | // I. Passing options to the dababse |
33 | 32 | ||
34 | |||
35 | 33 | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DBPROG" + | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DBPROG" + |
36 | 34 | "?user=testuser" + | "?user=testuser" + |
37 | 35 | "&password=password" + | "&password=password" + |
... | ... | public class AdvancedProg { | |
81 | 79 | * https://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getTables(java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String[]) | * https://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getTables(java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String[]) |
82 | 80 | * the full specification of this method. | * the full specification of this method. |
83 | 81 | * All you need to know, for now, is that the third parameter is | * All you need to know, for now, is that the third parameter is |
84 | * String tableNamePattern, | ||
82 | * String tableNamePattern, | ||
85 | 83 | * i.e., what must match the table name as it is stored in the database | * i.e., what must match the table name as it is stored in the database |
86 | 84 | * Here, by using the wildcard "%", we select all the table names. | * Here, by using the wildcard "%", we select all the table names. |
87 | 85 | * We can then iterate over the ResultSet as usual: | * We can then iterate over the ResultSet as usual: |
... | ... | public class AdvancedProg { | |
99 | 97 | ||
100 | 98 | ||
101 | 99 | ||
102 | // Batch Insertion | ||
103 | |||
100 | // Batch Insertion | ||
101 | |||
104 | 102 | ||
105 | 103 | String insert1 = "INSERT INTO DVD VALUES ('Aa', 129, 0.2)"; | String insert1 = "INSERT INTO DVD VALUES ('Aa', 129, 0.2)"; |
106 | 104 | String insert2 = "INSERT INTO DVD VALUES ('Bb', 129, 0.2)"; | String insert2 = "INSERT INTO DVD VALUES ('Bb', 129, 0.2)"; |
... | ... | public class AdvancedProg { | |
193 | 191 | ||
194 | 192 | ||
195 | 193 | // Reading backward | // Reading backward |
196 | |||
194 | |||
197 | 195 | ||
198 | 196 | sqlStatement = "SELECT title FROM DVD WHERE Price < 1;"; | sqlStatement = "SELECT title FROM DVD WHERE Price < 1;"; |
199 | 197 | result = stmtNew.executeQuery(sqlStatement); | result = stmtNew.executeQuery(sqlStatement); |
File notes/code/java/AdvancedProg.java changed (mode: 100644) (index a6a2986..8a64ef4) | |||
1 | /* code/java/AdvancedProg.java */ | ||
2 | |||
3 | /* | ||
4 | |||
5 | This is a long program, introducing: | ||
6 | I. How to pass options when connecting to the database, | ||
7 | II. How to create a table | ||
8 | III. How to insert values | ||
9 | IV. How to use prepared statements | ||
10 | V. How to read backward and write in ResultSets | ||
1 | // code/java/AdvancedProg.java | ||
2 | |||
3 | /** | ||
4 | * This is a long program, introducing: | ||
5 | * I. How to pass options when connecting to the database, | ||
6 | * II. How to create a table | ||
7 | * III. How to insert values | ||
8 | * IV. How to use prepared statements | ||
9 | * V. How to read backward and write in ResultSets | ||
11 | 10 | ||
12 | If you want to run this program multiple times, you have to either: | ||
13 | |||
14 | 1. Comment first statement of II. Creating a table | ||
15 | 2. Change the name of the schema, from HW_DBPROG to whatever you want | ||
16 | 3. Drop the DVD table: connect to your database, and then enter | ||
17 | USE HW_DBPROG; | ||
18 | DROP TABLE DVD; | ||
19 | Or do it from within your program! | ||
11 | * If you want to run this program multiple times, you have to either: | ||
12 | |||
13 | * 1. Comment first statement of II. Creating a table | ||
14 | * 2. Change the name of the schema, from HW_DBPROG to whatever you want | ||
15 | * 3. Drop the DVD table: connect to your database, and then enter | ||
16 | * USE HW_DBPROG; | ||
17 | * DROP TABLE DVD; | ||
18 | * Or do it from within your program! | ||
20 | 19 | ||
21 | If you use option 1, you will keep inserting tuples in your table: cleaning it with | ||
22 | DELETE FROM DVD; | ||
23 | can help. You can do it from within the program! | ||
24 | */ | ||
20 | * If you use option 1, you will keep inserting tuples in your table: cleaning it with | ||
21 | * DELETE FROM DVD; | ||
22 | * can help. You can do it from within the program! | ||
23 | */ | ||
25 | 24 | ||
26 | 25 | import java.sql.*; | import java.sql.*; |
27 | 26 | ||
... | ... | public class AdvancedProg { | |
29 | 28 | public static void main(String[] args) { | public static void main(String[] args) { |
30 | 29 | try ( | try ( |
31 | 30 | ||
32 | /* | ||
33 | * I. Passing options to the dababse | ||
34 | */ | ||
31 | |||
32 | // I. Passing options to the dababse | ||
33 | |||
35 | 34 | ||
36 | 35 | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DBPROG" + | Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DBPROG" + |
37 | 36 | "?user=testuser" + | "?user=testuser" + |
... | ... | public class AdvancedProg { | |
40 | 39 | "&createDatabaseIfNotExist=true" + | "&createDatabaseIfNotExist=true" + |
41 | 40 | "&useSSL=true"); | "&useSSL=true"); |
42 | 41 | ||
43 | // Read about other options at | ||
44 | // https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html | ||
45 | // https://jdbc.postgresql.org/documentation/head/connect.html | ||
42 | /* | ||
43 | * Read about other options at | ||
44 | * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html | ||
45 | * https://jdbc.postgresql.org/documentation/head/connect.html | ||
46 | */ | ||
46 | 47 | ||
47 | 48 | Statement stmt = conn.createStatement(); | Statement stmt = conn.createStatement(); |
48 | 49 | ) { | ) { |
49 | 50 | ||
50 | /* | ||
51 | * II. Creating a table | ||
52 | */ | ||
51 | |||
52 | // II. Creating a table | ||
53 | |||
53 | 54 | ||
54 | 55 | stmt.execute("CREATE TABLE DVD (" + | stmt.execute("CREATE TABLE DVD (" + |
55 | 56 | "Title CHAR(25) PRIMARY KEY, " + | "Title CHAR(25) PRIMARY KEY, " + |
56 | 57 | "Minutes INTEGER, " + | "Minutes INTEGER, " + |
57 | 58 | "Price DOUBLE)"); | "Price DOUBLE)"); |
58 | 59 | ||
59 | /* If we were to execute | ||
60 | /* | ||
61 | * If we were to execute | ||
60 | 62 | * SHOW TABLES | * SHOW TABLES |
61 | 63 | * directly in the MySQL interpreter, this would display at the screen | * directly in the MySQL interpreter, this would display at the screen |
62 | 64 | * | * |
... | ... | public class AdvancedProg { | |
89 | 91 | System.out.println(rs.getString(3)); // In the ResultSet returned by getTables, 3 is the TABLE_NAME. | System.out.println(rs.getString(3)); // In the ResultSet returned by getTables, 3 is the TABLE_NAME. |
90 | 92 | } | } |
91 | 93 | ||
92 | /* | ||
93 | * III. Inserting values | ||
94 | */ | ||
94 | // III. Inserting values | ||
95 | 95 | ||
96 | 96 | String sqlStatement = "INSERT INTO DVD VALUES ('Gone With The Wind', 221, 3);"; | String sqlStatement = "INSERT INTO DVD VALUES ('Gone With The Wind', 221, 3);"; |
97 | 97 | int rowsAffected = stmt.executeUpdate(sqlStatement); | int rowsAffected = stmt.executeUpdate(sqlStatement); |
98 | 98 | System.out.print(sqlStatement + " changed " + rowsAffected + " row(s).\n"); | System.out.print(sqlStatement + " changed " + rowsAffected + " row(s).\n"); |
99 | 99 | ||
100 | 100 | ||
101 | /* | ||
102 | * Batch Insertion | ||
103 | */ | ||
101 | |||
102 | // Batch Insertion | ||
103 | |||
104 | 104 | ||
105 | 105 | String insert1 = "INSERT INTO DVD VALUES ('Aa', 129, 0.2)"; | String insert1 = "INSERT INTO DVD VALUES ('Aa', 129, 0.2)"; |
106 | 106 | String insert2 = "INSERT INTO DVD VALUES ('Bb', 129, 0.2)"; | String insert2 = "INSERT INTO DVD VALUES ('Bb', 129, 0.2)"; |
107 | 107 | String insert3 = "INSERT INTO DVD VALUES ('Cc', 129, 0.2)"; | String insert3 = "INSERT INTO DVD VALUES ('Cc', 129, 0.2)"; |
108 | 108 | String insert4 = "INSERT INTO DVD VALUES ('DD', 129, 0.2)"; | String insert4 = "INSERT INTO DVD VALUES ('DD', 129, 0.2)"; |
109 | 109 | ||
110 | // Method 1: Using executeUpdate, if the option allowMultiQueries=true was passed in the url given to getConnection and your DBMS supports it. | ||
110 | /* | ||
111 | * Method 1: Using executeUpdate, if the option allowMultiQueries=true was passed in the url given to getConnection | ||
112 | * and your DBMS supports it. | ||
113 | */ | ||
111 | 114 | stmt.executeUpdate(insert1 + ";" + insert2); | stmt.executeUpdate(insert1 + ";" + insert2); |
112 | 115 | ||
113 | 116 | // Method 2: Using the addBatch and executeBatch methods | // Method 2: Using the addBatch and executeBatch methods |
... | ... | public class AdvancedProg { | |
116 | 119 | stmt.executeBatch(); | stmt.executeBatch(); |
117 | 120 | ||
118 | 121 | ||
119 | /* | ||
120 | * IV. Prepared Statements | ||
121 | */ | ||
122 | |||
123 | // IV. Prepared Statements | ||
124 | |||
122 | 125 | ||
123 | 126 | // Example 1 | // Example 1 |
124 | 127 | sqlStatement = "SELECT title FROM DVD WHERE Price <= ?"; // We have a string with an empty slot, represented by "?". | sqlStatement = "SELECT title FROM DVD WHERE Price <= ?"; // We have a string with an empty slot, represented by "?". |
125 | 128 | PreparedStatement ps = conn.prepareStatement(sqlStatement); // We create a PreparedStatement object, using that string with an empty slot. | PreparedStatement ps = conn.prepareStatement(sqlStatement); // We create a PreparedStatement object, using that string with an empty slot. |
126 | // Note that once the object is created, we cannot change the content of the query, beside instantiating the slot. | ||
127 | // cf. e.g. the discussion at <https://stackoverflow.com/q/25902881/>. | ||
129 | /* | ||
130 | * Note that once the object is created, we cannot change the content of the query, beside instantiating the slot. | ||
131 | * cf. e.g. the discussion at <https://stackoverflow.com/q/25902881/>. | ||
132 | */ | ||
128 | 133 | double maxprice = 0.5; | double maxprice = 0.5; |
129 | 134 | ps.setDouble(1, maxprice); // This statement says "Fill the first slot with the value of maxprice". | ps.setDouble(1, maxprice); // This statement says "Fill the first slot with the value of maxprice". |
130 | 135 | ResultSet result = ps.executeQuery(); // And then we can execute the query, and display the results: | ResultSet result = ps.executeQuery(); // And then we can execute the query, and display the results: |
... | ... | public class AdvancedProg { | |
152 | 157 | preparedStatement.setString(2, "The Great Dictator"); | preparedStatement.setString(2, "The Great Dictator"); |
153 | 158 | preparedStatement.setString(3, "The Great Dictator"); | preparedStatement.setString(3, "The Great Dictator"); |
154 | 159 | ||
155 | // Java compiler will be ok, but we'll have an error at execution time when executing the query. You can uncomment the line below to see for yourself. | ||
156 | //rowsAffected = preparedStatement.executeUpdate(); | ||
160 | /* | ||
161 | * Java compiler will be ok, but we'll have an error at execution time when executing the query. You can uncomment the line below to see for yourself. | ||
162 | * rowsAffected = preparedStatement.executeUpdate(); | ||
163 | */ | ||
157 | 164 | ||
158 | 165 | // Of course, we can use prepared statement inside loops. | // Of course, we can use prepared statement inside loops. |
159 | 166 | for (int i = 1; i < 5; i++) { | for (int i = 1; i < 5; i++) { |
... | ... | public class AdvancedProg { | |
163 | 170 | preparedStatement.executeUpdate(); | preparedStatement.executeUpdate(); |
164 | 171 | } | } |
165 | 172 | ||
166 | /* | ||
167 | * V. Reading backward and writing in ResultSets | ||
168 | */ | ||
173 | |||
174 | // V. Reading backward and writing in ResultSets | ||
175 | |||
169 | 176 | ||
170 | 177 | // To read backward and write in ResultSets, you need to have a statement with certain options: | // To read backward and write in ResultSets, you need to have a statement with certain options: |
171 | 178 | ||
... | ... | public class AdvancedProg { | |
184 | 191 | * CONCUR_UPDATABLE: we can change the database without issuing SQL statement. | * CONCUR_UPDATABLE: we can change the database without issuing SQL statement. |
185 | 192 | */ | */ |
186 | 193 | ||
187 | /* | ||
188 | * Reading backward | ||
189 | */ | ||
194 | |||
195 | // Reading backward | ||
196 | |||
190 | 197 | ||
191 | 198 | sqlStatement = "SELECT title FROM DVD WHERE Price < 1;"; | sqlStatement = "SELECT title FROM DVD WHERE Price < 1;"; |
192 | 199 | result = stmtNew.executeQuery(sqlStatement); | result = stmtNew.executeQuery(sqlStatement); |
... | ... | public class AdvancedProg { | |
213 | 220 | * absolute(x): move to the row number x. 1 is the first. | * absolute(x): move to the row number x. 1 is the first. |
214 | 221 | */ | */ |
215 | 222 | ||
216 | /* | ||
217 | * Changing the values | ||
218 | */ | ||
223 | |||
224 | // Changing the values | ||
225 | |||
219 | 226 | ||
220 | 227 | System.out.print("\n\nLet us apply a 50% discount. Currently, the prices are:\n"); | System.out.print("\n\nLet us apply a 50% discount. Currently, the prices are:\n"); |
221 | 228 |
File notes/code/java/FirstProg.java changed (mode: 100644) (index ebf04be..9bcf9bf) | |||
... | ... | public class FirstProg { | |
10 | 10 | "testuser", | "testuser", |
11 | 11 | "password" | "password" |
12 | 12 | ); | ); |
13 | /* If at execution time you receive an error that starts with | ||
13 | /* | ||
14 | * If at execution time you receive an error that starts with | ||
14 | 15 | * "java.sql.SQLException: The server time zone value 'EDT' is unrecognized or | * "java.sql.SQLException: The server time zone value 'EDT' is unrecognized or |
15 | 16 | * represents more than one time zone. You must configure either the server ..." | * represents more than one time zone. You must configure either the server ..." |
16 | 17 | * add ?serverTimezone=UTC at the end of the previous string, | * add ?serverTimezone=UTC at the end of the previous string, |
File notes/code/java/FirstProgBis.java changed (mode: 100644) (index 1c0ba32..fc7e792) | |||
... | ... | public class FirstProgBis { | |
10 | 10 | "testuser", | "testuser", |
11 | 11 | "password" | "password" |
12 | 12 | ); | ); |
13 | /* If at execution time you receive an error that starts with | ||
13 | /* | ||
14 | * If at execution time you receive an error that starts with | ||
14 | 15 | * "java.sql.SQLException: The server time zone value 'EDT' is unrecognized or | * "java.sql.SQLException: The server time zone value 'EDT' is unrecognized or |
15 | 16 | * represents more than one time zone. You must configure either the server ..." | * represents more than one time zone. You must configure either the server ..." |
16 | 17 | * add ?serverTimezone=UTC at the end of the previous string, | * add ?serverTimezone=UTC at the end of the previous string, |
File notes/code/java/GuestProgram.java changed (mode: 100644) (index 55b1bc9..5535bd0) | |||
1 | /* code/java/GuestProgram.java */ | ||
1 | // code/java/GuestProgram.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; // Importing a java API to read from the keyboard. | import java.util.Scanner; // Importing a java API to read from the keyboard. |
... | ... | public class GuestProgram { | |
14 | 14 | ); | ); |
15 | 15 | Statement stmt = conn.createStatement(); | Statement stmt = conn.createStatement(); |
16 | 16 | ) { | ) { |
17 | // We create a schema, use it, create two tables, and insert a value in the second one. | ||
17 | /** We create a schema, use it, create two tables, and insert a value in the second one. */ | ||
18 | 18 | stmt.execute( | stmt.execute( |
19 | 19 | "CREATE SCHEMA HW_GUEST_PROGRAM;" + | "CREATE SCHEMA HW_GUEST_PROGRAM;" + |
20 | 20 | "USE HW_GUEST_PROGRAM;" | "USE HW_GUEST_PROGRAM;" |
... | ... | public class GuestProgram { | |
29 | 29 | "INSERT INTO BLACKLIST VALUES (\"Marcus Hells\");"; | "INSERT INTO BLACKLIST VALUES (\"Marcus Hells\");"; |
30 | 30 | ); | ); |
31 | 31 | ||
32 | // INSERT HERE Solution to exercises 1, 2 and 3. | ||
33 | // Tip for Exercise 1, this solves the first item. | ||
32 | /* | ||
33 | * INSERT HERE Solution to exercises 1, 2 and 3. | ||
34 | * Tip for Exercise 1, this solves the first item. | ||
35 | */ | ||
34 | 36 | System.out.print("How many guests do you have?\n"); | System.out.print("How many guests do you have?\n"); |
35 | 37 | Scanner key = new Scanner(System.in); | Scanner key = new Scanner(System.in); |
36 | 38 | int guest_total = key.nextInt(); | int guest_total = key.nextInt(); |
File notes/code/java/GuestProgram_Solution.java changed (mode: 100644) (index 9439894..aa7d039) | |||
1 | /* code/java/GuestProgram_Solution.java */ | ||
1 | // code/java/GuestProgram_Solution.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; // Importing a java API to read from the keyboard. | import java.util.Scanner; // Importing a java API to read from the keyboard. |
5 | 5 | ||
6 | /* This first part is "standard". Just note that we allow multiple statements and that | ||
7 | the ResultsSet we will construct with our conn objects will be scrollables.*/ | ||
6 | /* | ||
7 | * This first part is "standard". Just note that we allow multiple statements and that | ||
8 | * the ResultsSet we will construct with our conn objects will be scrollables. | ||
9 | */ | ||
8 | 10 | ||
9 | 11 | public class GuestProgram_Solution { | public class GuestProgram_Solution { |
10 | 12 | public static void main(String[] args) { | public static void main(String[] args) { |
... | ... | public class GuestProgram_Solution { | |
52 | 54 | ||
53 | 55 | // Solution B | // Solution B |
54 | 56 | /* | /* |
55 | PreparedStatement ps = conn.prepareStatement("INSERT INTO GUEST VALUES(?, ?, NULL);"); | ||
56 | while (counter < guest_total) { | ||
57 | System.out.print("Enter name of guest " + (counter + 1) + ".\n"); | ||
58 | guest_name = key.nextLine(); | ||
59 | ps.setInt(1, counter); | ||
60 | ps.setString(2, guest_name); | ||
61 | ps.executeUpdate(); | ||
62 | counter++; | ||
63 | } | ||
64 | */ | ||
65 | |||
57 | * PreparedStatement ps = conn.prepareStatement("INSERT INTO GUEST VALUES(?, ?, NULL);"); | ||
58 | * while (counter < guest_total) { | ||
59 | * System.out.print("Enter name of guest " + (counter + 1) + ".\n"); | ||
60 | * guest_name = key.nextLine(); | ||
61 | * ps.setInt(1, counter); | ||
62 | * ps.setString(2, guest_name); | ||
63 | * ps.executeUpdate(); | ||
64 | * counter++; | ||
65 | * } | ||
66 | */ | ||
66 | 67 | // Needed to test our solution to the following two exercises. | // Needed to test our solution to the following two exercises. |
67 | 68 | stmt.execute("INSERT INTO GUEST VALUES (-1, \"Marcus Hells\", true);"); | stmt.execute("INSERT INTO GUEST VALUES (-1, \"Marcus Hells\", true);"); |
68 | 69 | stmt.execute("INSERT INTO GUEST VALUES (-2, \"Marcus Hells\", false);"); | stmt.execute("INSERT INTO GUEST VALUES (-2, \"Marcus Hells\", false);"); |
File notes/code/java/InsecureProgram.java changed (mode: 100644) (index 215d5cd..11b185c) | |||
1 | /* code/java/InsecureProgram.java */ | ||
1 | // code/java/InsecureProgram.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; | import java.util.Scanner; |
File notes/code/java/MongoTest.java changed (mode: 100644) (index 5d9407b..e29cb8c) | |||
1 | /* code/java/MongoTest.java */ | ||
1 | // code/java/MongoTest.java | ||
2 | 2 | ||
3 | // javac -cp .:mongo-java-driver-3.7.0-rc0.jar MongoTest.java | ||
4 | // java -cp .:mongo-java-driver-3.7.0-rc0.jar MongoTest | ||
3 | /* | ||
4 | * javac -cp .:mongo-java-driver-3.7.0-rc0.jar MongoTest.java | ||
5 | * java -cp .:mongo-java-driver-3.7.0-rc0.jar MongoTest | ||
6 | */ | ||
5 | 7 | ||
6 | 8 | import com.mongodb.MongoClient; | import com.mongodb.MongoClient; |
7 | 9 | import com.mongodb.MongoClientURI; | import com.mongodb.MongoClientURI; |
... | ... | public class MongoTest { | |
19 | 21 | public static void main(String[] args) { | public static void main(String[] args) { |
20 | 22 | ||
21 | 23 | ||
22 | // MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017"); | ||
23 | // MongoClient mongoClient = new MongoClient(connectionString); | ||
24 | /* | ||
25 | * MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017"); | ||
26 | * MongoClient mongoClient = new MongoClient(connectionString); | ||
27 | */ | ||
24 | 28 | MongoClient mongoClient = new MongoClient(); | MongoClient mongoClient = new MongoClient(); |
25 | 29 | ||
26 | 30 | MongoDatabase database = mongoClient.getDatabase("mydb"); // Creates the database if it doesn't exist, when we add documents to it. | MongoDatabase database = mongoClient.getDatabase("mydb"); // Creates the database if it doesn't exist, when we add documents to it. |
27 | 31 | ||
28 | 32 | MongoCollection < Document > collection = database.getCollection("test"); | MongoCollection < Document > collection = database.getCollection("test"); |
29 | 33 | ||
30 | // To create a document, we use the Document class: https://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/ | ||
31 | // We want to insert the following document: | ||
32 | 34 | /* | /* |
33 | { | ||
34 | "name" : "MongoDB", | ||
35 | "type" : "database", | ||
36 | "count" : 1, | ||
37 | "versions": [ "v3.2", "v3.0", "v2.6" ], | ||
38 | "info" : { "level" : "easy", "used" : "yes" } | ||
39 | } | ||
35 | * To create a document, we use the Document class: https://mongodb.github.io/mongo-java-driver/3.4/driver/getting-started/quick-start/ | ||
36 | * We want to insert the following document: | ||
37 | |||
38 | * { | ||
39 | * "name" : "MongoDB", | ||
40 | * "type" : "database", | ||
41 | * "count" : 1, | ||
42 | * "versions": [ "v3.2", "v3.0", "v2.6" ], | ||
43 | * "info" : { "level" : "easy", "used" : "yes" } | ||
44 | * } | ||
45 | |||
46 | * Remember that the order does not matter. | ||
40 | 47 | */ | */ |
41 | // Remember that the order does not matter. | ||
42 | 48 | ||
43 | 49 | Document doc = new Document("name", "MongoDB"); | Document doc = new Document("name", "MongoDB"); |
44 | 50 | doc.append("type", "database"); | doc.append("type", "database"); |
File notes/code/java/ProgWithErrors.java changed (mode: 100644) (index f4a5489..195bc3b) | |||
1 | /* code/java/ProgWithErrors.java */ | ||
1 | // code/java/ProgWithErrors.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | ||
... | ... | public class ProgWithErrors{ | |
10 | 10 | Statement stmt = conn.createStatement(); | Statement stmt = conn.createStatement(); |
11 | 11 | ) { | ) { |
12 | 12 | ||
13 | /* Errors after this point.*/ | ||
13 | // Errors after this point. | ||
14 | 14 | ||
15 | 15 | String strSelect = "SELECT title FROM DISKS WHERE qty > 40;"; | String strSelect = "SELECT title FROM DISKS WHERE qty > 40;"; |
16 | 16 | ResultSet rset = stmt.executeUpdate(strSelect); | ResultSet rset = stmt.executeUpdate(strSelect); |
... | ... | public class ProgWithErrors{ | |
29 | 29 | ||
30 | 30 | conn.close(); | conn.close(); |
31 | 31 | ||
32 | /* Errors before this point.*/ | ||
32 | // Errors before this point. | ||
33 | 33 | ||
34 | 34 | } catch(SQLException ex) { | } catch(SQLException ex) { |
35 | 35 | ex.printStackTrace(); | ex.printStackTrace(); |
File notes/code/java/ProgWithErrorsPatched.java changed (mode: 100644) (index 9403ac4..63157d2) | |||
1 | /* code/java/ProgWithErrorsPatched.java */ | ||
1 | // code/java/ProgWithErrorsPatched.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | ||
... | ... | public class ProgWithErrorsPatched { | |
9 | 9 | "HW_TestDB?user=testuser&password=password"); Statement stmt = conn.createStatement(); | "HW_TestDB?user=testuser&password=password"); Statement stmt = conn.createStatement(); |
10 | 10 | ) { | ) { |
11 | 11 | ||
12 | /* Errors after this point.*/ | ||
12 | // Errors after this point. | ||
13 | 13 | ||
14 | 14 | String strSelect = "SELECT title FROM DISKS WHERE qty > 40;"; | String strSelect = "SELECT title FROM DISKS WHERE qty > 40;"; |
15 | 15 | ResultSet rset = stmt.executeQuery(strSelect); // Error 1 | ResultSet rset = stmt.executeQuery(strSelect); // Error 1 |
... | ... | public class ProgWithErrorsPatched { | |
29 | 29 | ||
30 | 30 | conn.close(); | conn.close(); |
31 | 31 | ||
32 | /* Errors before this point.*/ | ||
32 | // Errors before this point. | ||
33 | 33 | ||
34 | 34 | } catch (SQLException ex) { | } catch (SQLException ex) { |
35 | 35 | ex.printStackTrace(); | ex.printStackTrace(); |
File notes/code/java/ScrollingProgram.java changed (mode: 100644) (index d790590..5cb2766) | |||
1 | /* code/java/ScrollingProgram.java */ | ||
1 | // code/java/ScrollingProgram.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | ||
... | ... | public class ScrollingProgram { | |
12 | 12 | "?user=testuser" + | "?user=testuser" + |
13 | 13 | "&password=password" + | "&password=password" + |
14 | 14 | "&allowMultiQueries=true" | "&allowMultiQueries=true" |
15 | // We want to allow multiple statements | ||
16 | // to be shipped in one execute() call. | ||
15 | |||
16 | /* | ||
17 | * We want to allow multiple statements | ||
18 | * to be shipped in one execute() call. | ||
19 | */ | ||
20 | |||
17 | 21 | ); | ); |
18 | 22 | Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); | Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); |
19 | // Finally, we want to be able to move back and forth in our | ||
20 | // ResultSets. This implies that we have to also chose if the | ||
21 | // ResultSets will be updatable or not: we chose to have them | ||
22 | // to be "read-only". | ||
23 | /* | ||
24 | * Finally, we want to be able to move back and forth in our | ||
25 | * ResultSets. This implies that we have to also chose if the | ||
26 | * ResultSets will be updatable or not: we chose to have them | ||
27 | * to be "read-only". | ||
28 | */ | ||
23 | 29 | ) { | ) { |
24 | 30 | ||
25 | // Before you ask: no, there are no "simple" way of | ||
26 | // constructing a string over multiple lines, | ||
27 | // besides concatenating them, | ||
28 | // cf. e.g. https://stackoverflow.com/q/878573 | ||
31 | /* | ||
32 | * Before you ask: no, there are no "simple" way of | ||
33 | * constructing a string over multiple lines, | ||
34 | * besides concatenating them, | ||
35 | * cf. e.g. https://stackoverflow.com/q/878573 | ||
36 | */ | ||
37 | |||
29 | 38 | ||
30 | 39 | stmt.execute( | stmt.execute( |
31 | 40 | "DROP SCHEMA IF EXISTS HW_SCROLLABLE_DEMO;" + | "DROP SCHEMA IF EXISTS HW_SCROLLABLE_DEMO;" + |
32 | // We drop the schema we want to use if it already exists. | ||
33 | // (This allows to execute the same program multiple times.) | ||
41 | /* | ||
42 | * We drop the schema we want to use if it already exists. | ||
43 | * (This allows to execute the same program multiple times.) | ||
44 | */ | ||
45 | |||
34 | 46 | "CREATE SCHEMA HW_SCROLLABLE_DEMO;" + | "CREATE SCHEMA HW_SCROLLABLE_DEMO;" + |
35 | 47 | "USE HW_SCROLLABLE_DEMO;" + | "USE HW_SCROLLABLE_DEMO;" + |
36 | 48 | // We create and use the schema. | // We create and use the schema. |
... | ... | public class ScrollingProgram { | |
39 | 51 | ");" | ");" |
40 | 52 | // The schema contains only one very simple table. | // The schema contains only one very simple table. |
41 | 53 | ); | ); |
42 | |||
43 | // We can execute all those queries at once | ||
44 | // because we passed the "allowMultiQueries=true" | ||
45 | // token when we created the Connection object. | ||
54 | /* | ||
55 | * We can execute all those queries at once | ||
56 | * because we passed the "allowMultiQueries=true" | ||
57 | * token when we created the Connection object. | ||
58 | */ | ||
46 | 59 | ||
47 | 60 | // Let us insert some dummy values in this dummy table: | // Let us insert some dummy values in this dummy table: |
48 | 61 | for (int i = 0; i < 10; i++) | for (int i = 0; i < 10; i++) |
49 | 62 | stmt.addBatch("INSERT INTO TEST VALUES (" + i + ")"); | stmt.addBatch("INSERT INTO TEST VALUES (" + i + ")"); |
50 | // no ";" in the statements that we add | ||
51 | // to the batch! | ||
52 | |||
63 | /* | ||
64 | * no ";" in the statements that we add | ||
65 | * to the batch! | ||
66 | */ | ||
53 | 67 | stmt.executeBatch(); | stmt.executeBatch(); |
54 | // We execute the 10 statements that were loaded | ||
55 | // at once. | ||
68 | // We execute the 10 statements that were loaded at once. | ||
56 | 69 | ||
57 | 70 | // Now, let us write a simple query, and navigate in the result: | // Now, let us write a simple query, and navigate in the result: |
58 | 71 | ||
59 | 72 | ResultSet rs = stmt.executeQuery("SELECT * FROM TEST"); | ResultSet rs = stmt.executeQuery("SELECT * FROM TEST"); |
60 | /* We select all the tuples in the table. | ||
61 | If we were to execute this instruction on the | ||
62 | command-line interface, we would get: | ||
63 | |||
64 | MariaDB [HW_SCROLLABLE_DEMO]> SELECT * FROM TEST; | ||
65 | +----+ | ||
66 | | Id | | ||
67 | +----+ | ||
68 | | 0 | | ||
69 | | 1 | | ||
70 | | 2 | | ||
71 | | 3 | | ||
72 | | 4 | | ||
73 | | 5 | | ||
74 | | 6 | | ||
75 | | 7 | | ||
76 | | 8 | | ||
77 | | 9 | | ||
78 | +----+ | ||
79 | 10 rows in set (0.001 sec) | ||
80 | |||
81 | */ | ||
73 | /* | ||
74 | * We select all the tuples in the table. | ||
75 | * If we were to execute this instruction on the | ||
76 | * command-line interface, we would get: | ||
77 | |||
78 | * MariaDB [HW_SCROLLABLE_DEMO]> SELECT * FROM TEST; | ||
79 | * +----+ | ||
80 | * | Id | | ||
81 | * +----+ | ||
82 | * | 0 | | ||
83 | * | 1 | | ||
84 | * | 2 | | ||
85 | * | 3 | | ||
86 | * | 4 | | ||
87 | * | 5 | | ||
88 | * | 6 | | ||
89 | * | 7 | | ||
90 | * | 8 | | ||
91 | * | 9 | | ||
92 | * +----+ | ||
93 | * 10 rows in set (0.001 sec) | ||
94 | */ | ||
82 | 95 | ||
83 | 96 | // We can "jump" to the 8th result in the set: | // We can "jump" to the 8th result in the set: |
84 | 97 | rs.absolute(8); | rs.absolute(8); |
85 | 98 | System.out.printf("%-22s %s %d.\n", "After absolute(8),", "we are at Id", rs.getInt(1)); | System.out.printf("%-22s %s %d.\n", "After absolute(8),", "we are at Id", rs.getInt(1)); |
86 | // Note that this would display "7" since the | ||
87 | // 8th result contains the value 7 (sql starts | ||
88 | // counting at 1. | ||
99 | /* Note that this would display "7" since the | ||
100 | * 8th result contains the value 7 (sql starts | ||
101 | * counting at 1. | ||
102 | */ | ||
89 | 103 | ||
90 | 104 | // We can move back 1 item: | // We can move back 1 item: |
91 | 105 | rs.relative(-1); | rs.relative(-1); |
File notes/code/java/SimpleInjection_1.java changed (mode: 100644) (index ada9031..5e1237e) | |||
1 | /* code/java/SimpleInjection_1.java */ | ||
1 | // code/java/SimpleInjection_1.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; // Importing a java API to read from the keyboard. | import java.util.Scanner; // Importing a java API to read from the keyboard. |
File notes/code/java/SimpleInjection_2.java changed (mode: 100644) (index 0bed710..16ef0b0) | |||
1 | /* code/java/SimpleInjection_2.java */ | ||
1 | // code/java/SimpleInjection_2.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; // Importing a java API to read from the keyboard. | import java.util.Scanner; // Importing a java API to read from the keyboard. |
File notes/code/java/SimpleInjection_3.java changed (mode: 100644) (index b6d32ca..2876144) | |||
1 | /* code/java/SimpleInjection_3.java */ | ||
1 | // code/java/SimpleInjection_3.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 | import java.util.Scanner; // Importing a java API to read from the keyboard. | import java.util.Scanner; // Importing a java API to read from the keyboard. |
File notes/code/java/TestForNull.java changed (mode: 100644) (index 4e122a5..7dbdfda) | |||
1 | /* code/java/TestForNull.java */ | ||
1 | // code/java/TestForNull.java | ||
2 | 2 | ||
3 | 3 | import java.sql.*; | import java.sql.*; |
4 | 4 |