java MySQL结果集到java数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14761087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL resultset to java array
提问by Curious_Bop
I'm not doing very well with what I'm trying to do at the moment. The aim is to to get all information from a MySQL table and put it into an array. Once into an array I will use the priority queue in java and try to get it to work with that.... but that's not the issue I'm stuck on at the moment.
我目前正在做的事情做得不是很好。目的是从 MySQL 表中获取所有信息并将其放入数组中。一旦进入一个数组,我将使用 Java 中的优先级队列并尝试让它与它一起工作......但这不是我目前遇到的问题。
public static void array(String args[]) { // create priority queue
public static void array(String args[]) { // 创建优先队列
try {
String url = "jdbc:mysql://localhost:3306/project";
Connection conn = DriverManager.getConnection(url,"root","nbuser");
PreparedStatement stmt = conn.prepareStatement("SELECT user_id,s_date,e_date,d_date,department,projectname,projectapplication FROM booking");
ResultSet rs;
rs=stmt.executeQuery();
List<User> userList = new ArrayList<User>();
while(rs.next()) {
User user = new User(); //issue here with non-static variable
user.userid(rs.getString("user_id"));
user.s_date(rs.getObject("s_date"));
user.e_date(rs.getObject("e_date"));
user.d_date(rs.getObject("d_date"));
user.department(rs.getObject("department"));
user.projectname(rs.getObject("projectname"));
user.projectapplication(rs.getObject("projectapplication"));
user.priority(rs.getObject("priority"));
userList.add(user);
}
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
The aim is to get it to run through all the different sections in the table and populate them into an array, but I'm stuck on populating the array.
目的是让它遍历表中的所有不同部分并将它们填充到数组中,但我坚持填充数组。
Any help would be appreciated thanks.
任何帮助将不胜感激谢谢。
EDIT:
编辑:
public class User {
public String userid;
public String s_date;
public String e_date;
public String d_date;
public String department;
public String projectname;
public String projectapplication;
public int priority;
private void userid(String string) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void s_date(Object object) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void e_date(Object object) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void d_date(Object object) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void department(Object object) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void projectname(Object object) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void projectapplication(Object object) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void priority(Object object) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
similar to this?
类似于这个?
回答by ogzd
Create a User
class with the appropriate fields in the SQL table and keep each user in a list.
User
使用 SQL 表中的适当字段创建一个类,并将每个用户保留在列表中。
List<User> userList = new ArrayList<User>();
while(rs.next()) {
User user = new User();
user.setId(rs.getString("user_id"));
user.setStartDate(rs.getTimestamp("s_date");
....
userList.add(user);
}
return userList;
EDIT:
编辑:
class User {
private String id;
private Date startDate;
//other fields need to be declared as well as their setter methods
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public void setId(String id) {
this.id = id;
}
}
I would also recommend to take a look at JPA
and Hibernate
.
我还建议您查看JPA
和Hibernate
。