gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:29:19 25 4
gpt4 key购买 nike

这是我当前的程序:

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)。

我的主要问题是:

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

最佳答案

您的小程序存在很多问题:

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

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

关于java - 如何创建一个 Applet 来创建从 Oracle 数据库中提取的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12570205/

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