所以目前我试图查询我的数据库并提取所有结果,使它们成为一个对象,然后将对象推送到对象数组中,我无法正常工作。
具有我的方法的工作对象如下
public class Table {
public List<Car> getTableConnection() throws ClassNotFoundException, SQLException {
Work work = new Work();
ResultSet result = null;
Statement sp = null;
Connection conn = null;
ResultSet rs = null;
List<Car> cars = new ArrayList<>();
try {
work.createConnection();
sp = conn.createStatement();
conn.setAutoCommit(false);
result = sp.executeQuery("SELECT * FROM cardetails");
while (result.next()) {
Car car = new Car();
car.setId(result.getInt("id"));
car.setYear(result.getString("Year"));
car.setMake(result.getString("Make"));
car.setModel(result.getString("Model"));
car.setColor(result.getString("Color"));
car.setAvailable(result.getString("Availability"));
cars.add(car);
}
} catch (Exception e) {
} finally {
}
return cars;
}
}
当我要从 JSP 上拉出这些对象时,我正在这样做。
<%
Table table = new Table();
table.getTableConnection();
List<Car> cars = table.getTableConnection();
String renting;
for (Car c : cars) {
int id = c.getId();
String year = c.getYear();
String make = c.getMake();
String model = c.getModel();
String color = c.getColor();
String avail = c.getAvailable();
}
%>
目前我没有收到任何错误,但在查询数据库时我得到的结果似乎什么也没有。
最佳答案
public class Table {
public List<Car> getTableConnection() throws ClassNotFoundException, SQLException {
Work work = new Work();
ResultSet result = null;
Statement sp = null;
Connection conn = null;
ResultSet rs = null;
List<Car> cars = new ArrayList<>();
try {
**conn =work.createConnection();**
sp = conn.createStatement();
conn.setAutoCommit(false);
result = sp.executeQuery("SELECT * FROM cardetails");
while (result.next()) {
Car car = new Car();
car.setId(result.getInt("id"));
car.setYear(result.getString("Year"));
car.setMake(result.getString("Make"));
car.setModel(result.getString("Model"));
car.setColor(result.getString("Color"));
car.setAvailable(result.getString("Availability"));
cars.add(car);
}
} catch (Exception e) {
} finally {
}
return cars;
}
}
关于java - 尝试查询我的数据库并将结果传递到一个对象中,然后传递到前面、mysql 和 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46835973/