如何创建一个创建从Oracle数据库中提取的下拉列表的Applet

时间:2012-09-24 17:50:55

标签: java database swing applet

这是我目前的计划:

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的返回语句)。

我的主要问题是:

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

1 个答案:

答案 0 :(得分:5)

您的小程序中存在许多问题:

  • 您永远不会将组合框添加到顶级容器层次结构
  • 如果您希望收到有关用户选择更改的通知,可以添加ActionListener to the JComboBoxes
  • 你不应该覆盖油漆
  • 每次调用绘画时,您都在重新创建组合框的内容。你应该在applet初始化时创建并添加你的组合框
  • Applet通常通过网页/服务器分发:您的propery文件无法使用
  • 除非您的数据库允许远程访问,否则这将无效。
  • 要向显示添加按钮,只需调用新的JButton("我的按钮")并将其添加到组件层次结构
  • ...

以下是Swing tutorials的链接。我认为很多章节可以帮到你