我正在創(chuàng)建一個簡單的應(yīng)用程序,它使用 JDBC 從 MySQL 獲取數(shù)據(jù)。我使用 dao 從數(shù)據(jù)庫中獲取數(shù)據(jù)。除了一個以外,其他都工作正常(所有 DAO 的代碼都相同)。我也手動提交 INSERT 和 UPDATE 方法。即使我手動設(shè)置隔離級別 read committed,Workbench 也會返回有效結(jié)果。JDBCSessionDao 創(chuàng)建方法:public void create(Session session) throws SQLException{ try(PreparedStatement ps = conn.prepareStatement(INSERT_SESSION)){ conn.setAutoCommit(false); LocalTime start = session.getStartTime(); LocalTime end = session.getEndTime(); System.out.println(start + ", " + end); System.out.println(Time.valueOf(start) + ", " + Time.valueOf(end)); ps.setTime(1, Time.valueOf(start)); ps.setTime(2, Time.valueOf(end)); ps.setDate(3, Date.valueOf(session.getDate())); ps.setLong(4, session.getMovieId()); ps.executeUpdate(); conn.commit(); conn.setAutoCommit(true); } catch (SQLException e){ logger.error(e.getMessage()); conn.rollback(); } }JDBCSessionDao findByDate 方法public List<Session> findByDate(LocalDate date) { List<Session> sessions = new ArrayList<>(); SessionMapper mapper = new SessionMapper(); try(PreparedStatement ps = conn.prepareStatement(SELECT_BY_DATE_ORDER_BY_TIME_ASC)){ ps.setDate(1, Date.valueOf(date)); ResultSet rs = ps.executeQuery(); System.out.println(rs.getFetchSize()); while(rs.next()){ Session s = mapper.extractFromResultSet(rs); sessions.add(s); } } catch (SQLException e){ logger.error(e.getMessage()); } return sessions;}詢問:String SELECT_BY_DATE_ORDER_BY_TIME_ASC = "SELECT * FROM sessions WHERE session_date=? ORDER by start_time ASC";JDBCDaoFactory getConnection() 方法:private Connection getConnection(){ String url = "jdbc:mysql://localhost:3306/cinemajee?useLegacyDatetimeCode=false&serverTimezone=Europe/Kiev"; String user = "root"; String password = "root";
2 回答

茅侃侃
TA貢獻1842條經(jīng)驗 獲得超22個贊
我忘記更改 SessionMapper 中的列名稱,它們是用駝峰式(例如 sessionId)編寫的,但我的數(shù)據(jù)庫列是蛇形(例如 session_id)。

白板的微信
TA貢獻1883條經(jīng)驗 獲得超3個贊
嘗試修改代碼中的查詢。也許session_date
參數(shù)不起作用。所以從這個改變:
"SELECT * FROM sessions WHERE session_date=? ORDER by start_time ASC"'
對此:
"SELECT * FROM sessions ORDER by start_time ASC LIMIT 5"'
添加回答
舉報
0/150
提交
取消