2

MySQL ワークベンチで次の手順を作成しました。

create procedure city(in CID int, out CName varchar(35), out CPopulation int)
begin
    select name, Population into CName, CPopulation from city where ID = CID;
end//

以下は私のJavaコードです:

public class CallableStmts {

    public static void main(String[] args) {

        Connection con = null;
        CallableStatement st = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/world","root","password");
            String sql ="{call city(?,?,?)}";
            st = con.prepareCall(sql);
            int CID = 1;
            st.setInt(1, CID);
            st.registerOutParameter(2, Types.VARCHAR);
            st.registerOutParameter(3, Types.INTEGER);
            st.execute();   
            String name = st.getString(2);
            int Pop = st.getInt(3);
            System.out.printf("%s %d", name, Pop);
            st.close();
            con.close();
        }catch(Exception e)
        {
            System.out.println(e);  
        }finally
        {
            try{
                 if(st!=null)
                    st.close();
              }catch(SQLException se2){ }

              try{
                 if(con!=null)
                    con.close();
              }catch(SQLException se){
                 se.printStackTrace();
              }
        }
        System.out.println("Goodbye!");
    }
}

しかし、実行すると次のエラーが発生します。

java.sql.SQLException: パラメータ番号 2 は OUT パラメータではありません

callable ステートメントを -> に変更してもString sql ="{? = call city(?,?,?)}";

新しいエラーが発生します:

java.sql.SQLException: 格納された関数呼び出しの戻り値に IN パラメータを設定できません。

4

0 に答える 0