运行程序时出现这个奇怪的错误(java.sql.SQLSyntaxErrorException)。不幸的是,我没有找到100%相关的东西。任何帮助将不胜感激!我正在使用Derby db。
String id = request.getParameter("gid");
stmt = con.createStatement();
String strSql = "select img from guitar where gid="+id+" ";
rs = stmt.executeQuery(strSql);
答案 0 :(得分:0)
String id = request.getParameter("gid");
因此'id'是字符串:
String strSql = "select img from guitar where gid="+"'"+id+"'"+" ";
如果您认为id必须是整数,那么在传递之前将其转换为整数
方注意:你的代码很容易被sql注入,为什么不使用PreparedStatment并传递变量
答案 1 :(得分:0)
错误说SyntaxError
所以你可能在query
写错了。 getParameter
会返回string
。根据评论,您尝试将string
插入integer
列。尝试先解析它。
我认为您已经建立了对数据库的连接并获得响应,请尝试获取表中记录列表的select * from guitar
,如果您收到响应,请尝试直接写入在您的数据库中查询。 gid
必须是列的名称,也是img
,并尝试将数据库名称指定为[name].guitar
。否则,你的连接就会崩溃
您使用的是哪个.jar
?看看JDBC
答案 2 :(得分:0)
我使用以下方法修复了它:
String strSql = "select img from guitar where gid=" + Integer.parseInt(idString);
谢谢你的帮助!!