0

这是我目前的程序:

import java.applet.Applet;
import java.awt.Graphics;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JComboBox;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Properties;

public class CodesApplet extends Applet 
{
    private Properties properties;
    private String configFilePath;
    private FileInputStream fis;
    private String driverName;
    private String userName;
    private String password;
    private String url;

    private Connection conn;
    private Statement st;
    private Timestamp created = new Timestamp(System.currentTimeMillis());

    private JComboBox codes1;
    private JComboBox codes2;
    private JComboBox otherCodes;

    public void init() 
    {
        try 
        {
            //Loads the values from the properties file 
            try 
            {
                properties = new Properties();
                configFilePath="C:\\scriptProps.properties";
                fis = new FileInputStream(configFilePath);

                properties.load(fis);

                if (fis != null)
                {
                    fis.close();
                }
            } 
            catch (FileNotFoundException e) 
            {
                System.err.println("init(): FileNotFoundException(): " + e.getMessage());
            }

            driverName=properties.getProperty("driverName");
            userName=properties.getProperty("userName");
            password=properties.getProperty("password");
            url=properties.getProperty("url");

            //Establishes the connection to the database
            System.out.println("init(): loading OracleDriver for applet created at " + created.toString());
            Class.forName(driverName);
            System.out.println("init(): getting connection");
            conn = DriverManager.getConnection(url, userName, password);
            st = conn.createStatement();

            //Instantiates the previously declared variables for the drop-downs.
            codes1 = new JComboBox();
            codes2 = new JComboBox();
            otherCodes = new JComboBox();
        }
        catch (ClassNotFoundException e)
        {
            System.err.println("init(): ClassNotFoundException: " + e.getMessage());
        } 
        catch (SQLException e)
        {
            System.err.println("init(): SQLException: " + e.getMessage());
        } catch (IOException e) 
        {
            System.err.println("init(): IOException. " + e.getMessage());
        }
    }

    public void start() 
    {
        System.out.println("start(): ");
    }

    public void stop()
    {
        System.out.println("stop(): ");
    }

    //Returns the first drop-down...
    public JComboBox getComboBox1() 
    {
        codes1.removeAllItems();
        codes1.addItem("Please Select...");

        try 
        {
            ResultSet rs = st.executeQuery("select codes from myTable");

            while (rs.next()) 
            {
                codes1.addItem(rs.getString("codes"));
            }

            rs.close();
            st.close();
        } 
        catch (SQLException sqle)
        {
            System.out.println(sqle);
        }

        return codes1;
    }

    //Returns the second drop-down...
    public JComboBox getComboBox2() 
    {
        codes2.removeAllItems();
        codes2.addItem("Please Select...");

        try 
        {
            ResultSet rs = st.executeQuery("select codes from myTable");

            while (rs.next()) 
            {
                codes2.addItem(rs.getString("codes"));
            }

            rs.close();
            st.close();
        } 
        catch (SQLException sqle)
        {
            System.out.println(sqle);
        }

        return codes2;
    }

    //Returns the third drop-down...
    public JComboBox getComboBox3() 
    {
        otherCodes.removeAllItems();
        otherCodes.addItem("Please Select...");

        try 
        {
            ResultSet rs = st.executeQuery("select otherCodes from myTable2");

            while (rs.next()) 
            {
                otherCodes.addItem(rs.getString("otherCodes"));
            }

            rs.close();
            st.close();
        } 
        catch (SQLException sqle)
        {
            System.out.println(sqle);
        }

        return otherCodes;
    }

    public void paint(Graphics g)
    {
        System.out.println("paint(): creating the drop-downs...");

        getComboBox1();
        getComboBox2();
        getComboBox3();
    }

    public void destroy() 
    {
        System.out.println("destroy(): closing connection for applet created at " + created.toString());

        try 
        {
            conn.close();
        } 
        catch (SQLException e) 
        {
            System.err.println("destroy: SQLException: " + e.getMessage());
        }
    }
}

本质上,我想做的是让这个小程序从多个表中提取数据并用该数据填充下拉框。我已经看到了一些关于如何通过一个下拉菜单执行此操作的示例(因此您会看到一个涉及代码 1 的返回语句)。

我的主要问题是:

  • 一般来说,我这样做对吗?这是从多个表中提取多个字段的最佳方式吗?
  • 另外,我知道这只会填充组合框。如果我想允许用户在从下拉列表中选择适当的值后(在它们被填充之后)点击一个按钮,并将这些值存储到数据库中的一个单独的表中,我该怎么做?
4

1 回答 1

5

你的小程序有很多问题:

  • 您永远不会将组合框添加到顶级容器层次结构中
  • 如果您想收到用户选择更改的通知,可以将ActionListener 添加到 JComboBoxes
  • 你不应该覆盖油漆
  • 每次调用paint 时,您都在重新创建组合框的内容。您应该在小程序初始化时创建和添加组合框
  • 小程序通常通过网页/服务器分发:您的属性文件将不可用
  • 除非您的数据库允许远程访问,否则这是行不通的。
  • 要将按钮添加到显示中,只需调用 new JButton("My button") 并将其添加到组件层次结构中
  • ...

这是Swing 教程的链接。我认为很多章节可以帮助你

于 2012-09-24T20:20:31.787 回答