当前位置: 技术问答>java相关
谁能告诉我这个错误是什么原因造成的 怎么修改
来源: 互联网 发布时间:2015-07-30
本文导语: 以下是java2核心技术这本书中的一个例子 但总是提示出错 出错信息为“java.lang.NullPointerException”请问出现这种错误的原因是什么改怎么改 import java.sql.*; import java.awt.*; import java.awt.event.*; import java.io.*; import...
以下是java2核心技术这本书中的一个例子 但总是提示出错 出错信息为“java.lang.NullPointerException”请问出现这种错误的原因是什么改怎么改
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
public class ViewDB{
public static void main(String[] args)
{
JFrame frame= new ViewDBFrame();
frame.show();
}
}
class ViewDBFrame extends JFrame implements ActionListener{
public ViewDBFrame()
{
setTitle("ViewDB");
setSize(300,200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
Container contentPane = getContentPane();
tablenames= new JComboBox();
tablenames.addActionListener(this);
dataPanel= new JPanel();
contentPane.add(dataPanel,"Center");
nextButton= new JButton("Next");
nextButton.addActionListener(this);
JPanel p= new JPanel();
p.add(nextButton);
contentPane.add(p,"South");
fields = new ArrayList();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex)
{
JOptionPane.showMessageDialog(this,ex);
}
try
{
String url="jdbc:odbc:db1";
Connection conn=DriverManager.getConnection(url);
Statement stmt= conn.createStatement();
md=conn.getMetaData();
ResultSet mrs= md.getTables(null,null,null,new String [] {"TABLE"});
while(mrs.next())
{
tablenames.addItem(mrs.getString(3));
}
mrs.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,e);
}
contentPane.add(tablenames,"North");
}
private void add(Container p,Component c,GridBagConstraints gbc,int x,int y,int w,int h)
{
gbc.gridx=x;
gbc.gridy=y;
gbc.gridwidth=w;
gbc.gridheight=h;
p.add(c,gbc);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource()==nextButton)
{
showNextRow();
}
else if (evt.getSource()==tablenames)
{
remove(dataPanel);
dataPanel=new JPanel();
fields.clear();
dataPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc= new GridBagConstraints();
gbc.weighty=100;
try
{
String tablename=(String)tablenames.getSelectedItem();
if (rs!=null) rs.close();
rs=stmt.executeQuery("select * from " + tablename);
ResultSetMetaData rsmd=rs.getMetaData();
for (int i=1;i
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
public class ViewDB{
public static void main(String[] args)
{
JFrame frame= new ViewDBFrame();
frame.show();
}
}
class ViewDBFrame extends JFrame implements ActionListener{
public ViewDBFrame()
{
setTitle("ViewDB");
setSize(300,200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
Container contentPane = getContentPane();
tablenames= new JComboBox();
tablenames.addActionListener(this);
dataPanel= new JPanel();
contentPane.add(dataPanel,"Center");
nextButton= new JButton("Next");
nextButton.addActionListener(this);
JPanel p= new JPanel();
p.add(nextButton);
contentPane.add(p,"South");
fields = new ArrayList();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex)
{
JOptionPane.showMessageDialog(this,ex);
}
try
{
String url="jdbc:odbc:db1";
Connection conn=DriverManager.getConnection(url);
Statement stmt= conn.createStatement();
md=conn.getMetaData();
ResultSet mrs= md.getTables(null,null,null,new String [] {"TABLE"});
while(mrs.next())
{
tablenames.addItem(mrs.getString(3));
}
mrs.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,e);
}
contentPane.add(tablenames,"North");
}
private void add(Container p,Component c,GridBagConstraints gbc,int x,int y,int w,int h)
{
gbc.gridx=x;
gbc.gridy=y;
gbc.gridwidth=w;
gbc.gridheight=h;
p.add(c,gbc);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource()==nextButton)
{
showNextRow();
}
else if (evt.getSource()==tablenames)
{
remove(dataPanel);
dataPanel=new JPanel();
fields.clear();
dataPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc= new GridBagConstraints();
gbc.weighty=100;
try
{
String tablename=(String)tablenames.getSelectedItem();
if (rs!=null) rs.close();
rs=stmt.executeQuery("select * from " + tablename);
ResultSetMetaData rsmd=rs.getMetaData();
for (int i=1;i