gpt4 book ai didi

java - J2ME数据库更新问题

转载 作者:行者123 更新时间:2023-11-30 23:25:51 26 4
gpt4 key购买 nike

我正在尝试从我的移动应用程序更新 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com