当前位置: 技术问答>java相关
求一个把server端的resultSet传到client端显示到JTable的例子。
来源: 互联网 发布时间:2015-05-07
本文导语: 求一个把把server端的resultSet传到client端显示到JTable的例子。 或者其他能够把server端从数据库中select 出来,显示到client中jTable中的例子 | 可以先把resultset序列化,然后再传过去 public static byte[...
求一个把把server端的resultSet传到client端显示到JTable的例子。
或者其他能够把server端从数据库中select 出来,显示到client中jTable中的例子
或者其他能够把server端从数据库中select 出来,显示到client中jTable中的例子
|
可以先把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;
}
试试吧.....................
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;
}
试试吧.....................
|
用value object
|
如果使用servlet之类的技术很容易啊,读取输出流然后封装成一个ObjectOutputStream,将你需要的数据存在自己定义的一个类里面,并且实现序列化接口,然后使用ObjectOutputStream的wirteIbject写,在客户端通过ObjectInputStream的readObject方法读就可以啊。
这个方法在我使用applet从servlet读取数据时用过,可行。
这个方法在我使用applet从servlet读取数据时用过,可行。
|
简单
客户端socket连接到server
server检索数据库后将结果存为一个数组然后writeObject()到客户端
客户端在接收到数组后对JTable调用setModel一下就完了。
客户端socket连接到server
server检索数据库后将结果存为一个数组然后writeObject()到客户端
客户端在接收到数组后对JTable调用setModel一下就完了。
|
我用的是很原始的方法 把查出来得数据加成字符串穿过去 再截取
然后用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;
}
}
然后用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;
}
}