0
0

More than 1 year has passed since last update.

CallableStatement

Last updated at Posted at 2023-02-08

derbyにprocedure登録。https://qiita.com/Natukasi/items/23476095cc798d41fa58

public class ProcedureExample {
    public static void method(String pname) {
        try {
            Connection con = DriverManager.getConnection("jdbc:default:connection");
            String sql = "update firsttable set name = ? where id = 10";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1,pname);
            ps.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
}
public class Outer {
    public static void main(String[] args) {
        try {
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/data/Sample");
            try(con) {
                shokai(con);
                String proc = "CALL UPDATE_ITEM(?)";
                CallableStatement cs = con.prepareCall(proc);
                cs.setString(1,"change name");
                cs.execute();
                shokai(con);
            }
        } catch (SQLException ex ) {
            ex.printStackTrace();
        }
    }
    static void shokai(Connection con) {
        try {
            String sql = "select * from firsttable";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while(rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                System.out.println(id + ":" + name);
            }
        } catch (SQLException ex ) {
            ex.printStackTrace();
        }
    }
}
10:TEN
20:TWENTY
30:THIRTY
60:sixty
50:sixty
90:AAA
110:hundred ten
200:one hundred
210:hundred ten
10:change name
20:TWENTY
30:THIRTY
60:sixty
50:sixty
90:AAA
110:hundred ten
200:one hundred
210:hundred ten
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0