import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class MyDictionary extends JFrame implements ActionListener{
TextField tfind=new TextField(16);
TextField tshow=new TextField(16);
Label lfind=new Label("请输入待查找的单词");
Label lshow=new Label("该英文单词的中文解释");
TextField tupdate=new TextField(16);
TextField tupdate1=new TextField(16);
Label lupdate=new Label("请输入要更新的单词");
Label lupdate1=new Label("请输入更新后的中文解释");
TextField tadd=new TextField(16);
TextField tadd1=new TextField(16);
Label ladd=new Label("请输入新增的单词");
Label ladd1=new Label("请输入新增单词的中文解释");
Button bfind=new Button("查词");
Button bupdate=new Button("更新");
Button badd=new Button("新增");
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Statement pstmt;
ResultSet rs;
MyDictionarydatebase mdd=new MyDictionarydatebase(pstmt,rs); public MyDictionary(String title){
super(title);
this.setLayout(new GridLayout(3,1));
this.add(p1);
this.add(p2);
this.add(p3);
p1.add(lfind);p1.add(tfind);p1.add(lshow);p1.add(tshow);p1.add(bfind);
p2.add(lupdate);p2.add(tupdate);p2.add(lupdate1);p2.add(tupdate1);p2.add(bupdate);
p3.add(ladd);p3.add(tadd);p3.add(ladd1);p3.add(tadd1);p3.add(badd);
bfind.addActionListener(this);
bupdate.addActionListener(this);
badd.addActionListener(this);
this.setBounds(200,220,650,220);
this.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
try{
if(ae.getSource()==bfind){
while(mdd.rs.next()){
if(mdd.rs.getString("word")==tfind.getText()){
tshow.setText(mdd.rs.getString("chiexp"));
}
else tshow.setText("找不到要查找的单词");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
new MyDictionary("我的英汉词典");
}
}class MyDictionarydatebase{
Connection conn=null;
Statement pstmt=null;
ResultSet rs=null;
public MyDictionarydatebase(Statement pstmt,ResultSet rs){
this.pstmt=pstmt;
this.rs=rs;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/mydatabase?useUnicode=true&characterEncoding=UTF-8";
conn=DriverManager.getConnection(url,"root","root");
pstmt=conn.createStatement();
rs=pstmt.executeQuery("select*from dictionary");
}catch(ClassNotFoundException e){
System.out.println("找不到系统驱动包");
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
编译没错,运行报空指针错误,指示mdd.rs.next()那一行
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class MyDictionary extends JFrame implements ActionListener{
TextField tfind=new TextField(16);
TextField tshow=new TextField(16);
Label lfind=new Label("请输入待查找的单词");
Label lshow=new Label("该英文单词的中文解释");
TextField tupdate=new TextField(16);
TextField tupdate1=new TextField(16);
Label lupdate=new Label("请输入要更新的单词");
Label lupdate1=new Label("请输入更新后的中文解释");
TextField tadd=new TextField(16);
TextField tadd1=new TextField(16);
Label ladd=new Label("请输入新增的单词");
Label ladd1=new Label("请输入新增单词的中文解释");
Button bfind=new Button("查词");
Button bupdate=new Button("更新");
Button badd=new Button("新增");
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Statement pstmt;
ResultSet rs;
MyDictionarydatebase mdd=new MyDictionarydatebase(pstmt,rs); public MyDictionary(String title){
super(title);
this.setLayout(new GridLayout(3,1));
this.add(p1);
this.add(p2);
this.add(p3);
p1.add(lfind);p1.add(tfind);p1.add(lshow);p1.add(tshow);p1.add(bfind);
p2.add(lupdate);p2.add(tupdate);p2.add(lupdate1);p2.add(tupdate1);p2.add(bupdate);
p3.add(ladd);p3.add(tadd);p3.add(ladd1);p3.add(tadd1);p3.add(badd);
bfind.addActionListener(this);
bupdate.addActionListener(this);
badd.addActionListener(this);
this.setBounds(200,220,650,220);
this.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
try{
if(ae.getSource()==bfind){
while(mdd.rs.next()){
if(mdd.rs.getString("word")==tfind.getText()){
tshow.setText(mdd.rs.getString("chiexp"));
}
else tshow.setText("找不到要查找的单词");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
new MyDictionary("我的英汉词典");
}
}class MyDictionarydatebase{
Connection conn=null;
Statement pstmt=null;
ResultSet rs=null;
public MyDictionarydatebase(Statement pstmt,ResultSet rs){
this.pstmt=pstmt;
this.rs=rs;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/mydatabase?useUnicode=true&characterEncoding=UTF-8";
conn=DriverManager.getConnection(url,"root","root");
pstmt=conn.createStatement();
rs=pstmt.executeQuery("select*from dictionary");
}catch(ClassNotFoundException e){
System.out.println("找不到系统驱动包");
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
编译没错,运行报空指针错误,指示mdd.rs.next()那一行
this.rs=rs;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/mydatabase?useUnicode=true&characterEncoding=UTF-8";
conn=DriverManager.getConnection(url,"root","root");
pstmt=conn.createStatement();
rs=pstmt.executeQuery("select*from dictionary");
你这里的 this.rs还是null,所以,会抛nullPoint,应该把this.rs=rs; 这句放到rs=pstmt.executeQuery("select*from dictionary");
的后面,还有,mdd.rs.getString("word")==tfind.getText()这个判断很有问题,String类的判断,这里最好用
mdd.rs.getString("word".equals(tfind.getText());
{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(Exception e){
e.printStackTrace();
}
} }把这个方法的调用哪个放到“try{
if(ae.getSource()==bfind){
while(mdd.rs.next()){
if(mdd.rs.getString("word")==tfind.getText()){
tshow.setText(mdd.rs.getString("chiexp"));
} else tshow.setText("找不到要查找的单词");
} ”这里的后面。
试下
mdd.rs.getString("word".equals(tfind.getText()); 这样确实能好点