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;
}
}
报告空指针异常!谁能给我看看啊!