File notes/lectures_notes.md changed (mode: 100644) (index 5d231c5..fd13d62) |
... |
... |
Every database application follows the same routine: |
8509 |
8509 |
#. Terminate / close the connection with the DBMS. |
#. Terminate / close the connection with the DBMS. |
8510 |
8510 |
|
|
8511 |
8511 |
Which API is used vary with the pair Language / DBMS. |
Which API is used vary with the pair Language / DBMS. |
8512 |
|
Here are some of the most commonly used pairs: |
|
|
8512 |
|
Here are some of the most commonly used pairs for MySQL (that may be compatible with other DBMS in some cases): |
8513 |
8513 |
|
|
8514 |
8514 |
| Language | API | Website |
| Language | API | Website |
8515 |
8515 |
--- | ------- | ------------- |
--- | ------- | ------------- |
|
... |
... |
C# | MySQL Connector/Net | <https://dev.mysql.com/downloads/connector/net/8.0.ht |
8519 |
8519 |
Java | Java DataBase Connectivity | <https://docs.oracle.com/javase/9/docs/api/java/sql/package-summary.html> |
Java | Java DataBase Connectivity | <https://docs.oracle.com/javase/9/docs/api/java/sql/package-summary.html> |
8520 |
8520 |
|
|
8521 |
8521 |
In this chapter, we will _more precisely_ study how to develop a database application _coded in Java_ that uses _the Java DataBase Connectivity_ library. |
In this chapter, we will _more precisely_ study how to develop a database application _coded in Java_ that uses _the Java DataBase Connectivity_ library. |
|
8522 |
|
If you were to work with a different API in your future life, you would likely realize that most of what we will be studying remains true: reading the documentation and understanding the general strategy is what matters in this chapter, to build comfidence in your capacities. |
8522 |
8523 |
|
|
8523 |
8524 |
## Java's Way |
## Java's Way |
8524 |
8525 |
|
|
|
... |
... |
For a quick introduction to Java, cf. <https://spots.augusta.edu/caubert/teachin |
8542 |
8543 |
## A First Program |
## A First Program |
8543 |
8544 |
|
|
8544 |
8545 |
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>.]. |
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>.]. |
8545 |
|
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. |
|
|
8546 |
|
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 (and also because it generally match usage: schemas are usually created before the program is executed). |
8546 |
8547 |
|
|
8547 |
8548 |
### The Database (`SQL`) |
### The Database (`SQL`) |
8548 |
8549 |
|
|
|
... |
... |
at FirstProg.main(FirstProg.java:9) |
8642 |
8643 |
|
|
8643 |
8644 |
A couple of comments: |
A couple of comments: |
8644 |
8645 |
|
|
8645 |
|
- `java.sql.*`, whose documentation is at <https://docs.oracle.com/javase/8/docs/api/java/sql/package-summary.html>, contains the classes |
|
|
8646 |
|
- `java.sql.*`, whose documentation is at <https://docs.oracle.com/javase/8/docs/api/java/sql/package-summary.html>, contains the following classes that we will use in this chapter: |
8646 |
8647 |
- `DriverManager`, used for managing a set of JDBC drivers, |
- `DriverManager`, used for managing a set of JDBC drivers, |
8647 |
8648 |
- `Connection`, used to make a connection with a database via `DriverManager` objects, |
- `Connection`, used to make a connection with a database via `DriverManager` objects, |
8648 |
8649 |
- `Statement`, used to send basic `SQL` statements via `Connection` objects, |
- `Statement`, used to send basic `SQL` statements via `Connection` objects, |
8649 |
8650 |
- `ResultSet`, to retrieve and update the results of a query, returned by a `Statement` object, |
- `ResultSet`, to retrieve and update the results of a query, returned by a `Statement` object, |
8650 |
|
- `ResultSetMetadata`, to get information about the columns of a `ResultSet` object, |
|
|
8651 |
|
- `ResultSetMetadata`, to get information about a `ResultSet` object, |
8651 |
8652 |
- `SQLException`, a class of exceptions relative to `SQL`. |
- `SQLException`, a class of exceptions relative to `SQL`. |
8652 |
8653 |
|
|
8653 |
8654 |
- Intuitively, a `Connection` is a bridge (the physical connection), and `Statement` is a lane (a symbolic, or logic, path on the bridge). |
- Intuitively, a `Connection` is a bridge (the physical connection), and `Statement` is a lane (a symbolic, or logic, path on the bridge). |
|
... |
... |
A couple of comments: |
8659 |
8660 |
- `HW_EBOOKSHOP` is the schema (that needs to already exist in this case). |
- `HW_EBOOKSHOP` is the schema (that needs to already exist in this case). |
8660 |
8661 |
|
|
8661 |
8662 |
- Note that `strSelect` does not end with `;` (it could, but does not have to). |
- Note that `strSelect` does not end with `;` (it could, but does not have to). |
8662 |
|
- `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 ressembles what 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`: indeed, the cursor is "above" the first row of results when the `ResultSet` object is created. |
|
|
8663 |
|
- `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 ressembles what 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 |
|
8664 |
|
``` |
|
8665 |
|
java.sql.SQLException: Before start of result set |
|
8666 |
|
``` |
|
8667 |
|
Undeed, the cursor is "above" the first row of results when the `ResultSet` object is created. |
8663 |
8668 |
- We could use `1`, `2`, and `3` instead of `"title"`, `"price"` and `"qty"` in the `while` loop: the `getString`, `getDouble` and `getInt` are overloaded, and have versions that take one integer as input, 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: the `getString`, `getDouble` and `getInt` are overloaded, and have versions that take one integer as input, corresponding to the position of the attribute in the result set. |
8664 |
8669 |
|
|
8665 |
8670 |
|
|
|
... |
... |
But, actually, `SQL` and `JAVA` datatypes can be mapped as follows: |
8728 |
8733 |
`REAL` | `float` |
`REAL` | `float` |
8729 |
8734 |
`DOUBLE` | ` double` |
`DOUBLE` | ` double` |
8730 |
8735 |
`DECIMAL(t,d)` | `java.math.BigDecimal` |
`DECIMAL(t,d)` | `java.math.BigDecimal` |
8731 |
|
`DATE` | `java.sql.date` |
|
|
8736 |
|
`DATE` | `java.sql.Date` |
8732 |
8737 |
`BOOLEAN` | `boolean` |
`BOOLEAN` | `boolean` |
8733 |
8738 |
`BIT(1)` | `byte` |
`BIT(1)` | `byte` |
8734 |
8739 |
|
|
8735 |
|
(`DECIMAL(t,d)` was not previously introduced: the `t` stands for the number of digits, the `d` for the precision.) |
|
|
8740 |
|
Remember that in `DECIMAL(t,d)` the `t` stands for the number of digits, the `d` for the precision. |
8736 |
8741 |
|
|
8737 |
|
We cannot always have that correspondance: what would correspond to a reference variable? |
|
|
8742 |
|
However, we cannot always have a correspondance going the other way around (from Java to SQL): what would correspond to a reference variable? |
8738 |
8743 |
To a private attribute? |
To a private attribute? |
8739 |
8744 |
This series of problems is called "object-relational impedance mismatch", it can be overcomed, but at a cost. |
This series of problems is called "object-relational impedance mismatch", it can be overcomed, but at a cost. |
|
8745 |
|
We will come back to this in the [Presentation of NoSQL](#presentation-of-nosql) Chapter. |
8740 |
8746 |
|
|
8741 |
8747 |
|
|
8742 |
8748 |
## Differences Between `executeQuery`, `executeUpdate` and `execute` |
## Differences Between `executeQuery`, `executeUpdate` and `execute` |