import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import java.awt.*;
import javax.swing.JScrollPane;
public class Frame2 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JTable jTable1 = new JTable(new Untitled1());
JScrollPane jsp = new JScrollPane(jTable1);
public Frame2() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
contentPane.add(jsp, java.awt.BorderLayout.CENTER);
}
}
import javax.swing.table.AbstractTableModel;
import java.util.Vector;
import java.sql.*;
import javax.swing.JOptionPane;
public class Untitled1 extends AbstractTableModel {
private Vector title;
private Vector data;
Connection con;
Statement st;
ResultSet rs;
ResultSetMetaData rsmd;
public Untitled1() {
super();
}
public void myTable()
{
try
{
String str = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(str);
}
catch(ClassNotFoundException ce)
{
JOptionPane.showMessageDialog(null,ce.toString());
}
try
{
con = DriverManager.getConnection("jdbc:odbc:NetBar");
st = con.createStatement();
rs = st.executeQuery("SELECT * FROM Card");
rsmd = rs.getMetaData();
for(int i = 0;i<rsmd.getColumnCount();i++)
{
title.add(rsmd.getColumnLabel(i));
}
while(rs.next())
{
for(int j = 0;j<rsmd.getColumnCount();j++)
data.add(rs.getString(j));
}
}
catch(SQLException se)
{
JOptionPane.showMessageDialog(null,se.toString());
}
}
public int getColumnCount() {
return title.size();
}
public int getRowCount() {
return data.size()/getColumnCount();
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get((rowIndex*getColumnCount())+columnIndex);
}
public String getColumnName(int columnIndex) {
String colName = new String();
if (columnIndex<=getColumnCount())
colName=(String)title.get(columnIndex);
return colName;
}
}
报告空指针异常!谁能给我看看啊!
----------------解决方案--------------------------------------------------------
空指针异常 那就是你那个地方的变量没有初始化就引用了吧.....
----------------解决方案--------------------------------------------------------
试过了 没有用!这鸟问题搞了我5天了还没有会 痛苦啊!!!!
----------------解决方案--------------------------------------------------------
写程序要仔细
----------------解决方案--------------------------------------------------------
你的主程序呢!!!这样我不能运行啊....
不知道具体是什么问题!!
----------------解决方案--------------------------------------------------------
.....大概看了下 问题好像是
for(int i = 0;i<rsmd.getColumnCount();i++)
{
title.add(rsmd.getColumnLabel(i));
}
while(rs.next())
{
for(int j = 0;j<rsmd.getColumnCount();j++)
data.add(rs.getString(j));
}
记得ResultSet列是要从1开始计数的 不是从0吧
[此贴子已经被作者于2006-12-7 17:06:46编辑过]
----------------解决方案--------------------------------------------------------
问题已经解决了 我开个新帖子发出来!!!!
----------------解决方案--------------------------------------------------------