gpt4 book ai didi

java - 将 JComboBox 与 SQL 链接

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:44 25 4
gpt4 key购买 nike

我正在编写一段代码,使用数据库中的数据在 JComboBox 中创建一个列表。它实际上是针对 POS 系统的,我必须从数据库的列表中选择一个项目..

这是我一直在尝试的代码:(我确实在没有 while(itemsList != null) 的情况下尝试过..但它也不起作用

private class ButtonHandlerSales implements ActionListener 
{
public final String userName = "root";
private final String password = "";
private final String serverName = "localhost";
private final int portNumber = 3306;
private final String dbName = "alphapos";



public void actionPerformed(ActionEvent action)
{
Connection conn = null;
try
{
conn = this.getConnection();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
//System.out.println("Connected to database");

while(itemsList != null)
{
String[] list= null;
String command = "SELECT itemName FROM item";

try
{
list = viewTable(conn, command);
}
catch (SQLException e)
{
e.printStackTrace();
}

itemList = new JComboBox(list);


}


}

private String[] viewTable(Connection con, String command) throws SQLException
{
String list[] = null;
Statement stmt = null;

try
{
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(command);

int i=0;
while (rs.next())
{
list[i] = rs.getString("itemName");
i++;
}

}

catch (SQLException e )
{
e.printStackTrace();
}

finally
{
if (stmt !=
null) { stmt.close(); }
}

return list;

}

private Connection getConnection() throws SQLException
{
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", this.userName);
connectionProps.put("password", this.password);

conn = DriverManager.getConnection("jdbc:mysql://"
+ this.serverName + ":" + this.portNumber + "/" + this.dbName,
connectionProps);

return conn;
}

}//end of class

我没有收到任何错误..代码编译..但我没有得到任何输出(列表为空)..我在这里做错了什么吗?

非常感谢任何帮助...(我不是在寻找直接代码)

最佳答案

问题 1

在您的viewTable方法中String list[] = null;。您永远不会使用 new String[..] 初始化它。因此,您将新建一个 NullPointerException。但最好使用 ArrayList,因为您可能不知道将返回多少个值

private List<String> viewTable(Connection con, String command) {
List<String> list = new ArrayList<>();
...
while (rs.next()) {
list.add(rs.getString("itemName"));
}
return list;
}

问题2

您正在 ActionListener 中创建 JComboBox。因此,您无法在执行操作之前添加组合。对于组合框,在处理数据时,最好使用其模型,而不是组件。 JComboBox 有一个 ComboBoxModel。我们可以使用具体的DefaultComboBoxModel 。您可以将数组传递给其构造函数。 List 具有方法 toArray,我们可以调用该方法从 List 创建数组。然后只需将数组传递给 DefaultComboBoxModel 构造函数并调用组合框 setModel

List<String> list = null;
String command = "SELECT itemName FROM item";

try {
list = viewTable(conn, command);
} catch (SQLException e) {
e.printStackTrace();
}

ComboBoxModel model = new DefaultComboBoxModel(list.toArray());
itemList.setModel(model);

这样,您可以在执行操作之前初始化组合框。

<小时/>

更新

如果您希望在应用程序启动时加载组合框,我不明白问题是什么。只需使用 actionPerformed 中的代码在您的类中创建一个方法即可。然后只需调用该方法,就像在主构造函数或其他东西中一样。也许您可以执行如下所示的操作,其中方法返回 ComboBoxModel 并且您可以使用它来设置组合框的模型

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Test {

private final JComboBox itemList;

public Test() throws SQLException {
itemList = new JComboBox(viewTable(getConnection(), command));
JButton button = new JButton("Populate");
button.addActionListener(new ButtonHandler());

JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(itemList);
panel.add(button);

JOptionPane.showMessageDialog(null, panel);
}

private class ButtonHandler implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
try {
ComboBoxModel model = viewTable(getConnection(), command);
itemList.setModel(model);
} catch (SQLException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

public static void main(String[] args) throws SQLException {
Test test = new Test();
}

private ComboBoxModel viewTable(Connection con, String command) throws SQLException {
List<String> list = new ArrayList<>();
try (Statement stmt = con.createStatement()) {
ResultSet rs = stmt.executeQuery(command);

while (rs.next()) {
list.add(rs.getString("itemName"));
}

} catch (SQLException e) {
e.printStackTrace();
}

return new DefaultComboBoxModel(list.toArray());

}

public final String userName = "root";
private final String password = "";
private final String serverName = "localhost";
private final int portNumber = 3306;
private final String dbName = "alphapos";
private final String command = "select itemName from item";

private Connection getConnection() throws SQLException {
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", this.userName);
connectionProps.put("password", this.password);

conn = DriverManager.getConnection("jdbc:mysql://"
+ this.serverName + ":" + this.portNumber + "/" + this.dbName,
connectionProps);

return conn;
}
}

关于java - 将 JComboBox 与 SQL 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26698351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com