java 将数据写入table_举一个简单的例子将数据库表内容写入Java Jtable中

这篇博客展示了如何从数据库中检索数据并将其填充到Java Swing应用中的JTable组件。通过选择不同的查询条件(入库单号、经手人ID、入库日期、货品编号、仓库编号),用户可以查看相应的数据记录。程序会检查输入的有效性,并在查询结果为空或输入不正确时给出提示。
摘要由CSDN通过智能技术生成

展开全部

package com.gxa.Export;

import java.awt.event.*;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import com.gxa.Main.Main;

import com.gxa.Pub.*;

import javax.swing.*;

import javax.swing.table.DefaultTableModel;

public class ExportSelectPanel implements ActionListener{

JPanel pSelect;

JComboBox cb;

JTextField tfd;

JLabel lb;

JButton btn1,btn2;

public void actionPerformed(ActionEvent e) {

Object btn = e.getSource();

PreparedStatement prep = null;

ResultSet rs;

if(btn==cb && cb.getSelectedItem().equals("入库日期")){

pSelect.add(lb);

}

if(btn==btn1){

Object cbName = cb.getSelectedItem();

String[] col = { "iExportId", "cEmployeeId", "dShipmentDate",

"cGoodId", "mPrice", "iAmount", "cStorageCode" };

DefaultTableModel mm = new DefaultTableModel(col, 0); // 定义一个表的模板

String str = tfd.getText();

try{

Connection conn = Conn.getCon().con;

if(tfd.getText().length()==0){

prep = conn.prepareStatement("select * from Export");

}

else if(cbName.equals("入库单号")){

prep = conn.prepareStatement("select * from Export where iExportId= ?");

prep.setString(1,str);

if(str.equals("")){

JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入入库单号!");

return;

}else if(!str.matches("^\\d+$")){

JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "入库单号只能e68a843231313335323631343130323136353331333239313431为数字!");

return;

}

}

else if(cbName.equals("经手人ID")){

prep = conn.prepareStatement("select * from Export where cEmployeeId= ?");

prep.setString(1,str);

if(str.equals("")){

JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入经手人!");

return;

}else if(!str.matches("^\\d+$")){

JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "经手人只能为数字!");

return;

}

}

else if(cbName.equals("入库日期")){

prep = conn.prepareStatement("select * from Export where dShipmentDate = '"+str+"'");

}

else if(cbName.equals("货品编号")){

prep = conn.prepareStatement("select * from Export where cCommodityId= ?");

prep.setString(1,str);

if(str.equals("")){

JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入货品编号!");

return;

}else if(!str.matches("^\\d+$")){

JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "货品编号只能为数字!");

return;

}

}

else if(cbName.equals("仓库编号")){

prep = conn.prepareStatement("select * from Export where cStorageCode= ?");

prep.setString(1,str);

if(str.equals("")){

JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入仓库编号!");

return;

}else if(!str.matches("^\\d+$")){

JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "仓库编号只能为数字!");

return;

}

}

rs = prep.executeQuery();// 将查询得到的结果集给rs

while (rs.next()) {

String iExportId = rs.getString(1);

String cEmployeeId = rs.getString(2);

String dShipmentDate = rs.getString(3);

String cCommodityId = rs.getString(4);

String mPrice = rs.getString(5);

String iAmount = rs.getString(6);

String cStorageCode = rs.getString(7);

String[] str_row = { iExportId, cEmployeeId, dShipmentDate,

cCommodityId, mPrice, iAmount, cStorageCode }; // 将一行的数据存在str_row

// 字符串数组里

mm.addRow(str_row);// 添加在表模板中

}

rs.close();

prep.close();

ExportMainPanel.getExportMainPanel().jtable.setModel(mm);

ExportMainPanel.getExportMainPanel().scp.add(ExportMainPanel.getExportMainPanel().jscrollpane);

// 将加载了表的滚动条在JFrame中显示

}catch(Exception ee){

ee.printStackTrace();

}

}

if(btn==btn2){

Main.getMain().setVisible(true);

ExportMainPanel.getExportMainPanel().setVisible(false);

}

}

public ExportSelectPanel(){

cb = new JComboBox();

tfd = new JTextField(10);

lb = new JLabel("日期格式2008-01-01 00:00:00");

pSelect = new JPanel();

btn1 = new JButton("确定查询");

btn2 = new JButton("返回主界面");

cb.addItem("入库单号");

cb.addItem("经手人ID");

cb.addItem("入库日期");

cb.addItem("货品编号");

cb.addItem("仓库编号");

cb.addActionListener(this);

btn1.addActionListener(this);

btn2.addActionListener(this);

pSelect.add(cb);

pSelect.add(tfd);

pSelect.add(btn1);

pSelect.add(btn2);

}

}

本回答被提问者采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值