求救,关于数据库出错___急急急急

earthwind 2003-09-11 04:09:08
java.sql.SQLException: Can not issue data manipulation statements with executeQuery()

这是错误提示.

下面是部分代码:其中第一和第二个判断可以执行。
username = request[1];
passwd = request[2];
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
if (request[0].equalsIgnoreCase("login")) {
rs = stmt.executeQuery("select * from elstu_user WHERE username=" +
"\"" + username + "\" and password = " + "\"" +
passwd + "\"");
if (rs.next()) {
bw.write("successful:");
bw.flush(); //刷新缓冲区
}
else {
bw.write("error:");
bw.flush(); //刷新缓冲区
System.out.println("密码错误!");
}
}else if (request[0].equalsIgnoreCase("check")) {
rs = stmt.executeQuery("select money from elstu_user WHERE username=" +
"\"" + username + "\" and password = " + "\"" +
passwd + "\"");
if (rs.next()) {
bw.write("successful:" + rs.getInt("money"));
bw.flush(); //刷新缓冲区
}
else {
bw.write("error:");
bw.flush(); //刷新缓冲区
System.out.println("数据库没有数据!");
}
}else if (request[0].equalsIgnoreCase("drawing")) {
System.out.println("drawing!");
rs = stmt.executeQuery("UPDATE elstu_user SET money = money -\'" +
request[3] + "\' WHERE username=" + "\"" +
username + "\" and password = " + "\"" +
passwd + "\"");
if (rs != null) {
rs = stmt.executeQuery(
"INSERT INTO elstu_note (id, username, money, datetime) VALUES ('', '" +
username + "', '-" + request[3] + "', '')");
bw.write("successful:" + rs.getInt("money"));
bw.flush(); //刷新缓冲区
}
else {
bw.write("error:");
bw.flush(); //刷新缓冲区
System.out.println("数据库没有数据!");
}
}else if (request[0].equalsIgnoreCase("save")) {
rs = stmt.executeQuery("UPDATE elstu_user SET money = money +'" +
request[3] + "' WHERE username=" + "\"" +
username + "\" and password = " + "\"" +
passwd + "\"");
if (rs != null) {
rs = stmt.executeQuery(
"INSERT INTO elstu_note (id, username, money, datetime) VALUES ('', '" +
username + "', '" + request[3] + "', '')");
bw.write("successful:" + rs.getInt("money"));
bw.flush(); //刷新缓冲区
}
else {
bw.write("error:");
bw.flush(); //刷新缓冲区
System.out.println("数据库没有数据!");
}
}else if (request[0].equalsIgnoreCase("move")) {
rs = stmt.executeQuery("UPDATE elstu_user SET money = money +\"" +
request[3] + "\" WHERE username=" + "\"" +
request[4] + "\"");
if (rs != null)
rs = stmt.executeQuery("UPDATE elstu_user SET money = money -\"" +
request[3] + "\" WHERE username=" + "\"" +
username + "\" and password = " + "\"" +
passwd + "\"");
if (rs != null) {
// String time = new Date();
rs = stmt.executeQuery(
"INSERT INTO elstu_note (id, username, money, datetime) VALUES ('', '" +
username + "', '-" + request[3] + "', '')");
rs = stmt.executeQuery(
"INSERT INTO elstu_note (id, username, money, datetime) VALUES ('', '" +
request[4] + "', '" + request[3] + "', '')");
bw.write("successful:" + rs.getInt("money"));
bw.flush(); //刷新缓冲区
}
else {
bw.write("error:");
bw.flush(); //刷新缓冲区
System.out.println("数据库没有数据!");
}
}
...全文
33 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
earthwind 2003-09-11
  • 打赏
  • 举报
回复
谢谢各位,我第一次写java 程序。

OK,问题解决。
earthwind 2003-09-11
  • 打赏
  • 举报
回复
谢谢各位,我第一次写java 程序。我去试试。
noratong 2003-09-11
  • 打赏
  • 举报
回复
想来告诉你也晚了!楼上两位兄弟说的都是你错误的原因,你好像就是范了这两个错误。改过来应该就可以了。
junyi2003 2003-09-11
  • 打赏
  • 举报
回复
executeQuery
public ResultSet executeQuery(String sql)
throws SQLExceptionExecutes an SQL statement that returns a single ResultSet object.
Parameters:
sql - typically this is a static SQL SELECT statement
Returns:
a ResultSet object that contains the data produced by the given query; never null
Throws:
SQLException - if a database access error occurs
junyi2003 2003-09-11
  • 打赏
  • 举报
回复
executeUpdate
public int executeUpdate(String sql)
throws SQLExceptionExecutes an SQL INSERT, UPDATE or DELETE statement. In addition, SQL statements that return nothing, such as SQL DDL statements, can be executed.
Parameters:
sql - an SQL INSERT, UPDATE or DELETE statement or an SQL statement that returns nothing
Returns:
either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing
Throws:
SQLException - if a database access error occurs
junyi2003 2003-09-11
  • 打赏
  • 举报
回复
晕,SQL的更新语句是用excuteUpdate(String str)的。
你用excuteQuery(String str)当然要报错
earthwind 2003-09-11
  • 打赏
  • 举报
回复
谁能够帮我,加我QQ 125524319
steven_cheng 2003-09-11
  • 打赏
  • 举报
回复
你吧SQL语句中的"改成',就是不要用\",用',试一下。错误信息太简单,有没有整个堆栈的消息?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧