Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 数据库记录修改
liulianju





发贴: 3
积分: 0
于 2006-08-01 10:42 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
大家好!我是java初学者,看了有关基础方面的书。自己试着做点练习,遇到了点麻烦,请大家帮忙!
我的程序是这样的,在用户界面,用户输入要修改的内容,然后将保存结果保存到数据库中,但是运行时出现异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at studentManage.DBManager.modify(DBManager.java:90)
  at StudentUI.modifyDialog.actionPerformed(modifyDialog.java:74)
不知道错在哪里,希望给予指点
用户界面主要代码为:
public class modifyDialog extends JFrame implements ActionListener{
  JTextField id_input;
  JTextField t_name;
  JTextField t_age;
  JButton modify;
  JButton cancel;
  JButton query;
  public String idInput;
  public String nameInput;
  public int ageInput;
  public static void main(String args[])
  {
    modifyDialog modify=new modifyDialog();
    modify.show();
  }
  public modifyDialog()
{
  setTitle("学生信息修改");
setLayout(new FlowLayout() );
JLabel input=new JLabel("请输入学号");
id_input=new JTextField(10);
JLabel name=new JLabel("姓名");
t_name=new JTextField(10);
JLabel age=new JLabel("年龄");
t_age=new JTextField(10);
query=new JButton("查询");
modify=new JButton("修改");
cancel=new JButton("取消");
query.addActionListener(this);
modify.addActionListener(this);
cancel.addActionListener(this);
add(input);
add(id_input);
add(query);
add(name);
add(t_name);
add(age);
add(t_age);
add(modify);
add(cancel);
setSize(300,200);
}
public void actionPerformed(ActionEvent e)
{
  if(e.getSource()==query)
  {
    try{
     Student stu=new Student();
     idInput=id_input.getText();
      stu.findStudentByID(idInput);
      t_name.setText(stu.name);
      t_age.setText(Integer.toString(stu.age));
  }catch(SQLException sql)
  {
    sql.printStackTrace();
  }
  }
  else
    if(e.getSource()==modify)
      {
      idInput=id_input.getText();
      nameInput=t_name.getText();
      ageInput=Integer.parseInt(t_age.getText());
      try {
        DBManager manage=new DBManager();
        manage.modify(idInput,nameInput,ageInput);
      } catch (SQLException e1) {
        e1.printStackTrace();
      }
      }
  else if(e.getSource()==cancel)
  {
    dispose();
    System.exit(0);
  }  
}

数据库管理类中modify方法为:
public void modify(String id,String name,int age)throws SQLException{
  try{
  String ms;
  ms="update liulianju.student set name=?,age=? where student.id=?";
  PreparedStatement mp=con.prepareStatement(ms);
  mp.setString(1,name);
  mp.setInt(2, age);
  mp.setString(3,id);
  mp.executeUpdate();
  mp.close();
  con.close();
  }
  catch(SQLException se)
  {
    se.printStackTrace();
  }
}



作者 Re:数据库记录修改 [Re:liulianju]
zjh3269





发贴: 4
积分: 0
于 2006-10-27 23:51 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
试试把 conn.prepareStatement(ms) 中的 ms 替换成那条 SQL 语句


作者 Re:数据库记录修改 [Re:liulianju]
sunsideupzxl





发贴: 2
积分: 0
于 2006-11-03 11:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你的数据库的驱动程序是否加载正确?数据库的地址,用户名,密码是不下正确?这都很关键的.


作者 Re:数据库记录修改 [Re:liulianju]
cxp108





发贴: 363
积分: 12
于 2006-11-03 15:20 user profilesend a private message to usersend email to cxp108search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
最后可能犯的错误是con根本没有被正确初始化,始终是null

还有一种可能:con.close()执行前,con已经被关掉,并且被JVM回收了,或者studentManage.DBManager中的其他方法将con指成null。




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923