举一个简单的例子将数据库表内容写入Java Jtable中
就随便定义一个jtable,然后将表(名字就叫biao吧)内容写入java程序中的表里面,设置一个按钮监听器,按钮(显示全部),简单的select*frombiao语句...
就随便定义一个jtable,然后将表(名字就叫biao吧)内容写入java程序中的表里面,设置一个按钮监听器,按钮(显示全部),简单的select * from biao语句
展开
展开全部
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(), "入库单号只能为数字!");
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);
}
}
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(), "入库单号只能为数字!");
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);
}
}
展开全部
/**************
从emp表查出姓名和工作,放入jtable,我也新学的,例子简陋,而且可能有些写法还不规范
******************/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class RsToJtable {
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
JFrame f = new JFrame("Sql结果放入JTable");
JTable table = new JTable(0, 2);
DefaultTableModel dataModel = (DefaultTableModel) table.getModel();
int count = dataModel.getRowCount();
while (count > 0) {
dataModel.removeRow(0);
}
Connection conn = getConn();
PreparedStatement ps = conn
.prepareStatement("SELECT ename,job from emp");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String ename = rs.getString(1);
String job = rs.getString(2);
Vector<String> v = new Vector<String>();
v.add(ename);
v.add(job);
dataModel.addRow(v);
}
table.setModel(dataModel);
f.add(table);
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static Connection getConn() throws ClassNotFoundException,
SQLException {
String url = "jdbc:oracle:thin:@127.0.0.1:1521/orcl";//连接
String username = "scott";//用户名
String password = "scott";//密码
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
return (conn != null) ? conn : null;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询