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

时间:2017-03-30 17:59:18

标签: java swing jcombobox

这是守则。我在尝试将数据库项目导入组合框时收到错误。我无法在(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;
}

}

1 个答案:

答案 0 :(得分:1)

  

我无法使用组合框的名称

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

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

例如,您将这些定义为实例变量:

private JSplitPane splitPaneRightMaster;

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

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