1
試圖將構建我的while循環的此對象推送到列表數組中。我無法準確弄清楚我做錯了什麼。有人可以先解釋一下,最好的語法是什麼,以及將這些信息推送到列表數組的最佳方法。所以後來我可以通過從中獲取信息來訪問列表數組。將語法推入內置到數組中的對象
public void getTableConnection() throws ClassNotFoundException,
SQLException {
List<Car>cars = new ArrayList<Car>();
Car car = new Car();
try {
Table table = new Table();
table.getTableConnection();
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/carrentalsystem";
String username = "root";
String password = "javatest";
String query = "select * from cardetails";
Connection conn = DriverManager.getConnection(url, username,
password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
car.setId(rs.getInt("id"));
car.setYear(rs.getString("Year"));
car.setMake(rs.getString("Make"));
car.setModel(rs.getString("Model"));
car.setColor(rs.getString("Color"));
car.setAvailable(rs.getString("Availability"));
String renting;
car.add()
}
return cars;
}catch(Exception e) {
}finally{
}
}
你可能要檢查一行'car.add()' –