File notes/code/sql/HW_EBOOKSHOP.sql changed (mode: 100644) (index 7b07a5b..e39ad21) |
1 |
1 |
/* code/sql/HW_EBOOKSHOP.sql */ |
/* code/sql/HW_EBOOKSHOP.sql */ |
2 |
2 |
|
|
|
3 |
|
-- DROP DATABASE HW_EBOOKSHOP; |
3 |
4 |
CREATE DATABASE HW_EBOOKSHOP; |
CREATE DATABASE HW_EBOOKSHOP; |
4 |
5 |
|
|
5 |
|
USE HW_eBOOKShop; |
|
|
6 |
|
USE HW_EBOOKSHOP; |
6 |
7 |
|
|
7 |
8 |
CREATE TABLE BOOKS ( |
CREATE TABLE BOOKS ( |
8 |
|
id int PRIMARY KEY, |
|
9 |
|
title varchar(50), |
|
10 |
|
author varchar(50), |
|
11 |
|
price float, |
|
12 |
|
qty int |
|
|
9 |
|
id INT PRIMARY KEY, |
|
10 |
|
title VARCHAR(50), |
|
11 |
|
author VARCHAR(50), |
|
12 |
|
price DECIMAL(10, 2), |
|
13 |
|
qty INT |
13 |
14 |
); |
); |
14 |
15 |
|
|
15 |
|
INSERT INTO BOOKS VALUES (1001, 'Java for dummies', 'Tan Ah Teck', 11.11, 11); |
|
16 |
|
INSERT INTO BOOKS VALUES (1002, 'More Java for dummies', 'Tan Ah Teck', 22.22, 22); |
|
17 |
|
INSERT INTO BOOKS VALUES (1003, 'More Java for more dummies', 'Mohammad Ali', 33.33, 33); |
|
18 |
|
INSERT INTO BOOKS VALUES (1004, 'A Cup of Java', 'Kumar', 44.44, 44); |
|
19 |
|
INSERT INTO BOOKS VALUES (1005, 'A Teaspoon of Java', 'Kevin Jones', 55.55, 55); |
|
|
16 |
|
/* Cf. https://en.wikipedia.org/wiki/List_of_best-selling_books */ |
|
17 |
|
|
|
18 |
|
INSERT INTO BOOKS VALUES (1, 'The Communist Manifesto', 'Karl Marx and Friedrich Engels', 11.11, 11); |
|
19 |
|
INSERT INTO BOOKS VALUES (2, 'Don Quixote', 'Miguel de Cervantes', 22.22, 22); |
|
20 |
|
INSERT INTO BOOKS VALUES (3, 'A Tale of Two Cities', 'Charles Dickens', 33.33, 33); |
|
21 |
|
INSERT INTO BOOKS VALUES (4, 'The Lord of the Rings', 'J. R. R. Tolkien', 44.44, 44); |
|
22 |
|
INSERT INTO BOOKS VALUES (5, 'Le Petit Prince', 'Antoine de Saint-Exupéry', 55.55, 55); |
20 |
23 |
|
|
21 |
24 |
SELECT * FROM BOOKS; |
SELECT * FROM BOOKS; |
File notes/code/sql/HW_FACULTY.sql changed (mode: 100644) (index 8157e45..27dd8ad) |
... |
... |
CREATE TABLE HW_FACULTY.PROF( |
25 |
25 |
PRIMARY KEY(Fname, Hiring) |
PRIMARY KEY(Fname, Hiring) |
26 |
26 |
); |
); |
27 |
27 |
|
|
|
28 |
|
/* |
|
29 |
|
* There are many other datatypes in SQL. |
|
30 |
|
* For monetary amount, you can use e.g. DECIMAL(10, 2) |
|
31 |
|
* cf. the discussion at |
|
32 |
|
* https://stackoverflow.com/q/4397302/ |
|
33 |
|
* and the documentation at |
|
34 |
|
* https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html |
|
35 |
|
*/ |
|
36 |
|
|
|
37 |
|
|
28 |
38 |
/* Or, instead of using the fully qualified name HW_FACULTY.PROF, we could have: |
/* Or, instead of using the fully qualified name HW_FACULTY.PROF, we could have: |
29 |
39 |
USE HW_FACULTY; |
USE HW_FACULTY; |
30 |
40 |
CREATE TABLE PROF(…) |
CREATE TABLE PROF(…) |
File notes/lectures_notes.md changed (mode: 100644) (index 8df2598..c4c3853) |
... |
... |
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 |
|
We will write and compile a simple java program that manipulates a simple database. |
|
|
5823 |
|
We will write and compile a simple java program that manipulates a simple database^[This program ows a lot to the one presented at <http://www.ntu.edu.sg/home/ehchua/programming/java/jdbc_basic.html>.] |
5824 |
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. |
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. |
5825 |
5825 |
|
|
5826 |
5826 |
### The database (`sql`) |
### The database (`sql`) |
|
... |
... |
at FirstProg.main(FirstProg.java:9) |
5917 |
5917 |
|
|
5918 |
5918 |
A couple of comments: |
A couple of comments: |
5919 |
5919 |
|
|
5920 |
|
- `java.sql.*` contains the classes `Connection`, `Statement`, `ResultSet`, `ResultSetMetadata`. |
|
|
5920 |
|
- `java.sql.*`, whose documentation is at <https://docs.oracle.com/javase/8/docs/api/java/sql/package-summary.html>, contains the classes |
|
5921 |
|
- `DriverManager`, used for managing a set of JDBC drivers, |
|
5922 |
|
- `Connection`, used to make a connection with a database via the `DriverManager`, |
|
5923 |
|
- `Statement`, used to send basic SQL statements via the `Connection`, |
|
5924 |
|
- `ResultSet`, to retriev and update the results of a query, returned by a `Statement` object, |
|
5925 |
|
- `ResultSetMetadata`, to get information about the columns of a `ResultSet` object, |
|
5926 |
|
- `SQLException`, a class of exceptions relative to `SQL`. |
|
5927 |
|
|
|
5928 |
|
- Intuitively, a `Connection` is a bridge (the physical connection), and `Statement` is a lane (a symbolic, or logic, path on the bridge). |
5921 |
5929 |
- 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). |
- 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 |
5930 |
- Note that `strSelect` doesn't end with `;` (it could, but doesn't have to). |
- 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. |
|
|
5931 |
|
- `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. If you try to use `getString` _before_ moving to the first row, you'll get an error like `java.sql.SQLException: Before start of result set`. |
5924 |
5932 |
- 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. |
- 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 |
5933 |
|
|
5926 |
5934 |
|
|