提个简单的问题

ameny 2004-10-29 11:27:31
这里有没有JSP的高手啊,小弟刚学JSP,有个问题不懂,想请教

我从提交的表单中接收了个参数

String name = request.getParameter( "username" );

然后我想用这个参数来参加数据库的查询

String sql="select * from test where name="+name;

可是总是提示错误在这句语句上

哪位大哥帮个忙吧!
...全文
69 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
leoer 2004-10-30
  • 打赏
  • 举报
回复
String sql="select * from test where name='"+ name + "'";
这样就没有问题了,
不过~~~~


请教:
psmt=con.prepareStatement("select * from news where username=?");
//在第1个问号处加入参数,参数为String对象
psmt.setString(1,String对象);
这里不太明白,能说细点吗?
形印声传 2004-10-30
  • 打赏
  • 举报
回复
你的应该这么写就ok了:
String sql="select * from test where name='"+ name + "'";
Net8Java 2004-10-30
  • 打赏
  • 举报
回复
......~!
昏 ~~~~~


你看下面

private Connection con=null;
private PreparedStatement psmt=null;
private ResultSet rs=null;
private DBConnectionManager dbm=null;

//从数据库连接池得到一个连接
con=dbm.getConnection("sqlserver");
//定义一个问号
psmt=con.prepareStatement("select * from news where username=?");
//在第1个问号处加入参数,参数为String对象
psmt.setString(1,String对象);
//设置最多记录数
psmt.setMaxRows(10);
//执行查询语句
rs=psmt.executeQuery();
jinannanhai79 2004-10-30
  • 打赏
  • 举报
回复
HueVan(冰与火)说的对
至于 Net8Java(杜杜) 说的也是一种解决办法
形印声传 2004-10-30
  • 打赏
  • 举报
回复
这是两种不同的操作数据库的方法
prepareStatement是先写出sql语句的摸板,然后把变量用值代替
比如:psmt=con.prepareStatement("select * from news where username=? and pwd =?");
String name = "tom";
String password = "123";
psmt.setString(1,name);
psmt.setString(2,password);


就相当于一般的方法:
String sql = "select * from news where username='"+name+"' and pwd='"+password+"'";
db.executeQuery(sql);




81,092

社区成员

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

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