List of commits:
Subject Hash Author Date (UTC)
Correcting the chapter on db programming. 484c4a6372056106bae77e48b397c1174738fc26 aubert@math.cnrs.fr 2019-04-04 18:57:50
Added solution to first problem of Exam 2. 45881a9b8ab1e5a0071b58d72362059593365d52 aubert@math.cnrs.fr 2019-04-03 18:38:48
Quick note in Java programming. 625282d4fece1f6a7a617a8c9bcf7c3c856185b3 aubert@math.cnrs.fr 2019-04-02 18:42:48
Added exam #2 0aa6788451b34cb6371fed5a0f719a4ac52cf0c8 aubert@math.cnrs.fr 2019-04-01 18:37:43
Fixing a typo. 502262b742ac86d7b3d5e95a0f9fcd7ca3a6e586 aubert@math.cnrs.fr 2019-03-22 17:35:43
Forgot one figure. 1212e57a5f2e7b2f104f432feb4dce0669cdb452 aubert@math.cnrs.fr 2019-03-22 17:32:36
Working on Ch. 4, adding the content of the weekly announcements to the core of the document, added illustrations on the conversion E.R. -> Rel. model, fixed various typos. 1995837f41c1508ae809d5a91065bdefd5bd6c03 aubert@math.cnrs.fr 2019-03-22 17:27:51
Fixing exercise and normalization in Chapter 4. 7aa4b9a581daf7144dd485ba791b7707585088b8 aubert@math.cnrs.fr 2019-03-20 18:55:42
Quick fix on UML diagrams. 86a52b2f6ce92750bdb2c33eaa4dbc4497322e1c aubert@math.cnrs.fr 2019-03-18 18:37:24
Adding some missing files. 0c090f6d43e84b71ca5e1f6d3e8d1aa0d733824d aubert@math.cnrs.fr 2019-03-18 18:28:32
Fixing the style for line numbers, adding information for weak entities, drawing some figures (for crow's notation, for alternative notations), removing various images. 26ce9a96701649988cfdfcb3e61011f4e154896d aubert@math.cnrs.fr 2019-03-18 18:25:09
Added solution to quiz #4. db0de8ba5c6605d77fdbc076f3a9e5b3851d83e0 aubert@math.cnrs.fr 2019-03-15 20:57:13
Added questions for Quiz #4. 30d908601e9431ec0901d4001ddbf99bf14f75d0 aubert@math.cnrs.fr 2019-03-15 17:52:24
Worked on narrative in Chapter 4, mostly ER diagrams. 9e0cc5fc5a2b4087fe1191f1ba5926f87ad5a7bd aubert@math.cnrs.fr 2019-03-12 18:57:16
Updated list of known bugs. d51ff0a5bb73f09eb8d3eb7520ebb77555911e69 aubert@math.cnrs.fr 2019-03-12 17:09:22
Updating the README with the list of new files. bb35e21746d958d649b226a5857150d975d85403 aubert@math.cnrs.fr 2019-03-07 15:20:04
Adding a list of bug, and the sketch of a guide to contribute. 3a94b1fa49aee97001a45325f3f10f09475bf388 aubert@math.cnrs.fr 2019-03-07 15:14:38
Fixed the size of the images, added mention of the support of Affordable Learning Georgia. 485cf486d8d2b08427e5d02eda56d5b7a9495b38 aubert@math.cnrs.fr 2019-03-05 19:37:39
Finishing homework #5. e322bc945941083b0649557d9ad6bdbb8308c10c aubert@math.cnrs.fr 2019-03-04 15:10:15
Preparing homework 5 2b9ba64d1d36ae9f413929947cac5e6cddceb3dc aubert@math.cnrs.fr 2019-03-01 20:06:12
Commit 484c4a6372056106bae77e48b397c1174738fc26 - Correcting the chapter on db programming.
Author: aubert@math.cnrs.fr
Author date (UTC): 2019-04-04 18:57
Committer name: aubert@math.cnrs.fr
Committer date (UTC): 2019-04-04 18:57
Parent(s): 45881a9b8ab1e5a0071b58d72362059593365d52
Signing key:
Tree: e9acd1fd4ac3a15806fa47d2df52094cfb16d8c4
File Lines added Lines deleted
notes/code/java/FirstProg.java 44 0
notes/code/java/ProgWithErrorsPatched.java 38 0
notes/code/java/TestForNull.java 32 0
notes/lectures_notes.md 139 171
File notes/code/java/FirstProg.java added (mode: 100644) (index 0000000..fd5dac3)
1 // code/java/FirstProg.java
2
3 import java.sql.*;
4
5 public class FirstProg {
6 public static void main(String[] args) {
7 try (
8 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_EBOOKSHOP", "testuser","password");
9 /* If at execution time you receive an error that starts with
10 * "java.sql.SQLException: The server time zone value 'EDT' is unrecognized or
11 * represents more than one time zone. You must configure either the server ..."
12 * add ?serverTimezone=UTC at the end of the previous string,
13 * i.e., replace the previous line of code with:
14 * Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_EBOOKSHOP?serverTimezone=UTC", "testuser","password");
15 * cf. for instance https://stackoverflow.com/q/26515700
16 */
17 Statement stmt = conn.createStatement();
18 ) {
19 String strSelect = "SELECT title, price, qty FROM BOOKS WHERE qty > 40";
20 System.out.print("The SQL query is: " + strSelect + "\n");
21 ResultSet rset = stmt.executeQuery(strSelect);
22
23 System.out.println("The records selected are:");
24 int rowCount = 0;
25 String title;
26 double price;
27 int qty;
28
29 while(rset.next()) {
30 title = rset.getString("title");
31 price = rset.getDouble("price");
32 qty = rset.getInt("qty");
33 System.out.println(title + ", " + price + ", " + qty);
34 rowCount++;
35 }
36
37 System.out.println("Total number of records = " + rowCount);
38 conn.close();
39
40 } catch(SQLException ex) {
41 ex.printStackTrace();
42 }
43 }
44 }
File notes/code/java/ProgWithErrorsPatched.java added (mode: 100644) (index 0000000..9403ac4)
1 /* code/java/ProgWithErrorsPatched.java */
2
3 import java.sql.*;
4
5 public class ProgWithErrorsPatched {
6 public static void main(String[] args) {
7 try (
8 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
9 "HW_TestDB?user=testuser&password=password"); Statement stmt = conn.createStatement();
10 ) {
11
12 /* Errors after this point.*/
13
14 String strSelect = "SELECT title FROM DISKS WHERE qty > 40;";
15 ResultSet rset = stmt.executeQuery(strSelect); // Error 1
16
17 System.out.println("The records selected are: (listed last first):");
18 rset.last();
19
20 do { // Error 2
21 String title = rset.getString("title"); // Error 3
22 System.out.println(title); // Not an error, but we probably don't need two new lines.
23 } while (rset.previous()); // Error 2 bis
24
25 String sss = "SELECT title FROM DISKS WHERE Price <= ?";
26 PreparedStatement ps = conn.prepareStatement(sss);
27 ps.setInt(1, 10); // Error 4
28 ResultSet result = ps.executeQuery();
29
30 conn.close();
31
32 /* Errors before this point.*/
33
34 } catch (SQLException ex) {
35 ex.printStackTrace();
36 }
37 }
38 }
File notes/code/java/TestForNull.java added (mode: 100644) (index 0000000..4e122a5)
1 /* code/java/TestForNull.java */
2
3 import java.sql.*;
4
5 public class TestForNull {
6 public static void main(String[] args) {
7 try (
8 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DBPROG?user=testuser&password=password&createDatabaseIfNotExist=true&serverTimezone=UTC"); Statement stmt = conn.createStatement();
9 ) {
10 stmt.execute("CREATE TABLE Test (" +
11 "A CHAR(25), " +
12 "B INTEGER, " +
13 "C DOUBLE)");
14
15 String strAdd = "INSERT INTO Test VALUES (NULL, NULL, NULL);";
16 int number_of_row_changed = stmt.executeUpdate(strAdd);
17 System.out.print("This last query changed " + number_of_row_changed + " row(s).\n");
18
19 ResultSet result = stmt.executeQuery("SELECT * FROM Test");
20
21 if (result.next()) {
22 System.out.print(result.getString(1) + " " + result.getDouble(2) + " " + result.getInt(3));
23 if (result.getString(1) == null) {
24 System.out.print("\nAnd null for CHAR in SQL is null for String in Java.\n");
25 }
26 }
27 conn.close();
28 } catch (SQLException ex) {
29 ex.printStackTrace();
30 }
31 }
32 }
File notes/lectures_notes.md changed (mode: 100644) (index 5806c4f..8df2598)
... ... For a quick introduction to Java, cf. <http://spots.augusta.edu/caubert/teaching
5820 5820
5821 5821 ## A First Program ## A First Program
5822 5822
5823 ### The Java code
5824
5825 ~~~{.java .numberLines}
5826 // javac FirstProg.java
5827 // java -cp .:mysql-connector-java-5.1.44/mysql-connector-java-5.1.44-bin.jar FirstProg
5828 import java.sql.*;
5829
5830 public class FirstProg {
5831 public static void main(String[] args) {
5832 try (
5833 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_EBOOKSHOP", "testuser","password");
5834 Statement stmt = conn.createStatement();
5835 ) {
5836 String strSelect = "SELECT title, price, qty FROM books WHERE qty > 40";
5837 System.out.print("The SQL query is: " + strSelect + "\n");
5838 ResultSet rset = stmt.executeQuery(strSelect);
5839
5840 System.out.println("The records selected are:");
5841 int rowCount = 0;
5842 String title;
5843 double price;
5844 int qty;
5845
5846 while(rset.next()) {
5847 title = rset.getString("title");
5848 price = rset.getDouble("price");
5849 qty = rset.getInt("qty");
5850 System.out.println(title + ", " + price + ", " + qty);
5851 rowCount++;
5852 }
5853
5854 System.out.println("Total number of records = " + rowCount);
5855 conn.close();
5856
5857 } catch(SQLException ex) {
5858 ex.printStackTrace();
5859 }
5860 }
5861 }
5862 ~~~
5863
5864 A couple of comments:
5865
5866 - `java.sql.*` contains the classes `Connection`, `Statement`, `ResultSet`, `ResultSetMetadata`.
5867 - In the string `"jdbc:mysql://localhost:3306/HW_EBOOKSHOP"`, `jdbc` is the protocol, `mysql` is the subprotocol, `localhost` is the url of the database, `3306` is the port, and `HW_EBOOKSHOP` is the schema (that needs to already exist in this case).
5868 - Note that `strSelect` doesn't end with `;` (it could, but doesn't have to).
5869 - `next()` returns true if there is something left in the set of result, and move to the next line if it is the case. It is close to the code we would use to read from a file.
5870 - We could use `1`, `2`, and `3` instead of `"title"`, `"price"` and `"qty"` in the `while` loop: `getString`, `getDouble` and `getInt` also take integers, corresponding to the position of the attribute in the result set.
5823 We will write and compile a simple java program that manipulates a simple database.
5824 Even if the creation and population of the database could have been done from within the program, we will do it as a preliminary step, using the C.L.I., to make our program simpler.
5871 5825
5826 ### The database (`sql`)
5872 5827
5873 ### The database
5828 For this program, we will use the following database:
5874 5829
5875 5830 ```{.sqlmysql .numberLines include=code/sql/HW_EBOOKSHOP.sql} ```{.sqlmysql .numberLines include=code/sql/HW_EBOOKSHOP.sql}
5876 5831 ``` ```
5877 5832
5878
5879 5833 ~~~{.plain .numberlines} ~~~{.plain .numberlines}
5880 5834 MariaDB [HW_EBOOKSHOP]> SELECT * FROM books; MariaDB [HW_EBOOKSHOP]> SELECT * FROM books;
5881 5835 +------+----------------------------+--------------+-------+------+ +------+----------------------------+--------------+-------+------+
 
... ... MariaDB [HW_EBOOKSHOP]> SELECT * FROM books;
5890 5844 5 rows in set (0.00 sec) 5 rows in set (0.00 sec)
5891 5845 ~~~ ~~~
5892 5846
5847 You can copy and paste the code, then execute it, or use MySQL's batch mode: you can find the code previously given at `code/sql/HW_EBOOKSHOP.sql`, i.e., at <https://rocketgit.com/user/caubert/CSCI_3410/source/tree/branch/master/blob/notes/code/sql/HW_EBOOKSHOP.sql>.
5848 Open a terminal (or command-line interpreter), navigate to the folder where you stored that file (using `cd`), and type
5849
5850 ~~~{.bash}
5851 mysql -u testuser -p < HW_EBOOKSHOP.sql
5852 ~~~
5853
5854 for linux, or (something like)
5855
5856 ~~~{.bash}
5857 "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" -u testuser -p < HW_EBOOKSHOP.sql
5858 ~~~
5859
5860 for Windows.
5861
5862 You just discovered MySQL's batch mode, that perform *series* of instructions from a file.
5863 You can easily make sure that the database and the table were indeed created, and the values inserted, by logging the way you used to, and executing the usual commands.
5864
5865 ### Executing Database application
5866
5867 As we're about to see, a database application needs to be written following this order:
5868
5869 #. Load the API,
5870 #. Try to open the connection, using a try/catch statement.
5871 #. Perform the required actions on the database.
5872 #. Close the connection.
5873
5874 Of course, if the second step failed, then the program needs to exit gracefully, or to provide debugging information to the user.
5875 The program we will obtain can (normally) be compiled, using something like `javac FirstProg.java`{.bash} (or an equivalent command for windows).
5876 But another refinment is needed when you want to compile it.
5877 We need to set up the *driver* (or *connector*) to make the java `sql` API and MySQL communicate. To do so,
5878
5879 - Go to <https://dev.mysql.com/downloads/connector/j/>
5880 - Click on "Download" in front of "Platform Independent (Architecture Independent), ZIP Archive"
5881 - Look for the (somewhat hidden) "No thanks, just start my download."
5882 - Download the file named "mysql-connector-java-***.zip", where `***` is the version number.
5883 - Unzip the file, and locate the "mysql-connector-java-***-bin.jar" file.
5884 - Copy that file in the same folder as where you intend to write your program.
5885
5886 Once this is done and your program was compiled, you can run it using (where you replace `***` with the actual number, of course, e.g. `8.0.15`):
5887
5888 ~~~{.bash}
5889 java -cp .:mysql-connector-java-***-bin.jar FirstProg
5890 ~~~
5891
5892 in Linux, or
5893
5894 ~~~{.bash}
5895 java -cp .;mysql-connector-java-***-bin.jar FirstProg
5896 ~~~
5897
5898 in Windows.
5899 The `-cp` option lists the places where java should look for the class used in the program: we are explicitely asking java to use the `mysql-connector-java-***-bin.jar` executable to execute our `FirstProg` executable.
5900
5901 If we try to execute `FirstProg` without that flag, we obtain the following error message:
5902
5903 ~~~{.bash}
5904 $ java FirstProg
5905 java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/HW_EBOOKSHOP
5906 at java.sql.DriverManager.getConnection(DriverManager.java:689)
5907 at java.sql.DriverManager.getConnection(DriverManager.java:247)
5908 at FirstProg.main(FirstProg.java:9)
5909 ~~~
5910
5911 ### The application program (`java`)
5912
5913
5914 ```{.java .numberLines include=code/java/FirstProg.java}
5915 ```
5916
5917
5918 A couple of comments:
5919
5920 - `java.sql.*` contains the classes `Connection`, `Statement`, `ResultSet`, `ResultSetMetadata`.
5921 - In the string `"jdbc:mysql://localhost:3306/HW_EBOOKSHOP"`, `jdbc` is the protocol, `mysql` is the subprotocol, `localhost` is the url of the database, `3306` is the port, and `HW_EBOOKSHOP` is the schema (that needs to already exist in this case).
5922 - Note that `strSelect` doesn't end with `;` (it could, but doesn't have to).
5923 - `next()` returns true if there is something left in the set of result, and move to the next line if it is the case. It is close to the code we would use to read from a file.
5924 - We could use `1`, `2`, and `3` instead of `"title"`, `"price"` and `"qty"` in the `while` loop: `getString`, `getDouble` and `getInt` also take integers, corresponding to the position of the attribute in the result set.
5925
5926
5893 5927 ### The result ### The result
5894 5928
5895 5929 If you store the program in `FirstProg.java`, compile it, with If you store the program in `FirstProg.java`, compile it, with
 
... ... javac FirstProg.java
5901 5935 and then execute it, with and then execute it, with
5902 5936
5903 5937 ~~~{.bash} ~~~{.bash}
5904 java -cp .:mysql-connector-java-5.1.44/mysql-connector-java-5.1.44-bin.jar FirstProg
5938 java -cp .:mysql-connector-java-8.0.15.jar FirstProg
5905 5939 ~~~ ~~~
5906 5940
5907 5941 then you would obtain: then you would obtain:
 
... ... The records selected are:
5952 5986 ## Mapping Datatypes ## Mapping Datatypes
5953 5987
5954 5988 `SQL` | `JAVA` | `SQL` | `JAVA` |
5955 --- | ---
5989 :---: | :---:
5956 5990 `INTEGER` | `int` `INTEGER` | `int`
5957 5991 `CHARACTER(n)` | `String` `CHARACTER(n)` | `String`
5958 5992 `VARCHAR(n)` | `String` `VARCHAR(n)` | `String`
 
... ... For more details, consult <https://docs.oracle.com/javase/7/docs/api/java/sql/St
5982 6016
5983 6017 ## A Second Program ## A Second Program
5984 6018
6019 The program in [%D %n (%T)](#problem:Advanced_java) uses the modifications discussed below.
6020
5985 6021 ### Passing options ### Passing options
5986 6022
5987 6023 We can pass options when connecting to the database: We can pass options when connecting to the database:
5988 6024
5989 6025 ~~~{.java} ~~~{.java}
5990 6026 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DBPROG" Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DBPROG"
5991 + "?user=testuser"
5992 + "&password=password"
5993 + "&allowMultiQueries=true"
5994 + "&createDatabaseIfNotExist=true"
5995 + "&useSSL=true");
6027 + "?user=testuser"
6028 + "&password=password"
6029 + "&allowMultiQueries=true"
6030 + "&createDatabaseIfNotExist=true"
6031 + "&useSSL=true");
5996 6032 ~~~ ~~~
5997 6033
5998 6034 `createDatabaseIfNotExist` is about schema, actually. `createDatabaseIfNotExist` is about schema, actually.
 
... ... Compared to executing SQL statements directly, prepared statements have three ma
6028 6064
6029 6065 You can pass options when creating Statement objects to be able to read it both ways, and to be able to update rows. You can pass options when creating Statement objects to be able to read it both ways, and to be able to update rows.
6030 6066
6031 **COPY HOMEWORK HERE**
6032
6033 6067 ## Exercises {-} ## Exercises {-}
6034 6068
6035 6069 Exercise +.# Exercise +.#
 
... ... Exercise +.#
6060 6094 ~ ~
6061 6095
6062 6096 Explain this JDBC URL format: Explain this JDBC URL format:
6063
6064 ~~~{.java .numberLines}
6065 jdbc:mysql://localhost:3306/HW_NewDB?createDatabaseIfNotExist=true&useSSL=true
6066 ~~~
6097
6098 ~~~{.java .numberLines}
6099 jdbc:mysql://localhost:3306/HW_NewDB?createDatabaseIfNotExist=true&useSSL=true
6100 ~~~
6067 6101
6068 6102 Exercise +.# Exercise +.#
6069 6103
 
... ... Solution +.#
6099 6133
6100 6134 Solution +.# Solution +.#
6101 6135
6102 : It checks if there is data to read, and if move the cursor reads it.
6136 : It checks if there is data to read, and if there is, it moves the cursor to read it.
6103 6137 It returns a Boolean. It returns a Boolean.
6104 6138
6105 6139 Solution +.# Solution +.#
 
... ... Solution +.#
6128 6162
6129 6163 Solution +.# Solution +.#
6130 6164 ~ ~
6131
6132 The errors are the following:
6133
6134 - l. 13: The query is incorrect (`WHERE`{.sqlmysql} should come before `FROM`{.sqylmysql}),
6135 - TO be written
6165
6166 The errors are:
6167
6168 - line 16, `stmt.executeUpdate(strSelect)`{.java}: `executeUpdate`{.java} cannot be used to perform `SELECT`{.sqlmysql} statements.
6169 - line 21, this error is subtle: we need to display the last record before using `previous()`{.java}, otherwise it would be just skipped. We can fix this using a `do`...`while` loop.
6170 - line 22, `String title = rset.getDouble("title");`{.java}: `getDouble`{.java} returns a `double`{.java}, and hence cannot be stored as a `String`{.java}.
6171 - line 28, `ps.executeQuery()`{.java}: the prepared statement did not received a value for the `?` argument.
6172
6173 You can find the program patched in `code/java/ProgWithErrorsPatched.java`, the (relevant) diff. is:
6174
6175 ~~~~{.bash}
6176 16c16
6177 < ResultSet rset = stmt.executeUpdate(strSelect);
6178 ---
6179 > ResultSet rset = stmt.executeQuery(strSelect); // Error 1
6180 21,24c21,24
6181 < while(rset.previous()) {
6182 < String title = rset.getDouble("title");
6183 < System.out.println(title + "\n");
6184 < }
6185 ---
6186 > do { // Error 2
6187 > String title = rset.getString("title"); // Error 3
6188 > System.out.println(title); // Not an error, but we probably don't need two new lines.
6189 > }while(rset.previous()); // Error 2 bis
6190 27a28
6191 > ps.setInt(1, 10); // Error 4
6192 ~~~~
6136 6193
6137 6194 <!-- <!--
6138 6195 % %
 
... ... Solution +.#
6154 6211
6155 6212 Here it is: Here it is:
6156 6213
6157 ~~~{.java .numberLines}
6158 import java.sql.*;
6159
6160 public class NullProg {
6161 public static void main(String[] args) {
6162 try (
6163 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HW_DBPROG?user=testuser&password=password&createDatabaseIfNotExist=true");
6164 Statement stmt = conn.createStatement();
6165 ) {
6166 stmt.execute("CREATE TABLE Test (" +
6167 "A CHAR(25), " +
6168 "B INTEGER, " +
6169 "C DOUBLE)");
6170
6171 String strAdd = "INSERT INTO Test VALUES (NULL, NULL, NULL);";
6172 int number_of_row_changed = stmt.executeUpdate(strAdd);
6173 System.out.print("This last query changed " + number_of_row_changed + " row(s).");
6174
6175 ResultSet result = stmt.executeQuery("SELECT * FROM Test");
6176
6177 if (result.next()) {
6178 System.out.print(result.getString(1) + " " + result.getDouble(2) + " " + result.getInt(3));
6179 if (result.getString(1) == null){System.out.print("\nAnd null for CHAR in SQL is null for String in Java.\n");}
6180 }
6181 conn.close();
6182 } catch (SQLException ex) {
6183 ex.printStackTrace();
6184 }
6185 }
6186 }
6187 ~~~
6188
6189
6190 ## Problems
6191
6192 Problem (MySQL's batch mode and HW_EBOOKSHOP.sql) +.#mysql_batch
6193 ~
6194 6214
6195 In the archive, navigate to `code/sql/` and open `HW_EBOOKSHOP.sql`.
6196 Then, open a terminal (or command-line interpreter), navigate to the folder where you stored that file (using `cd`), and type
6197
6198 ~~~{.bash}
6199 mysql -u testuser -p < HW_EBOOKSHOP.sql
6200 ~~~
6201
6202 for linux, or (something like)
6203
6204 ~~~{.bash}
6205 "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" -u testuser -p < HW_EBOOKSHOP.sql
6206 ~~~
6207
6208 for Windows.
6209
6210 You just discovered MySQL's batch mode, that perform *series* of instructions from a file.
6211 You can easily make sure that the database and the table were indeed created, and the values inserted.
6212
6213 ---
6214
6215 Problem (First Database Application) +.#db_application
6216 ~
6217
6218 This exercise supposes you successfully completed @problem:mysql_batch.
6219 We will compile and execute your first database application, using Java and MySQL.
6220
6221 - I will assume that you have MySQL installed and set-up as indicated in Homeworks \#1 and \#2.
6222 - I will assume that you have Java installed. If not, please refer to <http://spots.augusta.edu/caubert/teaching/general/java/> for a simple program and the instructions to compile and execute it.
6223 - We need to set up the *driver* (or *connector*) to make the java `sql` API and MySQL communicate. To do so,
6224 - Go to <https://dev.mysql.com/downloads/connector/j/>
6225 - Click on "Download" in front of "Platform Independent (Architecture Independent), ZIP Archive"
6226 - Look for the (somewhat hidden) "No thanks, just start my download."
6227 - You will download a file named "mysql-connector-java-***.zip", where `***` is the version number.
6228 - Upon completion of the download, unzip the file, and locate the "mysql-connector-java-***-bin.jar" file.
6229 - Copy that file in `code/java/`.
6230 - Open a terminal in that same folder, and compile `FirstProg.java`, using `javac FirstProg.java`{.bash} (or an equivalent command for windows).
6231 Normally, nothing will be printed, but a `FirstProg.class` file will be created.
6232 - Now, execute that program, using
6233 ~~~{.bash}
6234 java -cp .:mysql-connector-java-***-bin.jar FirstProg
6235 ~~~
6236
6237 in Linux, or
6215 ```{.java .numberLines include=code/java/TestForNull.java}
6216 ```
6238 6217
6239 ~~~{.bash}
6240 java -cp .;mysql-connector-java-***-bin.jar FirstProg
6241 ~~~
6218 This program should display:
6219
6220 ~~~~{.bash}
6221 This last query changed 1 row(s).
6222 null 0.0 0
6223 And null for CHAR in SQL is null for String in Java.
6224 ~~~~
6242 6225
6243 in Windows.
6244 The `-cp` option lists the places where java should look for the class used in the program: we are explicitely asking java to use the `mysql-connector-java-***-bin.jar` executable to execute our `FirstProg` executable.
6245 Try to execute `FirstProg` without that flag, and see what happens.
6226 ## Problems
6246 6227
6247 6228 Problem (Advanced Java Programming) +.#Advanced_java Problem (Advanced Java Programming) +.#Advanced_java
6248 6229 ~ ~
6249 6230
6250 6231 Read, execute, break, edit, compile, patch, hack and (most importantly) understand the following program: Read, execute, break, edit, compile, patch, hack and (most importantly) understand the following program:
6251 6232
6252 ```{.sqlmysql .numberLines include=code/java/AdvancedProg.java}
6233 ```{.java .numberLines include=code/java/AdvancedProg.java}
6253 6234 ``` ```
6254 6235
6236 <!--
6255 6237 ## Solution to Selected Problems {-} ## Solution to Selected Problems {-}
6256
6257 Solution to @problem:db_application
6258 ~
6259
6260 Without the flag, we obtain:
6261
6262 ~~~{.bash}
6263 $ java FirstProg
6264 java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/HW_EBOOKSHOP
6265 at java.sql.DriverManager.getConnection(DriverManager.java:689)
6266 at java.sql.DriverManager.getConnection(DriverManager.java:247)
6267 at FirstProg.main(FirstProg.java:9)
6268 ~~~
6269
6270 ---
6238 -->
6271 6239
6272 6240 # A Bit About Security # A Bit About Security
6273 6241
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/caubert/CSCI_3410

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/caubert/CSCI_3410

Clone this repository using git:
git clone git://git.rocketgit.com/user/caubert/CSCI_3410

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main