File notes/code/java/CallProcedure.java changed (mode: 100644) (index d772f1d..9526ec0) |
... |
... |
import java.sql.*; |
4 |
4 |
|
|
5 |
5 |
public class CallProcedure { |
public class CallProcedure { |
6 |
6 |
public static void main(String[] args) { |
public static void main(String[] args) { |
7 |
|
try ( |
|
8 |
|
Connection conn = |
|
|
7 |
|
try (Connection conn = |
9 |
8 |
DriverManager.getConnection( |
DriverManager.getConnection( |
10 |
9 |
"jdbc:mysql://localhost:3306/HW_CALL_TEST" |
"jdbc:mysql://localhost:3306/HW_CALL_TEST" |
11 |
10 |
+ "?user=testuser" |
+ "?user=testuser" |
|
... |
... |
public class CallProcedure { |
14 |
13 |
+ "&createDatabaseIfNotExist=true" |
+ "&createDatabaseIfNotExist=true" |
15 |
14 |
+ "&useSSL=true"); |
+ "&useSSL=true"); |
16 |
15 |
Statement stmt = conn.createStatement(); ) { |
Statement stmt = conn.createStatement(); ) { |
17 |
|
stmt.execute( |
|
18 |
|
"DROP SCHEMA IF EXISTS HW_CALL_TEST;" + "CREATE SCHEMA HW_CALL_TEST;" + "USE HW_CALL_TEST;"); |
|
19 |
|
|
|
20 |
16 |
stmt.execute( |
stmt.execute( |
21 |
|
"CREATE TABLE Test1 (" |
|
22 |
|
+ "A VARCHAR(25) PRIMARY KEY, " |
|
23 |
|
+ "B INTEGER);"); |
|
|
17 |
|
"DROP SCHEMA IF EXISTS HW_CALL_TEST;" |
|
18 |
|
+ "CREATE SCHEMA HW_CALL_TEST;" |
|
19 |
|
+ "USE HW_CALL_TEST;"); |
|
20 |
|
|
|
21 |
|
stmt.execute("CREATE TABLE Test1 (" + "A INT PRIMARY KEY);"); |
24 |
22 |
|
|
25 |
|
stmt.execute("INSERT INTO Test1 VALUES ('Bob', 1), ('Crystal', 2), ('Jack', 1);"); |
|
|
23 |
|
stmt.execute("INSERT INTO Test1 VALUES (1), (2), (3);"); |
26 |
24 |
// To create a procedure, we don't need to change the delimiter! |
// To create a procedure, we don't need to change the delimiter! |
27 |
25 |
// Cf. https://stackoverflow.com/a/5314879/ for instance. |
// Cf. https://stackoverflow.com/a/5314879/ for instance. |
28 |
|
stmt.execute(" CREATE PROCEDURE List () " |
|
29 |
|
+ " BEGIN " |
|
30 |
|
+ " SELECT * " |
|
31 |
|
+ " FROM Test1; " |
|
32 |
|
+ " END; "); |
|
33 |
|
// We create a CallabaleStatement object |
|
34 |
|
// https://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.html |
|
35 |
|
// that extends PreparedStatements and allow to call procedures. |
|
36 |
|
CallableStatement cs = conn.prepareCall("{call List}"); |
|
37 |
|
ResultSet rset = cs.executeQuery(); |
|
38 |
|
while(rset.next()){ |
|
39 |
|
System.out.println("The value of A is " + rset.getString("A") + " and the value of B is " + rset.getInt("B") + "."); |
|
40 |
|
} |
|
|
26 |
|
stmt.execute( |
|
27 |
|
" CREATE PROCEDURE List () " + " BEGIN " + " SELECT * " + " FROM Test1; " + " END; "); |
|
28 |
|
// We create a CallabaleStatement object |
|
29 |
|
// https://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.html |
|
30 |
|
// that extends PreparedStatements and allow to call procedures. |
|
31 |
|
CallableStatement cs = conn.prepareCall("CALL List()"); |
|
32 |
|
ResultSet rset = cs.executeQuery(); |
|
33 |
|
while (rset.next()) { |
|
34 |
|
System.out.println("The value of A is " + rset.getInt("A") + "."); |
|
35 |
|
} |
|
36 |
|
|
|
37 |
|
// Second example of procedure, with arguments |
|
38 |
|
stmt.execute( |
|
39 |
|
" CREATE PROCEDURE ListGreaterThan(arg INT) " |
|
40 |
|
+ " BEGIN " |
|
41 |
|
+ " SELECT * " |
|
42 |
|
+ " FROM Test1 " |
|
43 |
|
+ " WHERE A > arg; " |
|
44 |
|
+ " END; "); |
|
45 |
|
cs = conn.prepareCall("CALL ListGreaterThan(?)"); |
|
46 |
|
// Note that we use the same "?" placeholder |
|
47 |
|
// than we used for prepared statement. |
|
48 |
|
|
|
49 |
|
// We declare an int variable for the argument |
|
50 |
|
// for the sake of clarity, but don't need to. |
|
51 |
|
int x = 2; |
|
52 |
|
// We set the value of the first "? slot" |
|
53 |
|
// using setInt as for prepared statements. |
|
54 |
|
cs.setInt(1, x); // 1 is the position |
|
55 |
|
|
|
56 |
|
rset = cs.executeQuery(); |
|
57 |
|
System.out.println("The values of A greater than " + x + " are:"); |
|
58 |
|
while (rset.next()) { |
|
59 |
|
System.out.println("The value of A is " + rset.getInt("A") + "."); |
|
60 |
|
} |
41 |
61 |
|
|
42 |
62 |
conn.close(); |
conn.close(); |
43 |
63 |
} catch (SQLException ex) { |
} catch (SQLException ex) { |
File notes/code/java/TestingSemicolon.java changed (mode: 100644) (index f906c2a..2726383) |
... |
... |
public class TestingSemicolon { |
25 |
25 |
try (Connection conn = |
try (Connection conn = |
26 |
26 |
DriverManager.getConnection( |
DriverManager.getConnection( |
27 |
27 |
"jdbc:mysql://localhost:3306/?user=testuser&password=password"); // without + |
"jdbc:mysql://localhost:3306/?user=testuser&password=password"); // without + |
28 |
|
// "&allowMultiQueries=true" |
|
|
28 |
|
// "&allowMultiQueries=true" |
29 |
29 |
Statement stmt = conn.createStatement(); ) { |
Statement stmt = conn.createStatement(); ) { |
30 |
30 |
stmt.addBatch("DROP SCHEMA IF EXISTS HW_Testing_Semicolon"); |
stmt.addBatch("DROP SCHEMA IF EXISTS HW_Testing_Semicolon"); |
31 |
31 |
stmt.addBatch("CREATE SCHEMA HW_Testing_Semicolon"); |
stmt.addBatch("CREATE SCHEMA HW_Testing_Semicolon"); |