-2

我想用来getParameter进入Strings 和s 并使用SQLint将它们放入数据库中。prepareStatement它给了我setString和的错误setInt

try {
    String id = request.getParameter("clientid");
    String cname = request.getParameter("clientname");
    String address = request.getParameter("address");
    String phonemodel= request.getParameter("phonemodel");
    String imei = request.getParameter("imei");
    String problem = request.getParameter("problem");
    String date2 = request.getParameter("date");
    String comments1= request.getParameter("comments");

    int  clientid = Integer.parseInt(id);
    int imeino  = Integer.parseInt(imei);
    // int date1 = Integer.parseInt(date2);

    Statement pstmt;

    Class.forName("org.apache.derby.jdbc.ClientDriver"); 
    Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app" , "app");

    pstmt=con.prepareStatement("Insert into movilapp(id,clientname,address,modle,imei,problem,date,comments) values(?,?,?,?,?,?,?,?)");
    pstmt.setInt(1,clientid);
    pstmt.setString(2,cname );
    pstmt.setString(3,address);
    pstmt.setString(4,phonemodel);
    pstmt.setInt(5,imeino);
    pstmt.setString(6,problem);
    pstmt.setString(7, comments1);
    pstmt.executeUpdate();
}
catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}
4

2 回答 2

3

您调用con.prepareStatement以获取 a PreparedStatement,但随后将其分配给 type 的变量Statement,因此 Java 不知道存在PreparedStatement-specific 方法setString

通过将PreparedStatementpstmt 的声明类型从

Statement pstmt;

PreparedStatement pstmt;
于 2013-07-25T23:03:07.937 回答
1

* IF 使用Statement那么它的语法是 *

 Statement stmt = null;
 stmt = conn.createStatement( );
 String sql = "UPDATE Employees set age=30 WHERE id=103";
 stmt.executeUpdate(sql);

因为PreparedStatement它是

PreparedStatement pstmt = null;
String SQL = "Update Employees SET age = ? WHERE id = ?";
pstmt = conn.prepareStatement(SQL);
psmt.setInt(1,30);
psmt.setInt(2,1003);
psmt.executeUpdate(SQL);

所以更改Statement pstmt;PreparedStatement pstmt = null;它会起作用。

于 2013-07-26T20:57:55.933 回答