请教Java操作sql数据库的问题
[code=Java][/code]
程序运行后单击button,数据库中内容返回不到JTextArea中,并出现以下错误,请高手指导.
java.lang.NullPointerException at bus.test.actionPerformed(test.java:54)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
以下是程序代码:
public class Odbc
{
private Connection con = null;
public Connection getConnection()
{
String url = "jdbc:odbc:bond";//数据库URL
String userName = "sa";//登录数据库用户名
String password = "";//用户密码
try
{
//登录JDBC-ODBC驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, userName, password);
}
catch(
SQLException e)
{
e.printStackTrace();
}
catch(
ClassNotFoundException ex)
{
ex.printStackTrace();
}
return con;
}
}
public class test extends JFrame implements ActionListener
{
private JButton button;
private JScrollPane resultareaScrollPane;
private JSplitPane SplitPane;
private JTextArea resultarea;
private Odbc sql = new Odbc();
public test()
{
button = new JButton("button");
button.addActionListener(this);
JTextArea resultarea = new JTextArea();
resultarea.setLineWrap(true);
JScrollPane resultareaScrollPane = new JScrollPane(resultarea);
JSplitPane SplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,button,resultareaScrollPane);
this.getContentPane().add(SplitPane);
this.setSize(300,200);
this.setLocation(100,100);
this.setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == button)
{
try
{
Connection con = sql.getConnection();
String command = "select * from student";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(command);
while(rs.next())
{
String reSname = rs.getString("Sname").trim();
resultarea.append(reSname);