Jsp中MySQL分页查询语句

qq_39208530 2017-11-18 10:02:20
public class GetCon { //得到Connection对象
public GetCon(){

}
public static Connection getCon(){
String url="jdbc:mysql://localhost/userdata";
String user="root";
String password="121609";
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver");

}catch(ClassNotFoundException e){
System.out.println("加载驱动器类出现错误");
}
try{
con=DriverManager.getConnection(url, user, password);
}catch(SQLException e){
System.out.println("SQL异常");
}
return con;
}
}
public class splitPage {//实现分页查询
private Connection con=null;
private Statement sta=null;
private ResultSet rs=null;
private ResultSetMetaData rsmd=null;
private String sql=null;
private int pageCount=0;
private int rowCount=0;
private int pageSize=0;
public splitPage(Connection con){
this.con=con;
}
public void initialize(String sql, int pageSize, int apage){
this.pageSize=pageSize;
this.sql=sql;
int arows=pageSize*(apage-1);
try{
sta=this.con.createStatement();
rs=sta.executeQuery(sql);
rsmd=rs.getMetaData();
if(rs!=null){
rs.last();
this.rowCount=rs.getRow();
}
rs.close();
this.pageCount=(this.rowCount-1)/pageSize+1;
this.sql=sql+"limit"+arows+","+pageSize;
}catch(SQLException e){
System.out.println(e.toString());
}
}
public Vector getPage(){
Vector v=new Vector();
try{
sta=this.con.createStatement();
rs=sta.executeQuery(sql);
rsmd=rs.getMetaData();
while(rs.next()){
String s[]=new String[rsmd.getColumnCount()];
for(int i=0; i<s.length; i++){
s[i]=rs.getString(i+1);
}
v.addElement(s);
}
rs.close();
sta.close();
}catch(SQLException e){
System.out.println(e.toString());
}
return v;
}
public int getpageCount(){
return this.pageCount;
}
public int getrowCount(){
return this.rowCount;
}
}
<body> //jsp内容
<%! int pageSize=3; %>
<% int showPage=1;
String sql="select * from student";
String str=request.getParameter("showPage");
splitPage pages=new splitPage(GetCon.getCon());
if(str!=null){
try{
showPage=Integer.parseInt(str);
if(showPage<1){
showPage=1;
}
}catch(ClassCastException e){
showPage=1;
}
}
pages.initialize(sql, pageSize, showPage);
Vector v=pages.getPage();
%>
<table border=1>
<tr>
<th>学号</th><th>姓名</th><th>地址</th><th>出生日期</th></tr>
<% for(int i=0; i<v.size(); i++){
String[] s=(String[])v.get(i);%>
<tr>
<td><%=s[0] %></td><td><%=s[1] %></td><td><%=s[2] %></td><td><%=s[3] %></td>
</tr>
<%}%>
</table>
<table>
<tr>
<td>共<%=pages.getrowCount() %>条</td><td><%=pageSize %>条/页</td><td>第<%=showPage %>页</td><td>共<%=pages.getpageCount() %>页</td>
</tr>
<tr>
<td><a href="MyJsp9.jsp?showPage=1">[首页]</a></td><td><a href="MyJsp9.jsp?showPage=<%=showPage-1%>">上一页</a></td><td><a href="MyJsp9.jsp?showPage=<%=showPage+1%>">下一页</a></td><td><a href="MyJsp9.jsp?showPage=<%=pages.getpageCount()%>">[尾页]</a></td>
</tr>
</table>
<form name=form method=get>
<select name=showPage>
<%for(int i=1; i<=pages.getpageCount(); i++){ %>
<option value=<%=i %>><%=i %></option>
<%} %>
</select>
<input type=submit name=submit value=提交>
</form>
</body>
错误:com.mysql.jdbc.exceptions.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 '3' at line 1
...全文
204 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39208530 2017-11-20
  • 打赏
  • 举报
回复
谢谢,弄好了
  • 打赏
  • 举报
回复
rs=sta.executeQuery(sql);在执行这句之前打印出sql看执行的语句是否正确,在代码中拼接sql字符串的时候要注意空格的问题
qq_39208530 2017-11-18
  • 打赏
  • 举报
回复
这是运行结果
qq_39208530 2017-11-18
  • 打赏
  • 举报
回复

10,608

社区成员

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

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