当前位置: 代码迷 >> Java相关 >> 请教,表格无法显示!!
  详细解决方案

请教,表格无法显示!!

热度:140   发布时间:2007-06-14 23:55:13.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("数据库关闭连接失败!");
}

}
}

搜索更多相关的解决方案: 表格  

----------------解决方案--------------------------------------------------------
  相关解决方案