从数据库表中添加 JTable 中的值
Posted
技术标签:
【中文标题】从数据库表中添加 JTable 中的值【英文标题】:Add values in JTable from Database Table 【发布时间】:2013-07-18 10:01:32 【问题描述】:这是我的第一个类名 ConnectionDBClass.java 我想保存从数据库中检索的数据,然后存储/显示到 JTable 中。
public class ConnectionDBClass implements Job
public void execute(JobExecutionContext arg0) throws JobExecutionException
String serverName = "192.168.0.1";
String portNumber = "1521";
String sid = "hifi";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "courser_hotline";
String password = "courser_hotline";
String[] columnNames = "command_name", "omc_name", "to_module", "start_time", "end_time", "status", "priority", "cmd_id";
try
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// System.out.println("Connecting to the database...");
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
String query = "select command_name, omc_name, to_module, start_time, end_time, status, priority, cmd_id from sync_task_table";
// String query = "select * from sync_task_table";
ResultSet resultset = statement.executeQuery(query);
// Create some data
String dataValues[][] =
resultset.getString("command_name"), resultset.getString( "omc_name"), resultset.getString( "to_module"), resultset.getString("start_time"),resultset.getString("end_time"),resultset.getString("status"),resultset.getString("priority"),resultset.getString("cmd_id")
;
//
//// Create a new table instance
GUIClass.table = new JTable( dataValues, columnNames );
catch (Exception e)
System.out.println("The exception raised is:" + e);
这是我的第二个类名 GUIClass.java 在这个类中 GUI 的代码
public class GUIClass extends JFrame
static JFrame frame;
static JLabel formlabel;
static JPanel panel1;
static JPanel panel2;
static JTextField t1;
static JComboBox selectOMC;
static JButton run;
static JPanel panel3;
static DefaultTableModel model;
public static JTable table;
static JScrollPane tableScroll;
static Dimension tablePreferred;
public static void createGUI()
InilizationAndLabelGUIVariables.Initilization();
InilizationAndLabelGUIVariables.LabelForm();
public static void main(String[] args)
createGUI();
我的第三个类名是 InilizationAndLabelGUIVariables.java 在这个类中初始化并标记在 GUIClass.java 类中声明的变量
public class InilizationAndLabelGUIVariables
public static void Initilization()
GUIClass.frame = new JFrame("Syncronization Optimizer");
GUIClass.panel1 = new JPanel(new BorderLayout(5,5));
GUIClass.panel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT, 3,3));
GUIClass.formlabel = new JLabel("Syncronization Optimizer Tool");
GUIClass.t1 = new JTextField(23);
GUIClass.selectOMC = new JComboBox(ConnectionDB.OMCName);
GUIClass.run = new JButton(" Run ");
GUIClass.panel2 = new JPanel();
GUIClass.model = new DefaultTableModel(ConnectionDBClass.dataValues, ConnectionDBClass.columnNames);
GUIClass.table = new JTable(GUIClass.model);
try
// 1.6+
GUIClass.table.setAutoCreateRowSorter(true);
catch(Exception continuewithNoSort)
GUIClass.tableScroll = new JScrollPane(GUIClass.table);
GUIClass.tablePreferred = GUIClass.tableScroll.getPreferredSize();
GUIClass.tableScroll.setPreferredSize(
new Dimension(GUIClass.tablePreferred.width, GUIClass.tablePreferred.height/3) );
GUIClass.frame.setContentPane(GUIClass.panel1);
GUIClass.frame.setBounds(150,100,570,491);
GUIClass.frame.setResizable(false);
GUIClass.frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
GUIClass.frame.setVisible(true);
public static void LabelForm()
GUIClass.panel1.add(GUIClass.panel2, BorderLayout.NORTH);
GUIClass.panel2.add(GUIClass.formlabel);
GUIClass.panel2.add(GUIClass.selectOMC);
GUIClass.panel1.add(GUIClass.tableScroll);
GUIClass.panel2.add(GUIClass.table);
// gui.add( splitPane, BorderLayout.CENTER );
请帮我解决这个问题。
【问题讨论】:
【参考方案1】:JTable (its model)
和Table from Database
结构相同,数据存放在columns
和rows
中
在ResultSet
和Resultset
中的每一行循环以将new Vector<Object>
或new Object[]
添加到表模型或JTable.addRow
以添加DefaultTableModel
搜索ResultSetTableModel
或更好地搜索TableFromDatabase
,只是避免重新发明***
【讨论】:
兄弟,我明白这一点,但您能否根据需要更改我的代码并将最终结果发送给我,因为我不知道如何根据您的指南修改我的代码。我会非常感谢你。以上是关于从数据库表中添加 JTable 中的值的主要内容,如果未能解决你的问题,请参考以下文章