com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您的SQL语法有错误;查看与MariaDB服务器版本对应的手册,了解在第1行"1"附近使用的正确语法
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/jsupermarket","root","");
String signString ="";
if (type == 1)//greater
{
signString = ">=" ;
System.out.print("1 selected n");
}
if (type == 2)//lesser
{
signString = "<=" ;
System.out.print("2 selected n");
}
if (type == 3)//equa;
{
signString = "=" ;
System.out.print("3 selected n");
}
String sql = "SELECT * FROM total WHERE totals ? ?;";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1,signString);
stmt.setString(2,amount);
ResultSet rs = stmt.executeQuery();
语句是根据在"0"之后替换的条件返回数据;其中";陈述我有语法错误,但我看不出问题,请帮忙
当接收到数据时,它保存在2D ARRAY中,然后返回到另一个称为"客户端"的类,从而将数据分散到JTable
*我正在使用RMI
主要问题是使用?
语法的参数绑定只能绑定SQL语句中的值。不能用于>=
、=
等操作员。
您需要使用字符串串联来构建查询字符串。正如Ksu在评论中指出的那样,分号也应该省略。
String sql = "SELECT * FROM total WHERE totals " + signString + " ?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1,amount);
您可能知道,通常不鼓励使用字符串串联来构建SQL查询,因为这可能会导致SQL注入漏洞。这是首选绑定参数的原因之一。
在这种情况下,signString
值完全由程序控制,恶意用户没有机会注入另一个值,在这里使用串联是安全的。
仍然建议在允许的地方使用绑定参数,如上面的示例所示。