java DB ,derby数据库的select语句查询传递参数问题
自己开发的项目,连接数据库遇到的参数传递问题:
看问题://连接数据库的代码就省了
String name=getnameField().trim();//获取到的名字,现在要传递给sql语句
ResultSet rs1 = s.executeQuery("select name from friends where name="+name+"");//查询语句
出错提示:Column '飞鱼' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then '飞鱼' is not a column in the target table.
解释:这里的“飞鱼”就是通过name传递进去的参数,其他的语句都是没问题的,我用select name from friends where name='飞鱼' 测试过了,可以正常查询数据库,但是当把“飞鱼”用参数name传递进去的时候就有问题。试过方法1:ResultSet rs1 = s.executeQuery("select name from friends where name='"&name&"'"); 提示语法错误;试过方法2:ResultSet rs1 = s.executeQuery("select name from friends where name="@name@""); 提示语法错误。只有当用+号的时候,才不会提示语法错误。 求高手指教问题是什么。
------解决方案--------------------
ResultSet rs1 = s.executeQuery("select name from friends where name='"+name+"'");
这样试试,加了一对单引号!因为数据库中是要这样执行查询的!