Java 如何用数据库中的值填充组合框?
如何插入带有值的组合框从数据库 我想从数据库中选择并添加到组合框中 我有两门课: 构造函数数据库()第一类Java 如何用数据库中的值填充组合框?,java,mysql,swing,sqlexception,Java,Mysql,Swing,Sqlexception,如何插入带有值的组合框从数据库 我想从数据库中选择并添加到组合框中 我有两门课: 构造函数数据库()第一类 //constructeur Database() { void remplir_Jcomb() { Connection conn = null; Statement st = null; String rq1 = "SELECT region FROM rg"
//constructeur
Database()
{
void remplir_Jcomb() {
Connection conn = null;
Statement st = null;
String rq1 = "SELECT region FROM rg";
String rq2 = "SELECT ACTELS FROM rg";
// - Paramètres de connexion à la base de données
String url = "jdbc:mysql://localhost/réseau";
String login = "root";
String password = "";
String driver = "com.mysql.jdbc.Driver";
try {
//comboBox_gouver.removeAllItems();
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,login,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
st = conn.createStatement();
ResultSet rs1 = st.executeQuery(rq1);
ResultSet rs2 = st.executeQuery(rq2);
while ((rs1.next())&&(rs2.next())) {
comboBox_gouver.addItem(rs1.getString(1));
comboBox_ACTELS.addItem(rs2.getString(1));
}
st.close();
rs1.close();
rs2.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();}
}
}
接口swing第二类,包含两个JComboBox
调用构造函数数据库()
private Database BD=new Database();
公共区域(){
//第一个JComboBox
comboBox_gouver=新的JComboBox();
BD.remplir_Jcomb();
sl_contentPanel.putConstraint(SpringLayout.NORTH、lbl_gouver、5、SpringLayout.NORTH、comboBox_gouver);
sl_contentPanel.putConstraint(SpringLayout.NORTH,组合框_gouver,5,SpringLayout.NORTH,contentPanel);
sl_contentPanel.putConstraint(SpringLayout.WEST、comboBox_gouver、16、SpringLayout.EAST、lbl_gouver);
sl_contentPanel.putConstraint(SpringLayout.EAST,组合框\u gouver,-26,SpringLayout.EAST,contentPanel);
contentPanel.add(comboBox\u gouver);
comboBox_ACTELS=新JComboBox();
sl_contentPanel.putConstraint(SpringLayout.NORTH、lbl_ACTELS、5、SpringLayout.NORTH、comboBox_ACTELS);
sl_contentPanel.putConstraint(SpringLayout.NORTH,comboBox_ACTELS,6,SpringLayout.SOUTH,comboBox_gouver);
sl_contentPanel.putConstraint(SpringLayout.WEST、comboBox_ACTELS、90、SpringLayout.EAST、lbl_ACTELS);
sl_contentPanel.putConstraint(SpringLayout.SOUTH,组合框_ACTELS,-29,SpringLayout.SOUTH,contentPanel);
sl_contentPanel.putConstraint(SpringLayout.EAST,组合框_ACTELS,-26,SpringLayout.EAST,contentPanel);
contentPanel.add(组合框\动作);
}}
erreur:
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:794)
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7139)
at tn.pack.ACTEL.Database.remplir_Jcomb(Database.java:94)
at tn.pack.ACTEL.Region.<init>(Region.java:75)
at tn.pack.ACTEL.Region.main(Region.java:41)
java.sql.SQLException:结果集关闭后不允许操作
位于com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
位于com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
位于com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
位于com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
位于com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:794)
位于com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7139)
位于tn.pack.ACTEL.Database.remplir_Jcomb(Database.java:94)
位于tn.pack.ACTEL.Region.(Region.java:75)
位于tn.pack.ACTEL.Region.main(Region.java:41)
对两个ResultSet对象使用两个单独的语句对象。如果语句对象已打开并由ResultSet使用,则不能重用该对象。大概是这样的:-
void remplir_Jcomb() {
Connection conn = null;
Statement st1 = null;
Statement st2 = null;
String rq1 = "SELECT region FROM rg";
String rq2 = "SELECT ACTELS FROM rg";
//Rest of your code here
try {
// snipping off some more code
//...
st1 = conn.createStatement();
st2 = conn.createStatement();
ResultSet rs1 = st1.executeQuery(rq1);
ResultSet rs2 = st2.executeQuery(rq2);
while ((rs1.next())&&(rs2.next())) {
comboBox_gouver.addItem(rs1.getString(1));
comboBox_ACTELS.addItem(rs2.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
st1.close();
st1.close();
rs1.close();
rs2.close();
conn.close();
}
}
1) 从数据库填充数据(使用finally block
关闭打开的对象,因为此代码在所有情况下都会执行)
2) 在Db语句内部,将数据填充到(注意API之间的差异/)
- 到
-ComboxModel
JComboBox(ComboxModel aModel)/JComboBox(ComboxModel aModel)
- 到对象/元素的
-数组
JComboBox(对象[]项)/JComboBox(E[]项)
- 到对象/元素的
-向量
JComboBox(向量项)/JComboBox(向量项)
import java.awt.*;
导入java.awt.event.*;
导入java.util.ArrayList;
导入javax.swing.*;
公共类ComboBoxListeners{
私有jf框架;
专用JComboBox组合框;
专用JLabel标签=新JLabel();
私有DefaultComboxModel ComboxModel=新的DefaultComboxModel();
公共ComboxListeners(){
comboBox=新的JComboBox(comboBoxModel);
comboBox.addItemListener(新的ItemListener(){
@凌驾
公共无效itemStateChanged(ItemEvent e){
如果((例如getStateChange()==ItemEvent.SELECTED)){
字符串str=comboBox.getSelectedItem().toString();
label.setText(“从JComboBox中选择的值为:“+str”);
}
}
});
标签。设置首选尺寸(新尺寸(400,30));
JButton btnAdd=新JButton(新抽象操作(“附加项”){
私有静态最终长serialVersionUID=1L;
@凌驾
已执行的公共无效操作(操作事件e){
addNewItemsFromDatabase();
}
});
JButton btnRefresh=newjbutton(新抽象操作(“刷新项”){
私有静态最终长serialVersionUID=1L;
@凌驾
已执行的公共无效操作(操作事件e){
refreshItemsFromDatabase();
}
});
f=新的JFrame(“组合框项目监听器”);
f、 setLayout(新的GridLayout(0,1,15,15));
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f、 添加(组合框);
f、 添加(标签);
f、 添加(btnAdd);
f、 添加(btnRefresh);
f、 设置位置(150150);
f、 包装();
f、 setVisible(真);
}
public void addNewItemsFromDatabase(){
ArrayList ageList=新建ArrayList();
对于(int i=1;我希望将close()语句移动到我尝试过的最后一个块,但他显示了一个错误:tn.pack.ACTEL.Database.remplir_Jcomb(Database.java:100)tn.pack.ACTEL.Region.(Region.java:66)tn.pack.ACTEL.Region.main(Region.java:36)@user1308236 NullPointerException?您是否仔细检查了代码?所有对象在使用之前是否都已正确初始化?例如,我可以看到正在使用lbl_gouver,但我没有看到它被分配了内存;代码是否已删除,或者是这样的?不是所有代码,而是错误:java.lang.NullPointerException位于tn.pack.ACTEL.Datn.pack.ACTEL.Region.main(Region.java:38)的tabase.remplir_Jcomb(Database.java:100)让我重新表述一下。检查所有对象在使用之前是否都分配了内存
void remplir_Jcomb() {
Connection conn = null;
Statement st1 = null;
Statement st2 = null;
String rq1 = "SELECT region FROM rg";
String rq2 = "SELECT ACTELS FROM rg";
//Rest of your code here
try {
// snipping off some more code
//...
st1 = conn.createStatement();
st2 = conn.createStatement();
ResultSet rs1 = st1.executeQuery(rq1);
ResultSet rs2 = st2.executeQuery(rq2);
while ((rs1.next())&&(rs2.next())) {
comboBox_gouver.addItem(rs1.getString(1));
comboBox_ACTELS.addItem(rs2.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
st1.close();
st1.close();
rs1.close();
rs2.close();
conn.close();
}
}
void whatever {
Connection conn = null;
Statement st1 = null;
try {
st1 = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
st1.close();
rs1.close();
rs2.close();
conn.close();
} catch (SQLException ex) {
//
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class ComboBoxListeners {
private JFrame f;
private JComboBox comboBox;
private JLabel label = new JLabel();
private DefaultComboBoxModel comboBoxModel = new DefaultComboBoxModel();
public ComboBoxListeners() {
comboBox = new JComboBox(comboBoxModel);
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() == ItemEvent.SELECTED)) {
String str = comboBox.getSelectedItem().toString();
label.setText("Selected Value From JComboBox is : " + str);
}
}
});
label.setPreferredSize(new Dimension(400, 30));
JButton btnAdd = new JButton(new AbstractAction("Append Items") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
addNewItemsFromDatabase();
}
});
JButton btnRefresh = new JButton(new AbstractAction("Refresh Items") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
refreshItemsFromDatabase();
}
});
f = new JFrame("ComboBox ItemListeners");
f.setLayout(new GridLayout(0, 1, 15, 15));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(comboBox);
f.add(label);
f.add(btnAdd);
f.add(btnRefresh);
f.setLocation(150, 150);
f.pack();
f.setVisible(true);
}
public void addNewItemsFromDatabase() {
ArrayList<Integer> ageList = new ArrayList<Integer>();
for (int i = 1; i <= 5; ++i) {
ageList.add(i);
}
for (final Integer i : ageList) {
EventQueue.invokeLater(new Runnable() {
public void run() {// updates to the Swing GUI must be done on EDT
comboBoxModel.addElement(i);
}
});
}
}
public void refreshItemsFromDatabase() {
comboBoxModel = new DefaultComboBoxModel();
ArrayList<Integer> ageList = new ArrayList<Integer>();
for (int i = 1; i <= 5; ++i) {
ageList.add(i);
}
for (final Integer i : ageList) {
EventQueue.invokeLater(new Runnable() {
public void run() {// updates to the Swing GUI must be done on EDT
comboBoxModel.addElement(i);
}
});
}
comboBox.setModel(comboBoxModel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ComboBoxListeners comboBoxListeners = new ComboBoxListeners();
}
});
}
}