M
maomaoyu810201
Unregistered / Unconfirmed
GUEST, unregistred user!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class TestJtable extends JFrame implements ActionListener{
JTable table;
JButton button1,button2;
JLabel label;
JPanel panel,panel2;
JScrollPane scrollpane;
Statement stmt;
Connection conn;
ResultSet rs;
Object [][] data;
String [] name;
int j=0;
public TestJtable()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbcdbc:MS Access Database","","");
stmt=conn.createStatement();
String sql="select * from userinfo";
rs = stmt.executeQuery(sql);
}
catch (Exception ex) { System.err.println(ex.getMessage());}
try{
while (rs.next()){
for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
{
data[j]=rs.getObject(i);
}
j++;
}//读取数据
for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
{ name=rs.getMetaData().getColumnName(i);}
//读取字段名
}
catch(Exception ex){System.err.println(ex.getMessage());}
Container con=this.getContentPane();
label=new JLabel("Wellcome");
button1=new JButton("Find");
button2=new JButton("Exit");
panel=new JPanel();
panel2=new JPanel();
table=new JTable(data,name);
scrollpane=new JScrollPane(table);
button2.addActionListener(this);
panel.setLayout(new FlowLayout());
panel.add(button1);
panel.add(button2);
panel2.add(label,BorderLayout.CENTER);
con.add(panel2,BorderLayout.NORTH);
con.add(new JPanel(),BorderLayout.WEST);
con.add(new JPanel(),BorderLayout.EAST);
con.add(scrollpane,BorderLayout.CENTER);
con.add(panel,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button2)
{System.exit(0);}
}
public static void main(String []args)
{
TestJtable frm=new TestJtable();
frm.pack();
frm.show();
}
}
数据库连接没问题,编译能过,但运行时出现错误提示:
null
Exception in thread "main" java.lang.NullPointerException
at javax.swing.JTable$1.getColumnCount(JTable.java:423)
at javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:936)
at javax.swing.JTable.tableChanged(JTable.java:2853)
at javax.swing.JTable.setModel(JTable.java:2721)
at javax.swing.JTable.<init>(JTable.java:368)
at javax.swing.JTable.<init>(JTable.java:308)
at javax.swing.JTable.<init>(JTable.java:420)
at TestJtable.<init>(TestJtable.java:53)
at TestJtable.main(TestJtable.java:83)
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class TestJtable extends JFrame implements ActionListener{
JTable table;
JButton button1,button2;
JLabel label;
JPanel panel,panel2;
JScrollPane scrollpane;
Statement stmt;
Connection conn;
ResultSet rs;
Object [][] data;
String [] name;
int j=0;
public TestJtable()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbcdbc:MS Access Database","","");
stmt=conn.createStatement();
String sql="select * from userinfo";
rs = stmt.executeQuery(sql);
}
catch (Exception ex) { System.err.println(ex.getMessage());}
try{
while (rs.next()){
for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
{
data[j]=rs.getObject(i);
}
j++;
}//读取数据
for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
{ name=rs.getMetaData().getColumnName(i);}
//读取字段名
}
catch(Exception ex){System.err.println(ex.getMessage());}
Container con=this.getContentPane();
label=new JLabel("Wellcome");
button1=new JButton("Find");
button2=new JButton("Exit");
panel=new JPanel();
panel2=new JPanel();
table=new JTable(data,name);
scrollpane=new JScrollPane(table);
button2.addActionListener(this);
panel.setLayout(new FlowLayout());
panel.add(button1);
panel.add(button2);
panel2.add(label,BorderLayout.CENTER);
con.add(panel2,BorderLayout.NORTH);
con.add(new JPanel(),BorderLayout.WEST);
con.add(new JPanel(),BorderLayout.EAST);
con.add(scrollpane,BorderLayout.CENTER);
con.add(panel,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button2)
{System.exit(0);}
}
public static void main(String []args)
{
TestJtable frm=new TestJtable();
frm.pack();
frm.show();
}
}
数据库连接没问题,编译能过,但运行时出现错误提示:
null
Exception in thread "main" java.lang.NullPointerException
at javax.swing.JTable$1.getColumnCount(JTable.java:423)
at javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:936)
at javax.swing.JTable.tableChanged(JTable.java:2853)
at javax.swing.JTable.setModel(JTable.java:2721)
at javax.swing.JTable.<init>(JTable.java:368)
at javax.swing.JTable.<init>(JTable.java:308)
at javax.swing.JTable.<init>(JTable.java:420)
at TestJtable.<init>(TestJtable.java:53)
at TestJtable.main(TestJtable.java:83)