import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
class Update extends JFrame implements ActionListener
{
JFrame f;
Container cp;
JPanel jp1,jp2,jp3,jp4,jp,jpanelWest;
JButton jbt1,jbt2;//按钮,确定、取消
JLabel label;//标签:修改密码
JTextField tf0,tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;
String sno;
Update(){}
Update(String bookno)
{
sno=bookno;
f=new JFrame();
cp=f.getContentPane(); // 初始化
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp4=new JPanel();
jpanelWest=new JPanel();
jp=new JPanel();
jbt1=new JButton("确定");
jbt2=new JButton("取消");
int size=20;
label=new JLabel("修改图书信息",SwingConstants.CENTER);
label.setFont(new Font("修改图书信息",Font.PLAIN,size));//加粗,字体大小20
label.setForeground(Color.blue);
tf1=new JTextField(20);
tf2=new JTextField(20);
tf3=new JTextField(20);
tf4=new JTextField(20);
tf5=new JTextField(20);
tf6=new JTextField(20);
tf7=new JTextField(20);
tf8=new JTextField(20);
tf9=new JTextField(20);
tf10=new JTextField(20);
//布局,添加控件
jp1.add(jbt1);
jp1.add(jbt2);
jp1.add(new JLabel("欢迎登陆"));
JPanel jpanel=new JPanel();
jpanel.add(label);
JPanel pp4=new JPanel();
JPanel jpane4=new JPanel();
cp.add(jpanel,"North");
JPanel pp2=new JPanel(new GridLayout(6,1));
pp4.setLayout(new GridLayout(6,1));
pp4.add(new JLabel("输入要修改的图书号: ",SwingConstants.RIGHT));
pp2.add(tf0);
pp4.add(new JLabel("原书名: ",SwingConstants.RIGHT));
pp2.add(tf1);
pp4.add(new JLabel("现书名: ",SwingConstants.RIGHT));
pp2.add(tf2);
pp4.add(new JLabel("原单价: ",SwingConstants.RIGHT));
pp2.add(tf3);
pp4.add(new JLabel("现单价: ",SwingConstants.RIGHT));
pp2.add(tf4);
pp4.add(new JLabel("原作者: ",SwingConstants.RIGHT));
pp2.add(tf5);
pp4.add(new JLabel("现作者: ",SwingConstants.RIGHT));
pp2.add(tf6);
pp4.add(new JLabel("原出版社: ",SwingConstants.RIGHT));
pp2.add(tf7);
pp4.add(new JLabel("现出版社: ",SwingConstants.RIGHT));
pp2.add(tf8);
pp4.add(new JLabel("原入库时间: ",SwingConstants.RIGHT));
pp2.add(tf9);
pp4.add(new JLabel("现入库时间: ",SwingConstants.RIGHT));
pp2.add(tf10);
pp2.add(new JLabel());
JPanel jpbutton=new JPanel();
jpbutton.add(jbt1);
jpbutton.add(jbt2);
pp2.add(jpbutton);
cp.add(pp4,"West");
cp.add(pp2,"Center");
cp.add(jpane4,"East");
f.setSize(500,450);
f.setVisible(true);
jbt1.addActionListener(this);//注册监听器
jbt2.addActionListener(this);
}
public void updateM()
{
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){System.out.println("加载驱动程序失败!");}
try{
Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=Book","sa","xuqianqian");
Statement sql=con.createStatement();
String utf0=tf0.getText().trim();
String queryMima="select * from book where 图书号='"+utf0+"'";
ResultSet rs=sql.executeQuery(queryMima);
if(rs.next())
{
String newname=tf2.getText().trim();
String newcost=tf4.getText().trim();
String newauthor=tf6.getText().trim();
String newchuban=tf8.getText().trim();
String newruku=tf10.getText().trim();
String s="update book set 图书名='"+newname+"',作者='"+newauthor+"',单价='"+newcost+"',出版社='"+newchuban+"',入库时间='"+newruku+"' where 图书号 ='"+utf0+"'";
sql=con.createStatement();
int update=sql.executeUpdate(s);
if(update==1)
{
JOptionPane.showMessageDialog(f,"图书信息修改成功!");
}
con.close();
f.repaint();
}else{
JOptionPane.showMessageDialog(null,"该用户不存在","警告!",
JOptionPane.YES_NO_OPTION);
}
tf0.setText("");
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
tf6.setText("");
tf7.setText("");
tf8.setText("");
tf9.setText("");
tf10.setText("");
} catch(SQLException g)
{
System.out.println("E Code"+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("确定"))
{
if(tf0.getText().equals("")||tf1.getText().equals("")||tf2.getText().equals("")||tf3.getText().equals("")||tf4.getText().equals("")||tf5.getText().equals("")||tf6.getText().equals("")||tf7.getText().equals("")||tf8.getText().equals("")||tf9.getText().equals("")||tf10.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"请填写图书的所有信息","提示",
JOptionPane.YES_NO_OPTION);
return;
}
}
else if(cmd.equals("取消"))
f.hide();
}
public static void main(String[]args)
{
new Update("");
}
}
报错:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1045)
at java.awt.Container.add(Container.java:365)
at Update.<init>(Update.java:57)
at Update.main(Update.java:157)
请问哪里有问题~~
|