java - J2ME数据库更新问题

标签 java mysql java-me javabeans midlet

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

工作时间 (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

这里出了什么问题?

最佳答案

你应该调用 preparedStatement.executeUpdate();

看看你有什么

   ResultSet rs = ps.executeQuery() ;

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

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

preparedStatement.close();

关于java - J2ME数据库更新问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13396267/

相关文章:

java - 将图像加载到 jar 中

java - 泛型数组的resize方法

java-me:HttpConnection 无法写入 Instagram API 请求的正文

java - 使用Ant编译j2me

java - 无限的TimerTask方法调用

java - 带服务注入(inject)的 spring 链接构造函数

java - 格式化传递给Java函数的多个参数

php - 在 mysql_query 语句中绑定(bind)变量

php - 通过连接从表中选择数据

PHP 无法连接到我的 chrooted MySQL 套接字?