java 从mysql表中获取元素到arraylist
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10595011/
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
getting elements from mysql table to arraylist
提问by Gipsy
i have an ArrayList that looks like this:
我有一个如下所示的 ArrayList:
[
1 2011-05-10 1 22.0,
2 2011-05-10 2 5555.0,
3 2011-05-11 3 123.0,
4 2011-05-11 2 212.0,
5 2011-05-30 1 3000.0,
6 2011-05-30 1 30.0,
7 2011-06-06 1 307.0,
8 2011-06-06 1 307.0,
9 2011-06-06 1 307.0,
10 2011-06-08 2 3070.0,
11 2011-06-03 2 356.0,
12 2011-05-10 2 100.0,
13 2011-05-30 1 3500.0,
14 2011-05-10 3 1000.0,
15 2011-05-10 3 1000.0,
16 2011-05-07 1 5000.0,
17 2011-05-07 4 500.0,
18 2011-08-07 3 1500.0,
19 2011-08-08 6 11500.0,
20 2011-08-08 4 11500.0,
21 2011-08-08 7 11500.0,
22 2011-06-07 8 3000.0]
Here is the code how i got this arraylist:
这是我如何获得这个数组列表的代码:
@Override
public ArrayList<Expenses> getExpenses() {
ArrayList<Expenses> expenses = new ArrayList<Expenses>();
try {
Statement stmt = myConnection.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM expenses");
while(result.next()){
Expenses expense = new Expenses();
expense.setNum(result.getInt(1));
expense.setPayment(result.getString(2));
expense.setReceiver(result.getInt(3));
expense.setValue(result.getDouble(4));
expenses.add(expense);
}
}
catch (SQLException e){
System.out.println(e.getMessage());
}
return expenses;
}
but what i want to get to an arraylist so that each element of array wasn't the line of the table (what i have now), but every individual element of the table should be the element of array ( [1, 2011-05-10, 1, 22.0, 2, 2011-05-10, 2, 5555.0, 3, 2011-05-11, 3, 123.0,]. Can anyone help me with that?
但是我想得到一个数组列表,以便数组的每个元素都不是表格的行(我现在拥有的),但是表格的每个单独元素都应该是数组的元素([1, 2011-05 -10, 1, 22.0, 2, 2011-05-10, 2, 5555.0, 3, 2011-05-11, 3, 123.0,]. 谁能帮我?
回答by kgiannakakis
The only way you could do add into an ArrayList elements of different type, will be to treat them as general objects. However the code you already have is much superior.
您可以添加到不同类型的 ArrayList 元素的唯一方法是将它们视为通用对象。但是,您已经拥有的代码要优越得多。
@Override
public ArrayList<Object> getExpenses() {
ArrayList<Object> expenses = new ArrayList<Object>();
try {
Statement stmt = myConnection.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM expenses");
while(result.next()) {
expenses.add(new Integer(result.getInt(1)));
expenses.add(result.getString(2));
expenses.add(new Integer(result.getInt(3)));
expenses.add(result.getDouble(4));
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
return expenses;
}
回答by NPE
Since you say in the comments that you're looking to get an ArrayList
of Strings
, the following should work:
由于您在评论中说您希望获得ArrayList
of Strings
,因此以下内容应该有效:
@Override
public ArrayList<String> getExpenses() {
ArrayList<String> expenses = new ArrayList<String>();
try {
Statement stmt = myConnection.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM expenses");
while (result.next()) {
expenses.add(result.getString(1));
expenses.add(result.getString(2));
expenses.add(result.getString(3));
expenses.add(result.getString(4));
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
return expenses;
}
The JDBC specmandates (in Appendix B) that most SQL types -- including all types that are used here -- can be retrieved as strings using Statement.getString()
.
在JDBC规范任务(见附录B),大多数SQL类型-包括这里使用的所有类型-可以作为使用字符串来检索Statement.getString()
。
P.S. It is considered good practice to close each JDBC statement as soon as you're done with it. This is commonly done in a finally
block (or in Java 7, using the try
-with-resources construct). See https://stackoverflow.com/a/10514079/367273
PS 完成后立即关闭每个 JDBC 语句被认为是一种很好的做法。这通常在finally
块中完成(或在 Java 7 中,使用try
-with-resources 构造)。见https://stackoverflow.com/a/10514079/367273
回答by Rizstien
@Override
public ArrayList<String> getExpenses() {
ArrayList<String> expenses = new ArrayList<String>();
try {
Statement stmt = myConnection.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM expenses");
while(result.next()){
String expense = null;
setExpense(result.getInt(1).toString());
expenses.add(getExpense());
setExpense(result.getString(2));
expenses.add(getExpense());
setExpense(result.getInt(3).toString());
expenses.add(getExpense());
setExpense(result.getDouble(4).toString());
expenses.add(getExpense());
}
}
catch (SQLException e){
System.out.println(e.getMessage());
}
return expenses;
}
// make getter setters of expense accordingly