Java 将值从数据库解析到ComboBox中
嗯,我需要从数据库中获取值,并将它们插入组合框中进行选择 听起来很简单,只需使用两个类,UI类和实体类即可,其中包含所有SQL查询(与数据库有关的都在其中): 现在,上述输出工作正常,没有任何故障。在我的表单中,组合框在我指定的列中显示从数据库中获取的唯一值 当在其中实现另一层时,问题就来了 简而言之,我现在有三节课 类1:UI->该类仅处理UI 类2:Controller->该类纯粹运行方法 Class 3:Entity->该类纯粹运行与sql数据库查询有关的任何内容 我所做的是将上述代码修改为: 这是UI类:Java 将值从数据库解析到ComboBox中,java,sql,eclipse,swing,combobox,Java,Sql,Eclipse,Swing,Combobox,嗯,我需要从数据库中获取值,并将它们插入组合框中进行选择 听起来很简单,只需使用两个类,UI类和实体类即可,其中包含所有SQL查询(与数据库有关的都在其中): 现在,上述输出工作正常,没有任何故障。在我的表单中,组合框在我指定的列中显示从数据库中获取的唯一值 当在其中实现另一层时,问题就来了 简而言之,我现在有三节课 类1:UI->该类仅处理UI 类2:Controller->该类纯粹运行方法 Class 3:Entity->该类纯粹运行与sql数据库查询有关的任何内容 我所做的是将上述代码修改
//Declare Variables
JComboBox comboBoxName = new JComboBox();
Controller ct = new Controller();
comboBoxName.addItem(ct.fillComboBox());
以及控制器类中的某个方法:
//Declare Variables
Entity et = new Entity();
public String fillComboBox(){
return et.takeNames();
}
最后是我的实体类,它包含了所有的sql查询
//Declare all variables first
Connection conn = null;
String task = null, names = null;
String query;
//This method connects to database
//There's nothing wrong with this method, I just placed it here to give a general overview of what this method exactly is for you to understand, as I will be calling it out later. Yes, I removed off the **URL** portion intentionally.
public static Connection dbConnector(){
try{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:URL");
JOptionPane.showMessageDialog(null, "Connected!");
return conn;
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);
return null;
}
}
public String takeNames(){
try{
conn = dbConnector();
query = "select distinct Name from DbTable order by Name";
PreparedStatement pst = conn.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next()){
//shows Name data in combobox
names = rs.getString("Name");
}
pst.close();
}
catch(Exception ex){
ex.printStackTrace();
}
return names;
}
基本上,这个“新”实现的运行方式是,UI类调用控制器类,它调用实体类,该实体类在内部运行方法,并将值解析回UI
这种方法很有用,因为它将程序的不同部分分开,使其看起来更整洁。遗憾的是,这是一个令人头痛的问题
现在,这里的错误是,它只检索1个值,而不是多个值。它所检索的是我指定的特定列中的第一个“distinct”值。其余的“不同”值将被忽略 我有预感这一切都与rs设置有关,@:
ResultSet rs = pst.executeQuery();
我的想法是,它只接受1个值并设置它,然后忽略其余的值。有人对此有什么解决办法吗?我尝试了arraylist,但在如何在arraylist中存储大量rs值方面失败了(这真的让我很困惑>)
我为这篇冗长的文章表示歉意,但在我被困在这一部分数小时之前,我已经尽了最大努力
public ArrayList<String> takeNames(){
//This will collect all names from db
ArrayList<String> names = new ArrayList<>();
try{
conn = dbConnector();
query = "select distinct Name from DbTable order by Name";
PreparedStatement pst = conn.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next()){
//shows Name data in combobox
//Add data to the array list
names.add(rs.getString("Name"));
}
pst.close();
}
catch(Exception ex){
ex.printStackTrace();
}
//Return the array list you created
return names;
}
public ArrayList<String> fillComboBox(){
return et.takeNames();
}
JComboBox comboBoxName = new JComboBox();
Controller ct = new Controller();
ArrayList<String> nameList = ct.fillComboBox();
for(String name : nameList){
comboBoxName.addItem(name);
}
public ArrayList takeNames(){
//这将从数据库中收集所有名称
ArrayList name=新的ArrayList();
试一试{
conn=dbConnector();
query=“按名称从DbTable order中选择不同的名称”;
PreparedStatement pst=conn.prepareStatement(查询);
ResultSet rs=pst.executeQuery();
while(rs.next()){
//在组合框中显示名称数据
//将数据添加到数组列表中
Name.add(rs.getString(“Name”));
}
pst.close();
}
捕获(例外情况除外){
例如printStackTrace();
}
//返回您创建的数组列表
返回姓名;
}
修改fillComboBox(),如下所示
public ArrayList<String> takeNames(){
//This will collect all names from db
ArrayList<String> names = new ArrayList<>();
try{
conn = dbConnector();
query = "select distinct Name from DbTable order by Name";
PreparedStatement pst = conn.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next()){
//shows Name data in combobox
//Add data to the array list
names.add(rs.getString("Name"));
}
pst.close();
}
catch(Exception ex){
ex.printStackTrace();
}
//Return the array list you created
return names;
}
public ArrayList<String> fillComboBox(){
return et.takeNames();
}
JComboBox comboBoxName = new JComboBox();
Controller ct = new Controller();
ArrayList<String> nameList = ct.fillComboBox();
for(String name : nameList){
comboBoxName.addItem(name);
}
public ArrayList fillComboBox(){
return et.takeNames();
}
然后修改其余的,如下所示
public ArrayList<String> takeNames(){
//This will collect all names from db
ArrayList<String> names = new ArrayList<>();
try{
conn = dbConnector();
query = "select distinct Name from DbTable order by Name";
PreparedStatement pst = conn.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while(rs.next()){
//shows Name data in combobox
//Add data to the array list
names.add(rs.getString("Name"));
}
pst.close();
}
catch(Exception ex){
ex.printStackTrace();
}
//Return the array list you created
return names;
}
public ArrayList<String> fillComboBox(){
return et.takeNames();
}
JComboBox comboBoxName = new JComboBox();
Controller ct = new Controller();
ArrayList<String> nameList = ct.fillComboBox();
for(String name : nameList){
comboBoxName.addItem(name);
}
JComboBox comboxname=newjcombobox();
控制器ct=新控制器();
ArrayList nameList=ct.fillComboBox();
用于(字符串名称:名称列表){
comboBoxName.addItem(名称);
}
你可以做一些事情,比如在顶部插入你面对的问题的图像吗。那是一堵文字墙,伙计。人们越早了解这个问题,你就越有可能得到答案。一幅画抵得上1000个字!我指定的那个专栏——要具体……程序员不需要对你是如何编写你的应用程序发表评论。他们只需要看到现在的代码。他们可以理解代码。如果您提供样本数据表以及您得到的与您想要的,这将澄清问题!好吧,这些评论基本上不是给我的,这是我的讲师希望我拥有的=/在UI中声明一个数组并将其存储在其中,嗯。好主意!这成功了,谢谢!哦,对不起!我不知道我必须这么做。再次为迟到的事道歉!