View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.*;
19  
20  public class StoredProcedures {
21    public static void selectTwoSetsOfTwoAuthors(int p1, int p2, ResultSet[] rs1, ResultSet[] rs2) throws SQLException {
22      try (Connection conn = DriverManager.getConnection("jdbc:default:connection")) {
23        PreparedStatement ps1 = conn.prepareStatement("select * from author where id in (?,?)");
24        ps1.setInt(1, p1);
25        ps1.setInt(2, p2);
26        rs1[0] = ps1.executeQuery();
27        PreparedStatement ps2 = conn.prepareStatement("select * from author where id in (?,?)");
28        ps2.setInt(1, p2);
29        ps2.setInt(2, p1);
30        rs2[0] = ps2.executeQuery();
31      }
32    }
33  
34    public static void insertAuthor(int id, String username, String password, String email) throws SQLException {
35      try (Connection conn = DriverManager.getConnection("jdbc:default:connection")) {
36        PreparedStatement ps = conn.prepareStatement("INSERT INTO author (id, username, password, email) VALUES (?,?,?,?)");
37        ps.setInt(1, id);
38        ps.setString(2, username);
39        ps.setString(3, password);
40        ps.setString(4, email);
41        ps.executeUpdate();
42      }
43    }
44  
45    public static void selectAuthorViaOutParams(int id, String[] username, String[] password, String[] email, String[] bio) throws SQLException {
46      try (Connection conn = DriverManager.getConnection("jdbc:default:connection")) {
47        PreparedStatement ps = conn.prepareStatement("select * from author where id = ?");
48        ps.setInt(1, id);
49        ResultSet rs = ps.executeQuery();
50        rs.next();
51        username[0] = rs.getString("username");
52        password[0] = rs.getString("password");
53        email[0] = rs.getString("email");
54        bio[0] = rs.getString("bio");
55      }
56    }
57  }