jsp, MySql Bulletin Select Query Questions

Asked 2 years ago, Updated 2 years ago, 34 views

I'm working on the part where I look up the entire list of articles with jsp and mysql

I thought it was a simple task to change the code that Oracle already made once to mysql, but I'm having a hard time because there's a little different part

public ArrayList<BoardDto> getBoardList(HashMap<String, Object> listOpt) {
        ArrayList<BoardDto> list = new ArrayList<BoardDto>();

        String opt = (String)listOpt.get("opt");
        int start = (Integer)listOpt.get("start");

        try {
            conn = ConnUtil.getConnection();
            StringBuffer sql = new StringBuffer();

            if(opt == null) {
                sql.append("select * from");
                sql.append(" (select rownum as rnum, date.* from");
                sql.append(" (select board_num, board_id, board_subject,");
                sql.append(" board_content, board_file, board_count,");
                sql.append(" board_re_ref, board_parent, board_date");
                sql.append(" from board a");
                sql.append(" date)");
                sql.append(" where rnum >= ? and rnum <= ?");

                System.out.println(sql);

                pstmt = conn.prepareStatement(sql.toString());
                pstmt.setInt(1, start);
                pstmt.setInt(2, start+9);

                sql.delete(0, sql.toString().length());
            } 
 ...

Turning the cord like that

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 'date) where rnum >= 1 and rnum <= 10' at line 1

I got an sql syntax error...

When sql statements are printed,

select * from (select rownum as rnum, date.* from (select board_num, board_id, board_subject, board_content, board_file, board_count, board_re_ref, board_parent, board_date from board a date) where rnum >= ? and rnum <= ?

It's like this

I put in a after the from board because I need to add an alias, is that right?

Is there an error in the where door?

MySql is too hard <

jsp mysql

2022-09-21 10:06

1 Answers

The error itself is because the grammar of from board a is wrong. If you look at the query as a whole... I don't know why rnum was introduced. I think it's probably because of paging, but I think there's only one table that I'm actually looking at board

Take this opportunity to use limit offset. Would you like to try if the code below works?

sql.append("SELECT * FROM board ORDERS LIMIT ? OFFSET ?");

pstmt.setInt(1, 10);
pstmt.setInt(2, start); // However, always start %10 == 0


2022-09-21 10:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.