将MySQL值加载到java列表

时间:2016-05-23 16:37:37

标签: java mysql arraylist

我使用list来存储可变席位,但我得到了

 Syntax error on token ".", @ expected after this token`

在线

ps.setString(1, no);

代码

public List<String> checkSeat(String no) throws Exception // get all the seats
    {
        String sql = "SELECT selected_seat from movie_payment WHERE hall_number= ? ";
        List<String> seat = new ArrayList<>();
        DatabaseConnection db = new DatabaseConnection();
        try (Connection conn = db.getConnection();
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, no);
                ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                seat.add(rs.getString("selected_seat"));
            }
        }
        return seat;
    }

错误

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token ".", @ expected after this token
    Syntax error on token ",", . expected
    Syntax error, insert "SimpleName" to complete QualifiedName
    Syntax error, insert "VariableDeclaratorId = VariableInitializer" to complete Resource

    at gui.selectSeat.checkSeat(selectSeat.java:145)
    at gui.selectSeat.<init>(selectSeat.java:78)
    at gui.selectSeat.createAndShowGui(selectSeat.java:55)
    at gui.selectSeat.lambda$0(selectSeat.java:45)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

try-with-resources语句是一个声明一个或多个资源的try语句。 因此,您应该仅使用它来声明资源不使用它们。 你的代码应该是这样的:

    public List<String> checkSeat(String no) throws Exception {
          ...
              try (Connection conn = db.getConnection();
              PreparedStatement ps = conn.prepareStatement(sql);) 
              {
              ps.setString(1, no);
              ResultSet rs = ps.executeQuery()            
              while (rs.next()) {
                 seat.add(rs.getString("selected_seat"));
              }
           }
           return seat;
    }