View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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  }