com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException异常求助

dushiduxiao 2015-03-11 03:31:41
具体错误是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 '5 * from forum where id>0 order by id desc' at line 1
异常代码如下图:


分页代码如下:
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import domain.ForumInfoVo;
import util.ConnectionDB;
import util.PageList;

public class ForumDAO {
Connection conn=null;
private PageList pl=null;
//构造方法,获得数据库连接与分页公共类
public ForumDAO() throws ClassNotFoundException{
conn=(new ConnectionDB()).getConnection();
pl=new PageList();
}
//获得分页数目
public Integer pageSize(){
int maxRow= pl.pageSize("forum");
int pages = maxRow / 5;
if (maxRow % 5 > 0) {
pages = pages + 1;
return pages;
}
return pages;
}
//保存帖子
public void save(ForumInfoVo forum) throws SQLException{
try {
String sql = "insert into forum (title,content,ip,createtime,author_id,authorname) values(?,?,?,?,?,?)";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setString(1,forum.getTitle());
psmt.setString(2,forum.getContent());
psmt.setString(3,forum.getIp());
psmt.setString(4,forum.getCreatetime());
psmt.setInt(5, forum.getAuthor_id());
psmt.setString(6, forum.getAuthorname());
psmt.execute();
conn.commit();
} catch (Exception e) {
conn.rollback();
e.printStackTrace();
}
}
//查询所有帖子
public List<ForumInfoVo> findAll(int currentePage ) throws SQLException{
try {
ResultSet rs=pl.pageList(5,"forum",currentePage,"desc","id");
rs.beforeFirst();
List<ForumInfoVo> list=new ArrayList<ForumInfoVo>();
while (rs.next()){
ForumInfoVo f=new ForumInfoVo();
f.setId(rs.getInt("id"));
f.setTitle(rs.getString("title"));
f.setIp(rs.getString("ip"));
f.setCreatetime(rs.getString("createtime"));
f.setAuthor_id(rs.getInt("author_id"));
f.setAuthorname(rs.getString("authorname"));
f.setCountback(rs.getInt("countback"));
list.add(f);
}

return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;

}
//查看单个帖子
public ForumInfoVo findById(int id) throws SQLException{
try {
String sql = "select * from forum where id=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, id);
ResultSet rs=psmt.executeQuery();
rs.next();
ForumInfoVo f=new ForumInfoVo();
f.setId(rs.getInt("id"));
f.setTitle(rs.getString("title"));
f.setContent(rs.getString("content"));
f.setIp(rs.getString("ip"));
f.setCreatetime(rs.getString("createtime"));
f.setAuthor_id(rs.getInt("author_id"));
f.setAuthorname(rs.getString("authorname"));
f.setCountback(rs.getInt("countback"));
conn.commit();
rs.close();
return f;
} catch (Exception e) {
e.printStackTrace();
}
return null;

}
//计算回帖数目
public Integer countBack(int forum_id) throws SQLException{
try {
String sql = "select countback from forum where id=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1,forum_id);
ResultSet rs=psmt.executeQuery();
int coutback;
if(rs.next()){
coutback=rs.getInt("countback");
return coutback;
}
} catch (Exception e) {
//添加总回帖数错误
conn.rollback();
e.printStackTrace();
}

return 0;
}
//增加回帖数目
public Integer addCountBack(int countback,int forum_id) throws SQLException{
try {
String sql = "update forum set countback=? where id=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1,countback+1);
psmt.setInt(2, forum_id);
psmt.executeUpdate();

} catch (Exception e) {
conn.rollback();
e.printStackTrace();
}

return 0;
}
//减少回帖数目
public Integer removeCountBack(int countback,int forum_id) throws SQLException{
try {
String sql = "update forum set countback=? where id=?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1,countback-1);
psmt.setInt(2, forum_id);
psmt.executeUpdate();

} catch (Exception e) {
conn.rollback();
e.printStackTrace();
}

return 0;
}
//删除帖子
public boolean delete(int id) throws SQLException{
try {
String delreform="delete reforum where forum_id=?";
PreparedStatement psmt1 = conn.prepareStatement(delreform);
psmt1.setInt(1, id);
psmt1.executeUpdate();
String sql = "delete forum where id=?";
PreparedStatement psmt2 = conn.prepareStatement(sql);
psmt2.setInt(1, id);
int b=psmt2.executeUpdate();
conn.commit();
if(b!=0){
return true;
}else{
return false;
}
} catch (Exception e) {
conn.rollback();
e.printStackTrace();
}
return false;
}
}

...全文
1919 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dushiduxiao 2015-03-11
  • 打赏
  • 举报
回复
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import domain.ForumInfoVo; import util.ConnectionDB; import util.PageList; public class ForumDAO { Connection conn=null; private PageList pl=null; //构造方法,获得数据库连接与分页公共类 public ForumDAO() throws ClassNotFoundException{ conn=(new ConnectionDB()).getConnection(); pl=new PageList(); } //获得分页数目 public Integer pageSize(){ int maxRow= pl.pageSize("forum"); int pages = maxRow / 5; if (maxRow % 5 > 0) { pages = pages + 1; return pages; } return pages; } //保存帖子 public void save(ForumInfoVo forum) throws SQLException{ try { String sql = "insert into forum (title,content,ip,createtime,author_id,authorname) values(?,?,?,?,?,?)"; PreparedStatement psmt = conn.prepareStatement(sql); psmt.setString(1,forum.getTitle()); psmt.setString(2,forum.getContent()); psmt.setString(3,forum.getIp()); psmt.setString(4,forum.getCreatetime()); psmt.setInt(5, forum.getAuthor_id()); psmt.setString(6, forum.getAuthorname()); psmt.execute(); conn.commit(); } catch (Exception e) { conn.rollback(); e.printStackTrace(); } } //查询所有帖子 public List<ForumInfoVo> findAll(int currentePage ) throws SQLException{ try { ResultSet rs=pl.pageList(5,"forum",currentePage,"desc","id"); rs.beforeFirst(); List<ForumInfoVo> list=new ArrayList<ForumInfoVo>(); while (rs.next()){ ForumInfoVo f=new ForumInfoVo(); f.setId(rs.getInt("id")); f.setTitle(rs.getString("title")); f.setIp(rs.getString("ip")); f.setCreatetime(rs.getString("createtime")); f.setAuthor_id(rs.getInt("author_id")); f.setAuthorname(rs.getString("authorname")); f.setCountback(rs.getInt("countback")); list.add(f); } return list; } catch (Exception e) { e.printStackTrace(); } return null; } //查看单个帖子 public ForumInfoVo findById(int id) throws SQLException{ try { String sql = "select * from forum where id=?"; PreparedStatement psmt = conn.prepareStatement(sql); psmt.setInt(1, id); ResultSet rs=psmt.executeQuery(); rs.next(); ForumInfoVo f=new ForumInfoVo(); f.setId(rs.getInt("id")); f.setTitle(rs.getString("title")); f.setContent(rs.getString("content")); f.setIp(rs.getString("ip")); f.setCreatetime(rs.getString("createtime")); f.setAuthor_id(rs.getInt("author_id")); f.setAuthorname(rs.getString("authorname")); f.setCountback(rs.getInt("countback")); conn.commit(); rs.close(); return f; } catch (Exception e) { e.printStackTrace(); } return null; } //计算回帖数目 public Integer countBack(int forum_id) throws SQLException{ try { String sql = "select countback from forum where id=?"; PreparedStatement psmt = conn.prepareStatement(sql); psmt.setInt(1,forum_id); ResultSet rs=psmt.executeQuery(); int coutback; if(rs.next()){ coutback=rs.getInt("countback"); return coutback; } } catch (Exception e) { //添加总回帖数错误 conn.rollback(); e.printStackTrace(); } return 0; } //增加回帖数目 public Integer addCountBack(int countback,int forum_id) throws SQLException{ try { String sql = "update forum set countback=? where id=?"; PreparedStatement psmt = conn.prepareStatement(sql); psmt.setInt(1,countback+1); psmt.setInt(2, forum_id); psmt.executeUpdate(); } catch (Exception e) { conn.rollback(); e.printStackTrace(); } return 0; } //减少回帖数目 public Integer removeCountBack(int countback,int forum_id) throws SQLException{ try { String sql = "update forum set countback=? where id=?"; PreparedStatement psmt = conn.prepareStatement(sql); psmt.setInt(1,countback-1); psmt.setInt(2, forum_id); psmt.executeUpdate(); } catch (Exception e) { conn.rollback(); e.printStackTrace(); } return 0; } //删除帖子 public boolean delete(int id) throws SQLException{ try { String delreform="delete reforum where forum_id=?"; PreparedStatement psmt1 = conn.prepareStatement(delreform); psmt1.setInt(1, id); psmt1.executeUpdate(); String sql = "delete forum where id=?"; PreparedStatement psmt2 = conn.prepareStatement(sql); psmt2.setInt(1, id); int b=psmt2.executeUpdate(); conn.commit(); if(b!=0){ return true; }else{ return false; } } catch (Exception e) { conn.rollback(); e.printStackTrace(); } return false; } }
microhex 2015-03-11
  • 打赏
  • 举报
回复
ResultSet rs=pl.pageList(5,"forum",currentePage,"desc","id"); 把这代码贴出来啊。。。。
dushiduxiao 2015-03-11
  • 打赏
  • 举报
回复
怎么解决啊,代码用SQL server 2008没有错,用MySQL就出现这个错误。
tony4geek 2015-03-11
  • 打赏
  • 举报
回复
调试看看出现null 了额。
如若那年 2015-03-11
  • 打赏
  • 举报
回复
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 '5 * from forum where id>0 order by id desc' at line 1

81,122

社区成员

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

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