1

我正在尝试从我的移动应用程序更新 Mysql 数据库字段。

hoursworked (int11), JobStatus (VarChar45). 能够更新工作时间但不能更新工作状态。更新操作后,数据库字段显示为空。

if (action.equals("updatejob")){
        try {
             // Create a new connection.
             //String url = "http://webi:8080/app_sandra/queryDB.jsp";
             String url = "http://localhost:8080/A1electrics/updateJob.jsp";
             HttpConnection conn = (HttpConnection)Connector.open(url);
             // Set request type (done before streams are created).
             conn.setRequestMethod(HttpConnection.POST);
             // Send header info - must have for post to work.
             conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
             // Make sure connection closes once server sends response.
             conn.setRequestProperty("Connection", "close");
             // Create output stream.
             OutputStream ostream = conn.openOutputStream();
             // getting input from mobile screen for Job ID to update, hours worked and for Job status
             byte [] data = ("jobIDtoUpdate=" + jobIDtoUpdate.getString()).getBytes();
             ostream.write(data);
             data = ("&hoursworked=" + hoursworked.getString()).getBytes(); ostream.write(data);

             data = ("&JobStatus=" + JobStatus.getString()).getBytes(); ostream.write(data);
             // Close stream - once closed HTTP POST is created and sent
             ostream.close();
             // Retrieve response ok-200, not found -404, internal error- 500
             if (conn.getResponseCode() == HttpConnection.HTTP_OK) 
             {updateResult.setText("Successfully updated your data");}

             else if(conn.getResponseCode() == HttpConnection.HTTP_NOT_FOUND) {
                 // Bad request.
                 updateResult.setText("404 - Not Found");
             }
             else if(conn.getResponseCode() == HttpConnection.HTTP_INTERNAL_ERROR) {
                 // Internal error.
                 updateResult.setText("500 - Internal Error");
             }
        }
        catch (Exception e)
        {
            // Do nothing.
        }

public class updateBean {

    private Connection Conn;
    private String jobIDtoUpdate;
    private String JobStatus;
    private String hoursworked;
    Connection conn;

    public void updateData() throws ClassNotFoundException, SQLException {

        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        //establish connection
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/a1electric?user=root&password=raam030");

        //store the information from the user
        String jabID = this.jobIDtoUpdate;
        String hrwork = this.hoursworked;
        String jobstatus = this.JobStatus;

        // int Empno = Integer.parseInt(ID);
        // create a prepared statement to add the user input to
        //PreparedStatement pstmt = conn.prepareStatement("UPDATE employee SET JobStatus='Completed', HoursWorked='6' WHERE JobID="+this.jobIDtoUpdate);
        PreparedStatement pstmt = conn.prepareStatement("UPDATE employee SET JobStatus=?, HoursWorked=? WHERE JobID=?");
        pstmt.setString(1, jobstatus);
        pstmt.setString(2, hrwork);
        pstmt.setString(3, jabID);
        pstmt.execute();
        conn.close();

        //end insert data
    } // end insertBean

这里出了什么问题?

4

1 回答 1

0

你应该调用preparedStatement.executeUpdate();

看看你有什么

   ResultSet rs = ps.executeQuery() ;

    // Loop through the result set
    while( rs.next() ){
    // check your data
    }

并且不要忘记关闭preparedStatement 和HttpConnection;

准备好的语句.close();

于 2012-11-15T11:18:36.840 回答