jsp 中用sql查询的问题
如何利用页面输入值进行数据库查询呢数据如何传递sql语句怎么写<%Connectionconn=null;try{Class.forName("com.mysql.jdb...
如何利用页面输入值进行数据库查询呢
数据如何传递
sql语句怎么写
<% Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String user = "root";
String password = "root";
String url="jdbc:mysql://127.0.0.1/myuser";
String na = request.getParameter("user");
String pa = request.getParameter("pwd");
conn = DriverManager.getConnection(url, "root", "root");
}catch (InstantiationException e){
String sql = "select * from test where password='pa' and name='na'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
out.println("恭喜你登录成功");
}
else
{out.println("登录失败");}
%>
老是登录失败 为神马?
数据库连接没有问题测试过了
sql语句那样写结果报错啊
pa cannot be resolved 展开
数据如何传递
sql语句怎么写
<% Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String user = "root";
String password = "root";
String url="jdbc:mysql://127.0.0.1/myuser";
String na = request.getParameter("user");
String pa = request.getParameter("pwd");
conn = DriverManager.getConnection(url, "root", "root");
}catch (InstantiationException e){
String sql = "select * from test where password='pa' and name='na'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
out.println("恭喜你登录成功");
}
else
{out.println("登录失败");}
%>
老是登录失败 为神马?
数据库连接没有问题测试过了
sql语句那样写结果报错啊
pa cannot be resolved 展开
3个回答
展开全部
(1)你的url字符串好像没有加上mysql数据库的访问端口吧……
String url="jdbc:mysql://127.0.0.1:3306/myuser";
(2)你的sql语句写的锋毁袜有错误,name、password都是变量……
String sql = "select * from test where password=' “+ pa +" ' and name=‘ “+ na +" ' ";
(3)看看你链接mysql的jar包是不是已经导入……
(4)如果还有错误,看看你的数据库myuser或者数据表test是否正确……
把“ String na = request.getParameter("user");
String pa = request.getParameter("pwd"银激);
”的定义移到“try”的前面去……
<% Connection conn = null;
String na = request.getParameter("user");
String pa = request.getParameter("pwd");
try {
Class.forName("com.mysql.jdbc.Driver"余嫌).newInstance();
String user = "root";
String password = "root";
String url="jdbc:mysql://127.0.0.1/myuser";
conn = DriverManager.getConnection(url, "root", "root");
}catch (InstantiationException e){}
String sql = "select * from test where password='pa' and name='na'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
out.println("恭喜你登录成功");
}
else
{out.println("登录失败");}
%>
String url="jdbc:mysql://127.0.0.1:3306/myuser";
(2)你的sql语句写的锋毁袜有错误,name、password都是变量……
String sql = "select * from test where password=' “+ pa +" ' and name=‘ “+ na +" ' ";
(3)看看你链接mysql的jar包是不是已经导入……
(4)如果还有错误,看看你的数据库myuser或者数据表test是否正确……
把“ String na = request.getParameter("user");
String pa = request.getParameter("pwd"银激);
”的定义移到“try”的前面去……
<% Connection conn = null;
String na = request.getParameter("user");
String pa = request.getParameter("pwd");
try {
Class.forName("com.mysql.jdbc.Driver"余嫌).newInstance();
String user = "root";
String password = "root";
String url="jdbc:mysql://127.0.0.1/myuser";
conn = DriverManager.getConnection(url, "root", "root");
}catch (InstantiationException e){}
String sql = "select * from test where password='pa' and name='na'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
out.println("恭喜你登录成功");
}
else
{out.println("登录失败");}
%>
展开全部
String ibmname = request.getParameter("bmname");
String ibmID = request.getParameter("bmID");
String ibmdw = request.getParameter("bmdw");
String ibmdz = request.getParameter("bmdz");
String ibmdh = request.getParameter("bmdh");
String ibmzy = request.getParameter("bmzy");
String ibmdj = request.getParameter("bmdj");
//上面是接受肢态神页闭桐面传递过来的参数
//下面是sql语句
String insertSql = "insert into wsbm(bmname,bmID,bmdw,bmdz,bmdh,bmzy,bmdj,add_time,site_id,pub_flag,cancel,hits ) values('"+ibmname+"','"+ibmID+"','"+ibmdw+"','"+ibmdz+"','"+ ibmdh+ "','"+ ibmzy+ "','"+ ibmdj+ "','"+add_time+"','"+site_id+"',0,0,1)";
System.out.println("insertSql----------------" + insertSql);
/历亏/下面是我自己执行sql语句的方法
DBManager.execute("mysql", insertSql, null, null);
String ibmID = request.getParameter("bmID");
String ibmdw = request.getParameter("bmdw");
String ibmdz = request.getParameter("bmdz");
String ibmdh = request.getParameter("bmdh");
String ibmzy = request.getParameter("bmzy");
String ibmdj = request.getParameter("bmdj");
//上面是接受肢态神页闭桐面传递过来的参数
//下面是sql语句
String insertSql = "insert into wsbm(bmname,bmID,bmdw,bmdz,bmdh,bmzy,bmdj,add_time,site_id,pub_flag,cancel,hits ) values('"+ibmname+"','"+ibmID+"','"+ibmdw+"','"+ibmdz+"','"+ ibmdh+ "','"+ ibmzy+ "','"+ ibmdj+ "','"+add_time+"','"+site_id+"',0,0,1)";
System.out.println("insertSql----------------" + insertSql);
/历亏/下面是我自己执行sql语句的方法
DBManager.execute("mysql", insertSql, null, null);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可是无法得出正确的结果,而且无法用out.println输出该变量
首先,是否报错? 是否抛出异常? 有的话贴出来
然后,String类型变量 是否有值? 用System.out.print(String类型变量),看看是否打印出结果? 有结果是否符合你页面输入值?
最后,request.getParameter()方法获得的是字符串类型,而你数据库中对应查询字段的类型是否是int或(oracle数据库number)? 建议把代码贴出来孙散,不要莫名纠结告御
还有就是 “yuewutao” 写则友氏是插入数据的代码,不要搞混了,插入数据返回值只是受影响行数,而不是查询结果
首先,是否报错? 是否抛出异常? 有的话贴出来
然后,String类型变量 是否有值? 用System.out.print(String类型变量),看看是否打印出结果? 有结果是否符合你页面输入值?
最后,request.getParameter()方法获得的是字符串类型,而你数据库中对应查询字段的类型是否是int或(oracle数据库number)? 建议把代码贴出来孙散,不要莫名纠结告御
还有就是 “yuewutao” 写则友氏是插入数据的代码,不要搞混了,插入数据返回值只是受影响行数,而不是查询结果
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询