JAVA - 如何在查询 MS Access DB 后使用 ResultSet 的内容填充 JList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5304388/
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
JAVA - How to populate a JList with the contents of ResultSet after querying a MS Access DB
提问by Kunal
private void populateJlist(){
try
{
Connection connection = newConnection.createConnection();
Statement newStat = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet res = newStat.executeQuery("SELECT AccName FROM AccList");
String data = "";
while (res.next()){
data += res.getString("AccName") + " ";
}
String acclist[] = data.split(" ");
AccountList = new JList(acclist);
newStat.close();
connection.close();
}
catch(SQLException e){
System.err.println("SQLException: " + e.getMessage());
}
}
WHAT IS SHOULD DO - The method will query the DB AccName column from AccList Table and get all account names and insert them onto a Jlist
应该做什么 - 该方法将从 AccList 表中查询 DB AccName 列并获取所有帐户名称并将它们插入到 Jlist 中
WHAT I NEED HELP WITH - I need help implementing a Jlist which will be filled with fields from a DB column.
我需要什么帮助 - 我需要帮助实现一个 Jlist,它将填充来自 DB 列的字段。
I tried by searching online for help and the nearest to a solution was what I manage to do above.
我尝试通过在线搜索寻求帮助,最接近解决方案的是我在上面设法做的事情。
I learnt that there are 2 possible ways
我了解到有两种可能的方法
Resultset -> Array -> Jlist Resultset -> Vector -> Jlist
结果集 -> 数组 -> Jlist 结果集 -> 向量 -> Jlist
however I dont know how to do those conversions either. I would say I am a new beginner in JAVA. Any detailed help is appreciated.
但是我也不知道如何进行这些转换。我会说我是 JAVA 的新手。任何详细的帮助表示赞赏。
回答by Mark Tozzi
As you say, using either an array or a vector would be cleaner and easier than your split solution. Personally, I would suggest the Vector. Basically, you want to create an empty vector, iterate over the result set adding each value to the vector via the Vector.add(Object o)
method, then create the jlist outside the loop with the vector. Something like this (warning: untested)
正如您所说,使用数组或向量将比拆分解决方案更清晰、更容易。就个人而言,我建议使用 Vector。基本上,您要创建一个空向量,遍历结果集,通过该Vector.add(Object o)
方法将每个值添加到向量中,然后使用该向量在循环外创建 jlist。像这样的东西(警告:未经测试)
Vector<String> temp = new Vector<String>();
while (res.next()) {
temp.add(res.getString("AccName"));
}
Jlist acctList = new Jlist(temp);