俺写了JDBC连接SQL2000数据库的操作的一段代码,对所连接的数据进行插入操作,谁知出现以下错误提示:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
俺的源代码:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class rong extends JFrame implements ActionListener {
JLabel t_1=new JLabel("姓名");
JLabel t_2=new JLabel("成绩");
JLabel t_3=new JLabel("年龄");
JTextField b_1=new JTextField("");
JTextField b_2=new JTextField("");
JTextField b_3=new JTextField("");
JButton x=new JButton("确定");
JButton y=new JButton("取消");
Connection con = null;
Statement st = null;
ResultSet rs = null;
public rong() throws HeadlessException {
super("插入界面");
Container c=this.getContentPane();
c.setLayout(null);
c.add(t_1);
c.add(t_2);
c.add(t_3);
c.add(b_1);
c.add(b_2);
c.add(b_3);
c.add(x);
c.add(y);
x.addActionListener(this);
y.addActionListener(this);
t_1.setBounds(20,20,100,20);
t_2.setBounds(20, 50, 100,20);
t_3.setBounds(20,80,100,20);
b_1.setBounds(130,20,100,20);
b_2.setBounds(130,50,100,20);
b_3.setBounds(130,80,100,20);
x.setBounds(20,110,100,20);
y.setBounds(130,110,100,20);
this.setLocation(350,200);
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
Object ob=e.getSource();
if(y==ob){
this.dispose();
}
if(x==ob){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs","sa","198277");
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String name=b_1.getText();
int score=Integer.valueOf(b_2.getText());
int age=Integer.valueOf(b_3.getText());
String sql="insert into yy values('"+name+"','"+score+"','"+age+"')";
int rs=st.executeUpdate(sql);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}finally{
try {
rs.close();
st.close();
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}