/notes/code/java/ScrollingProgram.java (228d2f129fc8716d4f70735f4f9b05cfd92fcce6) (3789 bytes) (mode 100644) (type blob)
// code/java/ScrollingProgram.java
import java.sql.*;
public class ScrollingProgram {
public static void main(String[] args) {
try (Connection conn =
DriverManager.getConnection(
// We connect to the database, not to a particular schema.
"jdbc:mysql://localhost:3306/"
+ "?user=testuser"
+ "&password=password"
+ "&allowMultiQueries=true"
/*
* We want to allow multiple statements
* to be shipped in one execute() call.
*/
);
Statement stmt =
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
/*
* Finally, we want to be able to move back and forth in our
* ResultSets. This implies that we have to also chose if the
* ResultSets will be updatable or not: we chose to have them
* to be "read-only".
*/
) {
/*
* Before you ask: no, there are no "simple" way of
* constructing a string over multiple lines,
* besides concatenating them,
* cf. e.g. https://stackoverflow.com/q/878573
*/
stmt.execute(
"DROP SCHEMA IF EXISTS HW_SCROLLABLE_DEMO;"
+
/*
* We drop the schema we want to use if it already exists.
* (This allows to execute the same program multiple times.)
*/
"CREATE SCHEMA HW_SCROLLABLE_DEMO;"
+ "USE HW_SCROLLABLE_DEMO;"
+
// We create and use the schema.
"CREATE TABLE TEST("
+ " Id INT"
+ ");"
// The schema contains only one very simple table.
);
/*
* We can execute all those queries at once
* because we passed the "allowMultiQueries=true"
* token when we created the Connection object.
*/
// Let us insert some dummy values in this dummy table:
for (int i = 0; i < 10; i++) stmt.addBatch("INSERT INTO TEST VALUES (" + i + ")");
/*
* no ";" in the statements that we add
* to the batch!
*/
stmt.executeBatch();
// We execute the 10 statements that were loaded at once.
// Now, let us write a simple query, and navigate in the result:
ResultSet rs = stmt.executeQuery("SELECT * FROM TEST");
/*
* We select all the tuples in the table.
* If we were to execute this instruction on the
* command-line interface, we would get:
* MariaDB [HW_SCROLLABLE_DEMO]> SELECT * FROM TEST;
* +----+
* | Id |
* +----+
* | 0 |
* | 1 |
* | 2 |
* | 3 |
* | 4 |
* | 5 |
* | 6 |
* | 7 |
* | 8 |
* | 9 |
* +----+
* 10 rows in set (0.001 sec)
*/
// We can "jump" to the 8th result in the set:
rs.absolute(8);
System.out.printf("%-22s %s %d.\n", "After absolute(8),", "we are at Id", rs.getInt(1));
/* Note that this would display "7" since the
* 8th result contains the value 7 (sql starts
* counting at 1.
*/
// We can move back 1 item:
rs.relative(-1);
System.out.printf("%-22s %s %d.\n", "After relative(-1),", "we are at Id", rs.getInt(1));
// We can move to the last item:
rs.last();
System.out.printf("%-22s %s %d.\n", "After last(),", "we are at Id", rs.getInt(1));
// We can move to the first item:
rs.first();
System.out.printf("%-22s %s %d.\n", "After first(),", "we are at Id", rs.getInt(1));
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
Mode |
Type |
Size |
Ref |
File |
100644 |
blob |
15398 |
ee75155d2d99639acd17d31b2cc23cd752078e7e |
CONTRIB.md |
100644 |
blob |
20625 |
25b8e45e7f103089fb70fae5a219f09a29ef5312 |
KNOWN_BUGS.md |
100644 |
blob |
17217 |
e5c1f9f898cca948da42333b100e331d62b61d3c |
LICENSE.md |
100644 |
blob |
1997 |
f8801648fd4ba5843a2cbca8b10e4f69ba5d9b25 |
Makefile |
100644 |
blob |
6695 |
0b91924ffc7b73e2d36150369d4fd41a44b099c5 |
README.md |
040000 |
tree |
- |
eb7afc38251ada69e1967e1ce3e49967eca2267c |
install |
040000 |
tree |
- |
f16b283429b64b620b3bd7681a446ff54d504f84 |
notes |
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