jsp网页查询无记录,但是其他上午又可以查询,真的很急

muziziwang 2015-06-23 10:08:30
查询电影名称没有结果,查询其他的可以显示正常结果
chaxun1.jsp
%@ page language="java" pageEncoding="GB2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>查询条件提交页面</title>
<style type="text/css">
<!--
.STYLE5 {font-size: 24px}
-->
</style>
</head>
<body>
<div align="left"><span class="STYLE5"> 请选择查询条件</span> </div>
<hr width="100%" size="8">
<form action= "chaxun2.jsp" method="post"> 

<p align="left">
影片名称:
<input type="text" name="name" >
<br>
<br>
影片上映年份:
<input type="text" name="a1" >
<br>
<br>
影片产地:
<input type="text" name="country" >
<br>
<br>

<p align="left">影片类型:
<select name="leixing" >
<option>剧情</option><option selected="selected"></option>
<option>爱情</option>
<option>犯罪</option>

</select>
<br>
<br>


</p>
<p align="left">
<input type="submit" value="提 交">
       
<input type="reset" value="取 消">
</p>
</form>
</body></html>


chaxun2.jsp
<%@page contentType="text/html"pageEncoding="gb2312" import="java.sql.*"%>
<html>
<body>
<center>
<%String driverName = "com.mysql.jdbc.Driver"; //驱动程序名
String userName = "root"; //数据库用户名
String userPwd = "863125"; //密码
String dbName = "xuesheng"; //数据库名
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2 ="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url =url1+url2+url3; //形成带数据库读写编码的数据库连接字
Class.forName(driverName); //加载并为Mysql注册驱动
Connection conn=DriverManager.getConnection(url); //获取数据库连接
//接收表单数据
request.setCharacterEncoding("gb2312");//设置字符编码,避免出现乱码
String name=request.getParameter("name");
String year=request.getParameter("a1");
String country=request.getParameter("country");
String leixing=request.getParameter("leixing");

String sql="select * from movie where name=? or year=? or country=? or leixing=?";
PreparedStatement pstmt= conn.prepareStatement(sql);//定义数据库对象
pstmt.setString(1,name);
pstmt.setString(2,year); //给第一个参数赋值
pstmt.setString(3,country);
pstmt.setString(4,leixing);
ResultSet rs=pstmt.executeQuery(); //执行SQL语句
rs.last(); //移至最后一条记录
%>影片信息表中共有
<font size="5" color="red"> <%=rs.getRow()%></font>部电影
<table border="2" >
<tr align="center">
<td>编号</td> <td>影片名</td><td>年份</td><td>国 家</td><td>演员</td> <td>导演</td> <td>类型</td><td>评分</td> </tr>
<% rs.beforeFirst(); //移至第一条记录之前
while(rs.next()) { //判断是否还有下条记录
%> <tr align="center">
<td><%= rs.getString("number") %></td>
<td><%= rs.getString("name") %></td>
<td><%= rs.getString("year") %></td>
<td><%= rs.getString("country") %></td>
<td><%= rs.getString("yanyuan") %></td>
<td><%= rs.getString("daoyan") %></td>
<td><%= rs.getString("leixing") %></td>
<td><%= rs.getString("pingfen") %></td>
</tr>
<% }%>
</table>
</center>
<%if(rs!=null){ rs.close(); } //关闭数据记录集
if(pstmt!=null){ pstmt.close(); } //关闭数据库对象
if(conn!=null){ conn.close(); } //关闭数据连接
%>
</body>
</html>
...全文
173 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
muziziwang 2015-06-23
  • 打赏
  • 举报
回复
引用 11 楼 qq727013465 的回复:
引用 10 楼 muziziwang 的回复:
引用 9 楼 qq727013465 的回复:
[quote=引用 8 楼 muziziwang 的回复:] [quote=引用 7 楼 qq727013465 的回复:] 你名字 搜索 2 看下是否正常
jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where name like'%2%' or year='' or country='' or leixing=''' at line 1
like 后面 加 空格。。
谢谢了 可以了[/quote] 然后 中文的 也可以了么[/quote] 可以了
kobe8free 2015-06-23
  • 打赏
  • 举报
回复
引用 8 楼 muziziwang 的回复:
引用 7 楼 qq727013465 的回复:
你名字 搜索 2 看下是否正常
jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where name like'%2%' or year='' or country='' or leixing=''' at line 1
like 和 ''之间 应该有空格啊
悲鸣秋 2015-06-23
  • 打赏
  • 举报
回复
引用 10 楼 muziziwang 的回复:
引用 9 楼 qq727013465 的回复:
引用 8 楼 muziziwang 的回复:
[quote=引用 7 楼 qq727013465 的回复:] 你名字 搜索 2 看下是否正常
jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where name like'%2%' or year='' or country='' or leixing=''' at line 1
like 后面 加 空格。。
谢谢了 可以了[/quote] 然后 中文的 也可以了么
muziziwang 2015-06-23
  • 打赏
  • 举报
回复
引用 9 楼 qq727013465 的回复:
引用 8 楼 muziziwang 的回复:
引用 7 楼 qq727013465 的回复:
你名字 搜索 2 看下是否正常
jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where name like'%2%' or year='' or country='' or leixing=''' at line 1
like 后面 加 空格。。
谢谢了 可以了
悲鸣秋 2015-06-23
  • 打赏
  • 举报
回复
引用 8 楼 muziziwang 的回复:
引用 7 楼 qq727013465 的回复:
你名字 搜索 2 看下是否正常
jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where name like'%2%' or year='' or country='' or leixing=''' at line 1
like 后面 加 空格。。
muziziwang 2015-06-23
  • 打赏
  • 举报
回复
引用 7 楼 qq727013465 的回复:
你名字 搜索 2 看下是否正常
jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where name like'%2%' or year='' or country='' or leixing=''' at line 1
悲鸣秋 2015-06-23
  • 打赏
  • 举报
回复
你名字 搜索 2 看下是否正常
muziziwang 2015-06-23
  • 打赏
  • 举报
回复
引用 5 楼 qq727013465 的回复:
[quote=引用 4 楼 muziziwang 的回复:] [quote=引用 1 楼 qq727013465 的回复:] 查询电影名称 时出不来。是你用等号,没用模糊匹配。 修改如下 where name=? 改成 where name like ? pstmt.setString(1,name); 改成 pstmt.setString(1,“%”+name+“%”);
我修改后出现这个错误 javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where name like '%建国大业%' or year='' or country='' or leixing=''' at line 1 [/quote] 这个 乱码了。。[/qu那应该怎么改啦
悲鸣秋 2015-06-23
  • 打赏
  • 举报
回复
引用 4 楼 muziziwang 的回复:
[quote=引用 1 楼 qq727013465 的回复:] 查询电影名称 时出不来。是你用等号,没用模糊匹配。 修改如下 where name=? 改成 where name like ? pstmt.setString(1,name); 改成 pstmt.setString(1,“%”+name+“%”);
我修改后出现这个错误 javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where name like '%建国大业%' or year='' or country='' or leixing=''' at line 1 [/quote] 这个 乱码了。。
muziziwang 2015-06-23
  • 打赏
  • 举报
回复
引用 1 楼 qq727013465 的回复:
查询电影名称 时出不来。是你用等号,没用模糊匹配。 修改如下 where name=? 改成 where name like ? pstmt.setString(1,name); 改成 pstmt.setString(1,“%”+name+“%”);
我修改后出现这个错误 javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where name like '%建国大业%' or year='' or country='' or leixing=''' at line 1 org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779) org.apache.jsp.chaxun2_jsp._jspService(chaxun2_jsp.java:140) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) root cause com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where name like '%建国大业%' or year='' or country='' or leixing=''' at line 1 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) java.lang.reflect.Constructor.newInstance(Constructor.java:513) com.mysql.jdbc.Util.handleNewInstance(Util.java:406) com.mysql.jdbc.Util.getInstance(Util.java:381) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542) com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734) com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885) org.apache.jsp.chaxun2_jsp._jspService(chaxun2_jsp.java:82) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
乱世丶桃花 2015-06-23
  • 打赏
  • 举报
回复
<select name="leixing" > <option>剧情</option><option selected="selected"></option> <option>爱情</option> <option>犯罪</option> </select> 没写value也可以?
X元素 2015-06-23
  • 打赏
  • 举报
回复
这样的问题 一点点排除吧,首先保证数据库连接正常,然后根据查询sql 去数据库中执行一遍,
悲鸣秋 2015-06-23
  • 打赏
  • 举报
回复
查询电影名称 时出不来。是你用等号,没用模糊匹配。 修改如下 where name=? 改成 where name like ? pstmt.setString(1,name); 改成 pstmt.setString(1,“%”+name+“%”);

81,122

社区成员

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

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