0

NetBeans上のmysqlサーバーに支えられたGlassfishサーバー上に簡単な小さなWebサービスを作成しようとしています。

非常にシンプルな通貨換算サービスとして設計されています

これがその予定です

INTとしての金額(常にGBp)と、文字列として変換するための通貨が必要です。

次に、サービスはデータベーステーブルからその通貨を検索し、次のようなクエリでコンバージョン率を取得します。

select * from exchange.rates where currency = string

次に、簡単な計算を実行してお金を通貨に変換し、金額を返します

問題は、mysqlサーバーからその変換率を呼び出す方法がわからないことです。試してみましたが、何も起こらず、入力したのと同じ量を取得し続けます。

ユーロを入力してみましたが、データベースでそのレートを設定しましたが、Webサービスをテストしたときに10が戻ってきました。

 /**
     * Web service operation
     */
    @WebMethod(operationName = "convert")
    public int convert(@WebParam(name = "currency") String currency, @WebParam(name = "amount") int amount) {
        int newamount = 0;
        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con =
                    DriverManager.getConnection("jdbc:mysql://localhost:3306/exhange",
                    "root", "25587");
            PreparedStatement st =
                    con.prepareStatement("select * from rates where currency = '" + currency+"'");

            ResultSet rs = null;
            rs = st.executeQuery();
 rs.first();
            newamount =rs.getInt("conversion") * amount;

 return newamount;
        } catch (Exception e) {
            System.out.println("error");
        }

        return amount;
    }
4

1 に答える 1

0

準備されたステートマンを使用する場合は、パラメーターを明示的に渡す必要があります。

PreparedStatement st = con.prepareStatement("select * from rates where currency = ?");
st.setString(1,currency) //Check this
ResultSet rs = st.executeQuery();

// If you are sure that is only one row then you nee to do
String columnX = null;
if (rs.next() != null) {
   columnX = rs.getString(1); //Where 1 is the first column
}
于 2012-04-12T12:41:33.377 に答える