JAVA - 查询 MS Access DB 后如何使用 ResultSet 的内容填充 JList
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());
}
}
应该做什么 - 该方法将从 AccList 表中查询 DB AccName 列,获取所有帐户名称并将它们插入到 Jlist 中
我需要什么帮助 - 我需要帮助实现 Jlist,该 Jlist 将填充 DB 列中的字段。
我尝试通过在线搜索帮助,最接近的解决方案就是我上面所做的。
我了解到有两种可能的方法
Resultset ->数组->吉利斯特 结果集->矢量-> Jlist
但是我也不知道如何进行这些转换。我想说我是一个JAVA新手。任何详细的帮助表示赞赏。
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
WHAT I NEED HELP WITH - I need help implementing a Jlist which will be filled with fields from a DB column.
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如你所说,使用数组或向量会比你的分割解决方案更干净、更容易。就我个人而言,我建议使用 Vector。基本上,您想要创建一个空向量,迭代结果集,通过 Vector.add(Object o) 方法将每个值添加到向量中,然后使用该向量在循环外部创建 jlist。像这样的东西(警告:未经测试)
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)