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 

解决方案 »

  1.   

    怎么解决啊,代码用SQL server 2008没有错,用MySQL就出现这个错误。
      

  2.   

    ResultSet rs=pl.pageList(5,"forum",currentePage,"desc","id");
    把这代码贴出来啊。
      

  3.   

    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;
    }
    }