1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.databases.blog;
17
18 import java.sql.Connection;
19 import java.sql.DriverManager;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23
24 public class StoredProcedures {
25 public static void selectTwoSetsOfTwoAuthors(int p1, int p2, ResultSet[] rs1, ResultSet[] rs2) throws SQLException {
26 try (Connection conn = DriverManager.getConnection("jdbc:default:connection")) {
27 PreparedStatement ps1 = conn.prepareStatement("select * from author where id in (?,?)");
28 ps1.setInt(1, p1);
29 ps1.setInt(2, p2);
30 rs1[0] = ps1.executeQuery();
31 PreparedStatement ps2 = conn.prepareStatement("select * from author where id in (?,?)");
32 ps2.setInt(1, p2);
33 ps2.setInt(2, p1);
34 rs2[0] = ps2.executeQuery();
35 }
36 }
37
38 public static void insertAuthor(int id, String username, String password, String email) throws SQLException {
39 try (Connection conn = DriverManager.getConnection("jdbc:default:connection")) {
40 PreparedStatement ps = conn
41 .prepareStatement("INSERT INTO author (id, username, password, email) VALUES (?,?,?,?)");
42 ps.setInt(1, id);
43 ps.setString(2, username);
44 ps.setString(3, password);
45 ps.setString(4, email);
46 ps.executeUpdate();
47 }
48 }
49
50 public static void selectAuthorViaOutParams(int id, String[] username, String[] password, String[] email,
51 String[] bio) throws SQLException {
52 try (Connection conn = DriverManager.getConnection("jdbc:default:connection")) {
53 PreparedStatement ps = conn.prepareStatement("select * from author where id = ?");
54 ps.setInt(1, id);
55 ResultSet rs = ps.executeQuery();
56 rs.next();
57 username[0] = rs.getString("username");
58 password[0] = rs.getString("password");
59 email[0] = rs.getString("email");
60 bio[0] = rs.getString("bio");
61 }
62 }
63
64 private StoredProcedures() {
65 }
66 }