gpt4 book ai didi

java - 我无法将数据库中的项目输入 JComboBox

转载 作者:行者123 更新时间:2023-12-02 13:32:55 25 4
gpt4 key购买 nike

这是代码。我在尝试将数据库项目导入组合框中时遇到错误。我无法在 (cmbProjectDetails.additems(name);) 中获取组合牛的名称。

请看下面的代码。

import java.awt.Color;

public class ProjectDetails_3_1 {

public JFrame frame;

private Connection connection=null;
private Statement statement;
private ResultSet resultSet=null;
private PreparedStatement pst=null;

private JSplitPane splitPaneRightMaster;

private JSplitPane splitPaneMaster;
private String sqlQuery;
private JPanel panelLeft;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ProjectDetails_3_1 window = new ProjectDetails_3_1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public ProjectDetails_3_1() {
initialize();
}


public void getDatabaseConnection() {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load Driver
Connection con = DriverManager.getConnection("jdbc:odbc:PRMS Build 8"); //Create Connection with Data Source Name : HOD_DATA
Statement s = con.createStatement(); // Create Statement
String query = "select PROJ_NAME from PROJECT"; // Create Query
s.execute(query); // Execute Query
ResultSet rs = s.getResultSet(); //return the data from Statement into ResultSet
while(rs.next()) // Retrieve data from ResultSet
{
System.out.print("Project Name : "+rs.getString(1)); //1st column of Table from database
System.out.println();//System.out.print(" , Name : "+rs.getString(2)); //2nd column of Table

}
s.close();
con.close();
}
catch (Exception e)
{
System.out.println("Exception : "+e);
}
}


// Function to get database connection.
/*public void getDatabaseConnection() {
//Registering Oracle JDBC driver class
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
}catch(ClassNotFoundException ex) {
System.out.println("JDBC-ODBC driver not found: " + ex);
}

//Opening database connection
try {
connection = DriverManager.getConnection(
"jdbc:ucanaccess://D:/workspace/PRMS v1.0 Build 8/PRMS Build 8.mdb;memory=false",
"",
""
);
connection.setAutoCommit(false);
statement = connection.createStatement();
}catch(Exception ex){
ex.printStackTrace();
}

return;
}


*/
private void initialize() {

frame = new JFrame();
frame.getContentPane().setBackground(new Color(255, 255, 255));
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\Eclipse_Workspace\\PRMS v1.0 Build 9\\resources\\Logo_48.png"));
frame.setResizable(false);
frame.setTitle("PRMS[Build 9] - User Portal");
frame.setSize(1000, 700);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(false);
frame.getContentPane().setLayout(null);
JPanel panelHeader = new JPanel();
panelHeader.setBounds(0, 0, 1000, 10);
panelHeader.setBackground(SystemColor.textHighlight);
frame.getContentPane().add(panelHeader);
panelHeader.setLayout(null);
JPanel panelFooter = new JPanel();
panelFooter.setBounds(0, 662, 1000, 10);
panelFooter.setBackground(SystemColor.textHighlight);
frame.getContentPane().add(panelFooter);
panelFooter.setLayout(null);

splitPaneMaster = new JSplitPane();
splitPaneMaster.setResizeWeight(0.25);
splitPaneMaster.setVerifyInputWhenFocusTarget(false);
splitPaneMaster.setFocusable(false);
splitPaneMaster.setDividerSize(0);
splitPaneMaster.setBorder(null);
splitPaneMaster.setOpaque(false);
splitPaneMaster.setBounds(0, 7, 1000, 655);
frame.getContentPane().add(splitPaneMaster);

panelLeft = new JPanel();
panelLeft.setRequestFocusEnabled(false);
panelLeft.setBackground(SystemColor.textHighlight);
splitPaneMaster.setLeftComponent(panelLeft);
panelLeft.setLayout(null);

splitPaneRightMaster = new JSplitPane();
splitPaneRightMaster.setResizeWeight(0.084);
splitPaneRightMaster.setVerifyInputWhenFocusTarget(false);
splitPaneRightMaster.setFocusable(false);
splitPaneRightMaster.setDividerSize(0);
splitPaneRightMaster.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPaneRightMaster.setBorder(null);
splitPaneRightMaster.setOpaque(false);
splitPaneMaster.setRightComponent(splitPaneRightMaster);

JPanel panelDetails3_1 = new JPanel();
panelDetails3_1.setBackground(Color.WHITE);
splitPaneRightMaster.setRightComponent(panelDetails3_1);
panelDetails3_1.setLayout(null);

JLabel lblProjectDetails = new JLabel("Project Details");
lblProjectDetails.setFont(new Font("Segoe UI Symbol", Font.BOLD, 18));
lblProjectDetails.setBounds(80, 26, 178, 27);
panelDetails3_1.add(lblProjectDetails);

JLabel lblSelectAnyProject = new JLabel("Select any of the projects tagged to you to view the details.");
lblSelectAnyProject.setFont(new Font("SansSerif", Font.BOLD, 12));
lblSelectAnyProject.setBounds(80, 53, 466, 27);
panelDetails3_1.add(lblSelectAnyProject);

JLabel lblProjectName = new JLabel("Project Name:");
lblProjectName.setBounds(80, 102, 83, 16);
panelDetails3_1.add(lblProjectName);

JComboBox cmbProjectDetails = new JComboBox<String>();
//cmbProjectDetails.addItem("1111");
//cmbProjectDetails.addItem("2222");
cmbProjectDetails.setBounds(173, 97, 85, 27);
panelDetails3_1.add(cmbProjectDetails);


}
private void fillCombo(){
try{
String sql= "select PROJ_NAME from PROJECT";
pst=connection.prepareStatement(sql);
resultSet=pst.executeQuery();

while(resultSet.next()){
String name= resultSet.getString("PROJ_NAME");
cmbProjectDetails.additems(name);**//Error is here**

}
}
catch(Exception e){
System.out.println("Error releasing connection: " + e);
}
}
// Function to release database connection and resources.
public void releaseDatabaseConnection() {
try{
if(resultSet != null){ resultSet.close(); }
}catch(Exception ex){
System.out.println("Error releasing resultSet: " + ex);
}

try{
if(statement != null){ statement.close(); }
}catch(Exception ex){
System.out.println("Error releasing statement: " + ex);
}

try{
if(connection != null){ connection.close(); }
}catch(Exception ex){
System.out.println("Error releasing connection: " + ex);
}

return;
}

}

最佳答案

I am not able to take the name of the combobox

将组合框变量定义为实例变量,而不是局部变量。

实例变量可以通过类的任何方法访问。局部变量只能在定义它的方法中访问。

例如,您将它们定义为实例变量:

private JSplitPane splitPaneRightMaster;

private JSplitPane splitPaneMaster;
private String sqlQuery;
private JPanel panelLeft;

为什么不能在这里定义组合框变量?

关于java - 我无法将数据库中的项目输入 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43125188/

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