| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 438 人关注过本帖
标题:请教,表格无法显示!!
只看楼主 加入收藏
ranran
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-4-14
收藏
 问题点数:0 回复次数:0 
请教,表格无法显示!!

在JComboBox中放置了数据库中的所有表名,选择某一项后,在下面的JScrollPane中用表格显示选择表格的内容,
为什么,每次选择后表格都看不见,而我用鼠标略微一改表窗体的大小,表格立刻显示了!

import java.awt.*;
import java.sql.*;
import javax.swing.*;
public class ResultSetTable
{
public static void main(String[] args)
{
ResultSetTableFrame frame=new ResultSetTableFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ResultSetTableFrame extends JFrame
{
private JScrollPane spane;
private JComboBox tablenames;
private ResultSet rs;
private Statement st;
public ResultSetTableFrame()
{
setTitle("display data from database table on table!");
setSize(300,300);
final Container container=this.getContentPane();
container.setLayout(new BorderLayout());
JPanel panel=new JPanel();
String []names={"person","Course","Student"};
tablenames=new JComboBox(names);
panel.add(tablenames);
container.add(panel,BorderLayout.NORTH);
tablenames.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(spane!=null)
{
getContentPane().remove(spane);
}
String tablename=tablenames.getSelectedItem().toString();
String query="select * from "+tablename;
Connection con=ConnectionFactory.getConnection();
try
{
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=st.executeQuery(query);
ResultSetTableModel model=new ResultSetTableModel(rs);
JTable table=new JTable(model);
spane=new JScrollPane(table);
getContentPane().add(spane,BorderLayout.CENTER);
repaint();//这个要不要加上???
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
});

}
//这个函数要写吗????
public void paintComponent(Graphics g)
{
super.paintComponents(g);
//paint(g);
}
}
//定义表格模式
class ResultSetTableModel extends AbstractTableModel
{
private ResultSet rs;
private ResultSetMetaData rsmd;
public ResultSetTableModel(ResultSet rs)
{
this.rs=rs;
try
{
rsmd=rs.getMetaData();
}
catch(SQLException ec)
{
System.out.println(ec.getMessage()+"87");
}
}
public int getRowCount()
{
try
{
rs.last();
return rs.getRow();
}
catch(SQLException ec)
{
System.out.println(ec.getMessage()+"99");
return 0;
}
}
public int getColumnCount()
{
try
{
return rsmd.getColumnCount();
}
catch(SQLException ec)
{
System.out.println(ec.getMessage()+"111");
return 0;
}
}
public String getColumnName(int column)
{
try
{
return rsmd.getColumnName(column+1);
}
catch(SQLException ec)
{
System.out.println(ec.getMessage()+"123");
return null;
}
}
public Object getValueAt(int row, int column)
{
try
{
rs.absolute(row+1);
return rs.getObject(column+1);
}
catch(SQLException ec)
{
System.out.println(ec.getMessage()+"136");
return null;
}
}
}

import java.sql.*;
public class ConnectionFactory
{
private static Connection connection=null;
public static Connection getConnection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("成功加载JDBC_ODBC驱动程序!");
connection = DriverManager.getConnection("jdbc:odbc:myDatabase");
}
catch(ClassNotFoundException ex)
{
System.out.println("加载JDBC_ODBC驱动程序失败!");
System.out.println(ex.getMessage());
}
catch(SQLException ec)
{
System.out.println("查询数据库失败!");
System.out.println("SQLException:"+ec.getMessage());
}
return connection;
}
public static void ConnectionClose()
{
if(connection!=null)
try
{
connection.close();
}
catch(Exception e)
{
System.out.println("数据库关闭连接失败!");
}

}
}

搜索更多相关主题的帖子: 表格 
2007-06-14 23:55
快速回复:请教,表格无法显示!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014400 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved