当前位置: 技术问答>java相关
JBuilder7中数据库操作的一个问题
来源: 互联网 发布时间:2015-10-28
本文导语: 下面是我在类Frame1中的全部源代码(只是一个很小的程序) import java.awt.*; import javax.swing.*; import com.borland.jbcl.layout.*; import java.sql.*; import java.awt.event.*; public class Frame1 extends JFrame { private BoxLayout2 boxLayout21 =...
下面是我在类Frame1中的全部源代码(只是一个很小的程序)
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.sql.*;
import java.awt.event.*;
public class Frame1 extends JFrame {
private BoxLayout2 boxLayout21 = new BoxLayout2();
ResultSet rs;
private JTextField jTextField1 = new JTextField();
private JButton jButton1 = new JButton();
public Frame1() {
try {
jbInit();
this.show();
this.pack();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
String url="jdbc:odbc:test";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url,"","");
String sql="select name,age from test";
Statement sta=con.createStatement();
ResultSet rs=sta.executeQuery(sql);
}
catch(Exception ex){
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
jTextField1.setText("jTextField1");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(boxLayout21);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.getContentPane().add(jTextField1, null);
this.getContentPane().add(jButton1, null);
}
public void setData(){//点击jButton1时置jTextField1中文本为数据库中的值
try{
while(rs.next)
jTextField1.setText(rs.getString(1));
}
catch(Exception ex){
ex.printStackTrace();
}
}
void jButton1_actionPerformed(ActionEvent e) {
this.setData();
}
}
编译时无错,可是当我点击按钮jButton1时出现错误提示如下:
java.lang.NullPointerException
at wmr.Frame1.setData(Frame1.java:66)
at wmr.Frame1.jButton1_actionPerformed(Frame1.java:74)
at wmr.Frame1$1.actionPerformed(Frame1.java:57)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)
at java.awt.Component.processMouseEvent(Component.java:3715)
at java.awt.Component.processEvent(Component.java:3544)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2593)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1200)
at java.awt.Window.dispatchEventImpl(Window.java:914)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)
请您帮我分析一下是什麽原因,是不是需要配置JBuilder7还是...?
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.sql.*;
import java.awt.event.*;
public class Frame1 extends JFrame {
private BoxLayout2 boxLayout21 = new BoxLayout2();
ResultSet rs;
private JTextField jTextField1 = new JTextField();
private JButton jButton1 = new JButton();
public Frame1() {
try {
jbInit();
this.show();
this.pack();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
String url="jdbc:odbc:test";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url,"","");
String sql="select name,age from test";
Statement sta=con.createStatement();
ResultSet rs=sta.executeQuery(sql);
}
catch(Exception ex){
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
jTextField1.setText("jTextField1");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(boxLayout21);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.getContentPane().add(jTextField1, null);
this.getContentPane().add(jButton1, null);
}
public void setData(){//点击jButton1时置jTextField1中文本为数据库中的值
try{
while(rs.next)
jTextField1.setText(rs.getString(1));
}
catch(Exception ex){
ex.printStackTrace();
}
}
void jButton1_actionPerformed(ActionEvent e) {
this.setData();
}
}
编译时无错,可是当我点击按钮jButton1时出现错误提示如下:
java.lang.NullPointerException
at wmr.Frame1.setData(Frame1.java:66)
at wmr.Frame1.jButton1_actionPerformed(Frame1.java:74)
at wmr.Frame1$1.actionPerformed(Frame1.java:57)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)
at java.awt.Component.processMouseEvent(Component.java:3715)
at java.awt.Component.processEvent(Component.java:3544)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2593)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1200)
at java.awt.Window.dispatchEventImpl(Window.java:914)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)
请您帮我分析一下是什麽原因,是不是需要配置JBuilder7还是...?
|
ODBC中你建立test文件了吗?如果没建的话,就会出现的。