求一个把server端的resultSet传到client端显示到JTable的例子。 忘记说了,要用socket传。不一定要把resultset传过去。把他里面的数据拿出来传过去也可以。主要是要能在client端显示出来。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以先把resultset序列化,然后再传过去public static byte[] bwritedato(Object data) { byte[] bx = new byte[1]; try { ObjectOutputStream fin; ByteArrayOutputStream b = new ByteArrayOutputStream(); fin = new ObjectOutputStream(b); fin.writeObject(data); fin.flush(); fin.close(); fin = null; bx = new byte[b.size()]; bx = b.toByteArray(); } catch(Exception e) { log(" bAppending/writing object data error : " + e.toString()); } return bx; }先用这个涵数把对象序列化为byte[],然后把这个byte[]通过Socket类传到客户端,然后再客户端再调用下面这个涵数 public static Object breaddato(byte[] data) { ResultSet bx = new Object(); try { ObjectInputStream fin; ByteArrayInputStream b = new ByteArrayInputStream(data); fin = new ObjectInputStream(b); bx = fin.readObject(); fin.close(); fin = null; } catch(Exception e) { log(" bAppending/writing object data error : " + e.toString()); } return bx; } 试试吧..................... 能不能把resultset中的数据拿出来做成数组,然后再传啊? 如果使用servlet之类的技术很容易啊,读取输出流然后封装成一个ObjectOutputStream,将你需要的数据存在自己定义的一个类里面,并且实现序列化接口,然后使用ObjectOutputStream的wirteIbject写,在客户端通过ObjectInputStream的readObject方法读就可以啊。这个方法在我使用applet从servlet读取数据时用过,可行。 简单客户端socket连接到serverserver检索数据库后将结果存为一个数组然后writeObject()到客户端客户端在接收到数组后对JTable调用setModel一下就完了。 我也是这么考虑的可我要让JTable对改变了值的单元格,闪动一下。提醒别人哪个值改动过了。该怎么做啊? Object[][] data= { {"", "", ""}, {"", "", ""}, {"", "", ""}, }String colNanems = {"1", "2", "3"};javax.swing.table.?model tmodel= new ?(data, colNames)table1.setModel(tmodel) 我用的是很原始的方法 把查出来得数据加成字符串穿过去 再截取 然后用mvc作一个添加 删除 改动的方法 就可以了 服务器段的就不写了 这是客户端的:package client;import com.sun.java.swing.event.*;import java.awt.*;import java.awt.event.*;import com.sun.java.swing.*;import com.sun.java.swing.table.*;import java.util.*;import myutil.*;/*** ÔÚÕâÀï²åÈëÀàÐÍ˵Ã÷¡£ * ½¨Á¢ÈÕÆÚ£º(2001-11-21 10:54:02) * @³ÌÐòÉè¼ÆÕߣº */public class UserTable extends JPanel{ private int cols = 4; private Object[] rowData = new Object[cols]; private String[] columnNames = {"Óû§ID","êdzÆ","PM¶¯Ì¬","ÀÛ¼ÆÉÏÏß"}; private JTable table = new JTable(); private StereoDeckModel model = new StereoDeckModel(); /** * ÔÚÕâÀï²åÈë·½·¨ËµÃ÷¡£ * ½¨Á¢ÈÕÆÚ£º(2001-11-22 18:54:49) */public UserTable() { setBounds(1, 0, 299, 174); table.setMaximumSize(new Dimension(200,174)); //table.set table.setModel(model); table.setRowHeight(22); //Ö»ÏÔʾÊúÌõ table.setShowHorizontalLines(false); //Ñ¡È¡·¶Î§É趨 table.setRowSelectionAllowed(true); table.setColumnSelectionAllowed(false); table.setCellSelectionEnabled(false); table.setSelectionMode(0); TableColumn idColumn = table.getColumn("Óû§ID"); idColumn.setCellRenderer(new BulbRenderer()); idColumn.setWidth(25); JScrollPane tp = new JScrollPane(table); tp.setBounds(0, 0, 299, 174); add(tp, BorderLayout.CENTER);}/** * ÔÚÕâÀï²åÈë·½·¨ËµÃ÷¡£ * ½¨Á¢ÈÕÆÚ£º(2001-11-21 18:25:50) */public void addRow(UserInfo userinfo) { model.addRow(userinfo); }/** * ÔÚÕâÀï²åÈë·½·¨ËµÃ÷¡£ * ½¨Á¢ÈÕÆÚ£º(2001-11-22 20:20:11) * @return com.sun.java.swing.JTable */public JTable getTable() { return table;}}package client;import com.sun.java.swing.*;import com.sun.java.swing.table.*;import java.util.*;import myutil.*;/** * ÔÚÕâÀï²åÈëÀàÐÍ˵Ã÷¡£ * ½¨Á¢ÈÕÆÚ£º(2001-11-21 10:54:02) * @³ÌÐòÉè¼ÆÕߣº */class StereoDeckModel extends AbstractTableModel { private int cols = 4; private Vector v = new Vector(); private String[] columnNames = {"Óû§ID","êdzÆ","PM¶¯Ì¬","ÀÛ¼ÆÉÏÏß"};/** * ÔÚÕâÀï²åÈë·½·¨ËµÃ÷¡£ * ½¨Á¢ÈÕÆÚ£º(2001-11-22 18:51:51) */public StereoDeckModel() { }/** * ÔÚÕâÀï²åÈë·½·¨ËµÃ÷¡£ * ½¨Á¢ÈÕÆÚ£º(2001-11-22 19:42:16) * @param userinfo myutil.UserInfo */public void addRow(UserInfo userinfo) { v.addElement(userinfo);}public int getColumnCount() { return columnNames.length;}public String getColumnName(int col) { return columnNames[col];}public int getRowCount() { return v.size();}public Object getValueAt(int row, int col) { UserInfo userinfo = (UserInfo) v.elementAt(row); switch (col) { case 0 : return userinfo.getFace() + TUtil.FGF + userinfo.getID(); case 1 : return userinfo.getNick(); case 2 : return userinfo.getState(); case 3 : return userinfo.getCTime() + "Сʱ"; default : return null; }}public boolean isCellEditable(int row, int col) { return false;}}package client;import java.awt.*;import com.sun.java.swing.*;import com.sun.java.swing.table.*;/** * ÔÚÕâÀï²åÈëÀàÐÍ˵Ã÷¡£ * ½¨Á¢ÈÕÆÚ£º(2001-11-21 10:54:02) * @³ÌÐòÉè¼ÆÕߣº */class BulbRenderer extends JLabel implements TableCellRenderer { Icon[] icons = new Icon[10]; private Icon icon1 = new ImageIcon("user1.gif"); private Icon icon2 = new ImageIcon("user2.gif"); private Icon icon3 = new ImageIcon("user3.gif"); private Icon icon4 = new ImageIcon("user4.gif"); private Icon icon5 = new ImageIcon("user5.gif"); private Icon icon6 = new ImageIcon("user6.gif"); private Icon icon7 = new ImageIcon("user7.gif"); private Icon icon8 = new ImageIcon("user8.gif"); private Icon icon9 = new ImageIcon("user9.gif"); private Icon icon10 = new ImageIcon("user10.gif");public BulbRenderer() { setHorizontalAlignment(JLabel.CENTER); setOpaque(true); icons[0] = icon1; icons[1] = icon2; icons[2] = icon3; icons[3] = icon4; icons[4] = icon5; icons[5] = icon6; icons[6] = icon7; icons[7] = icon8; icons[8] = icon9; icons[9] = icon10;}public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { try { String str = value.toString(); int i = str.indexOf(myutil.TUtil.FGF); int t = Integer.parseInt(str.substring(0, i)); setIcon(icons[t]); setText(str.substring(i + 1)); } catch (Exception e) { e.toString(); } if (isSelected) { setBackground(table.getSelectionBackground()); setForeground(table.getSelectionForeground()); } else { setBackground(table.getBackground()); setForeground(table.getForeground()); } return this;}} JButton变颜色求助 SWING的一个窗口问题 jmf制作播放器怎样实现多种音频的播放 急:串口问题 一个SQL问题 很蹊跷 关于大界面刷新问题,请教! 如何在程序中调用系统的目录选择 (即加入"浏览"键) 关于Swing的几个问题,希望各位解答! 各位大侠帮我看看 在如下代码中 为何我输入参数2 3 * 会出现问题(无效运算符)??? 而其他的没有问题 JTable中不显示header的网格 好心人贴个jbuilder3.5的序列号上来吧,敲键盘敲的我快晕死了。 有兴趣的朋友跟我谈谈java中的关键字:static
public static byte[] bwritedato(Object data)
{
byte[] bx = new byte[1];
try
{
ObjectOutputStream fin;
ByteArrayOutputStream b = new ByteArrayOutputStream();
fin = new ObjectOutputStream(b);
fin.writeObject(data);
fin.flush();
fin.close();
fin = null;
bx = new byte[b.size()];
bx = b.toByteArray();
}
catch(Exception e)
{
log(" bAppending/writing object data error : " + e.toString());
}
return bx;
}
先用这个涵数把对象序列化为byte[],然后把这个byte[]通过Socket类传到客户端,然后再客户端再调用下面这个涵数
public static Object breaddato(byte[] data)
{
ResultSet bx = new Object();
try
{
ObjectInputStream fin;
ByteArrayInputStream b = new ByteArrayInputStream(data);
fin = new ObjectInputStream(b);
bx = fin.readObject();
fin.close();
fin = null;
}
catch(Exception e)
{
log(" bAppending/writing object data error : " + e.toString());
}
return bx;
}
试试吧.....................
这个方法在我使用applet从servlet读取数据时用过,可行。
客户端socket连接到server
server检索数据库后将结果存为一个数组然后writeObject()到客户端
客户端在接收到数组后对JTable调用setModel一下就完了。
{"", "", ""},
{"", "", ""},
{"", "", ""},
}
String colNanems = {"1", "2", "3"};
javax.swing.table.?model tmodel= new ?(data, colNames)
table1.setModel(tmodel)
然后用mvc作一个添加 删除 改动的方法 就可以了
服务器段的就不写了 这是客户端的:
package client;import com.sun.java.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.table.*;
import java.util.*;
import myutil.*;
/**
* ÔÚÕâÀï²åÈëÀàÐÍ˵Ã÷¡£
* ½¨Á¢ÈÕÆÚ£º(2001-11-21 10:54:02)
* @³ÌÐòÉè¼ÆÕߣº
*/public class UserTable extends JPanel{
private int cols = 4;
private Object[] rowData = new Object[cols];
private String[] columnNames = {"Óû§ID","êdzÆ","PM¶¯Ì¬","ÀÛ¼ÆÉÏÏß"};
private JTable table = new JTable();
private StereoDeckModel model = new StereoDeckModel();
/**
* ÔÚÕâÀï²åÈë·½·¨ËµÃ÷¡£
* ½¨Á¢ÈÕÆÚ£º(2001-11-22 18:54:49)
*/
public UserTable() {
setBounds(1, 0, 299, 174);
table.setMaximumSize(new Dimension(200,174));
//table.set
table.setModel(model);
table.setRowHeight(22);
//Ö»ÏÔʾÊúÌõ
table.setShowHorizontalLines(false);
//Ñ¡È¡·¶Î§É趨
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(false);
table.setCellSelectionEnabled(false);
table.setSelectionMode(0);
TableColumn idColumn = table.getColumn("Óû§ID");
idColumn.setCellRenderer(new BulbRenderer());
idColumn.setWidth(25);
JScrollPane tp = new JScrollPane(table);
tp.setBounds(0, 0, 299, 174);
add(tp, BorderLayout.CENTER);
}
/**
* ÔÚÕâÀï²åÈë·½·¨ËµÃ÷¡£
* ½¨Á¢ÈÕÆÚ£º(2001-11-21 18:25:50)
*/
public void addRow(UserInfo userinfo) {
model.addRow(userinfo);
}
/**
* ÔÚÕâÀï²åÈë·½·¨ËµÃ÷¡£
* ½¨Á¢ÈÕÆÚ£º(2001-11-22 20:20:11)
* @return com.sun.java.swing.JTable
*/
public JTable getTable() {
return table;
}
}package client;import com.sun.java.swing.*;
import com.sun.java.swing.table.*;
import java.util.*;
import myutil.*;
/**
* ÔÚÕâÀï²åÈëÀàÐÍ˵Ã÷¡£
* ½¨Á¢ÈÕÆÚ£º(2001-11-21 10:54:02)
* @³ÌÐòÉè¼ÆÕߣº
*/class StereoDeckModel extends AbstractTableModel {
private int cols = 4;
private Vector v = new Vector();
private String[] columnNames = {"Óû§ID","êdzÆ","PM¶¯Ì¬","ÀÛ¼ÆÉÏÏß"};
/**
* ÔÚÕâÀï²åÈë·½·¨ËµÃ÷¡£
* ½¨Á¢ÈÕÆÚ£º(2001-11-22 18:51:51)
*/
public StereoDeckModel() {
}
/**
* ÔÚÕâÀï²åÈë·½·¨ËµÃ÷¡£
* ½¨Á¢ÈÕÆÚ£º(2001-11-22 19:42:16)
* @param userinfo myutil.UserInfo
*/
public void addRow(UserInfo userinfo) {
v.addElement(userinfo);
}
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public int getRowCount() {
return v.size();
}
public Object getValueAt(int row, int col) {
UserInfo userinfo = (UserInfo) v.elementAt(row);
switch (col) {
case 0 :
return userinfo.getFace() + TUtil.FGF + userinfo.getID();
case 1 :
return userinfo.getNick();
case 2 :
return userinfo.getState();
case 3 :
return userinfo.getCTime() + "Сʱ";
default :
return null;
}
}
public boolean isCellEditable(int row, int col) {
return false;
}
}
package client;import java.awt.*;
import com.sun.java.swing.*;
import com.sun.java.swing.table.*;
/**
* ÔÚÕâÀï²åÈëÀàÐÍ˵Ã÷¡£
* ½¨Á¢ÈÕÆÚ£º(2001-11-21 10:54:02)
* @³ÌÐòÉè¼ÆÕߣº
*/class BulbRenderer extends JLabel implements TableCellRenderer {
Icon[] icons = new Icon[10];
private Icon icon1 = new ImageIcon("user1.gif");
private Icon icon2 = new ImageIcon("user2.gif");
private Icon icon3 = new ImageIcon("user3.gif");
private Icon icon4 = new ImageIcon("user4.gif");
private Icon icon5 = new ImageIcon("user5.gif");
private Icon icon6 = new ImageIcon("user6.gif");
private Icon icon7 = new ImageIcon("user7.gif");
private Icon icon8 = new ImageIcon("user8.gif");
private Icon icon9 = new ImageIcon("user9.gif");
private Icon icon10 = new ImageIcon("user10.gif");
public BulbRenderer() {
setHorizontalAlignment(JLabel.CENTER);
setOpaque(true);
icons[0] = icon1;
icons[1] = icon2;
icons[2] = icon3;
icons[3] = icon4;
icons[4] = icon5;
icons[5] = icon6;
icons[6] = icon7;
icons[7] = icon8;
icons[8] = icon9;
icons[9] = icon10;
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
try {
String str = value.toString();
int i = str.indexOf(myutil.TUtil.FGF);
int t = Integer.parseInt(str.substring(0, i));
setIcon(icons[t]);
setText(str.substring(i + 1));
} catch (Exception e) {
e.toString();
}
if (isSelected) {
setBackground(table.getSelectionBackground());
setForeground(table.getSelectionForeground());
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
return this;
}
}