gpt4 book ai didi

准备好的语句中的 MYSQL 异常在 '?' 附近使用正确的语法

转载 作者:行者123 更新时间:2023-11-29 02:22:05 26 4
gpt4 key购买 nike

<分区>

我写了下面的代码。但是我正在获取 MySQL 异常以在“?”附近使用正确的语法。你能帮我解决这个问题吗?

public static void inbox(String username) {
String query_recipient = "SELECT user_id, first_name, last_name FROM user_info WHERE username = '" + username + "' ";
String query = "SELECT sub, msg FROM message WHERE recipient = ?";
Statement s = null;
PreparedStatement ps = null;
ResultSet rs = null;
ResultSet rs1 = null;
int userId = 0;

try {
s = Connect.con.createStatement();
rs = s.executeQuery(query_recipient);
rs.next();
userId = rs.getInt("user_id");

ps = Connect.con.prepareStatement(query);
ps.setInt(1, userId);

rs1 = ps.executeQuery(query);

while (rs1.next()) {
System.out.println(rs1.getString("sub"));
System.out.println(rs1.getString("msg"));
}

} catch (SQLException ex) {
Logger.getLogger(UserBAL.class.getName()).log(Level.SEVERE, null, ex);
} //end catch
} //end inbox()

编辑:这是错误信息

SEVERE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2788)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2738)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1617)
at msg_pkg.MsgBAL.inbox(MsgBAL.java:82)
at msg_pkg.MsgBAL.main(MsgBAL.java:116)

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