2012-04-12 61 views
0

我想创建一个MySQL服务器上我的netbeans的Servlet/JSP - 无法对SOAP Web服务

它的设计是一个非常简单的货币转换服务

支持的GlassFish服务器上一个简单的小Web服务访问数据库

这里是它应该做的

它需要的钱数(始终英镑)作为INT,并把它转换中作为一个字符串的货币。

该服务,然后从我的数据库表中查找该货币与像

select * from exchange.rates where currency = string 

然后进行简单的计算来钱转换成外币的查询,以获得转化率和返回量

的问题是,我不知道如何调用,转换率从我的MySQL服务器,我试了又试,但没有任何反应 我打的时候老是在我输入了相同的量。

我试图进入欧元和10 我设定的速度在我的数据库,但我刚刚10回来时,我测试的web服务

/** 
    * 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; 
    } 

回答

0

WHE你使用准备好的statemen你需要传递明确的参数:

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 
} 
+0

谢谢你! 实际上,我发现错误,我没有正确输入数据库名称大声笑 – kamil 2012-04-16 09:48:54