Java增删改查数据库内容报错

hugongwang 2017-07-13 03:56:52
package shujuku;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.JTableHeader;
public class Home extends JFrame implements ActionListener{
JScrollPane scpDemo;
JFrame Add;
JTableHeader jth;
JTable tabDemo;
JTextField text,AddName,AddNumber;
JLabel name,number;
JButton anShow,anSet,anSelsct,anClear,anAdd,Addyes,Addno;
String SQLname;
String a[]={SQLname};
int SQLnumber;
int b[]={SQLnumber};

public Home(){
super("管理信息系统基本框架");
this.setSize(400,500);
this.setLayout(null);
this.setLocation(500,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
this.text=new JTextField();
this.text.setBounds(10, 20,200, 25);
this.anSelsct=new JButton("查询");
this.anSelsct.setBounds(230,20,70,25);
this.anShow=new JButton("刷新");
this.anShow.setBounds(310,20,70,25);
this.anAdd=new JButton("添加");
this.anAdd.setBounds(20,435,70,25);
this.anSet=new JButton("修改");
this.anSet.setBounds(100,435,70,25);
this.anClear=new JButton("删除");
this.anClear.setBounds(180,435,70,25);
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(10,50,365,380);
AddName=new JTextField();
AddNumber=new JTextField();
name=new JLabel("用户名");
number=new JLabel("密码");
Addyes=new JButton("确定");
Addno=new JButton("取消");
Add=new JFrame();
Add.setTitle("添加");
Add.setSize(350,400);
Add.setLocation(100, 260);
Add.setLayout(new GridLayout(3,2));
Add.add(name);Add.add(AddName);
Add.add(number);Add.add(AddNumber);
Add.add(Addyes); Add.add(Addno);
Addyes.addActionListener(this);
Addno.addActionListener(this);
anAdd.addActionListener(this);
anShow.addActionListener(this);
anSet.addActionListener(this);
anSelsct.addActionListener(this);
anClear.addActionListener(this);
this.scpDemo.getViewport().add(tabDemo);
add(this.scpDemo);
add(this.text);
add(this.anSelsct);
add(this.anShow);
add(this.anAdd);
add(this.anSet);
add(this.anClear);
this.remove(this);
this.repaint();
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=kuangjia","sa","123456");
String sql="select * from dbo.Table_1";
PreparedStatement pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
int count = 0;
while(rs.next()){
count++;
}
rs = pstm.executeQuery();
Object[][] info = new Object[count][2];
count = 0;
while(rs.next()){
info[count][0] = rs.getString("name");
info[count][1] = Integer.valueOf(rs.getInt("number"));
count++;
}
String[] title = {"用户名","密码"};
this.tabDemo = new JTable(info,title);
this.jth = this.tabDemo.getTableHeader();
this.scpDemo.getViewport().add(tabDemo);
}catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
}catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
chaxun re=new chaxun();
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==(anAdd)){
Add a=new Add(this,"添加学生信息",true);
re=new chaxun();
tabDemo.setModel(re);
}
else if(e.getSource()==(anSelsct)){
String name=this.text.getText().trim();
String sql="select * from dbo.Table_1 where name='"+name+"'";
chaxun aa=new chaxun(sql);
tabDemo.setModel(aa);
}
else if(e.getSource()==anShow){
tabDemo.setModel(re);
}
else if(e.getSource()==(anClear)){
int i=this.tabDemo.getSelectedRow();
clear aa=new clear(i);
re=new chaxun();
tabDemo.setModel(re);
}
else if(e.getSource()==anSet){
int j=this.tabDemo.getSelectedRow();
revise a=new revise(this,"修改学生信息",true,re,j);
re=new chaxun();
tabDemo.setModel(re);
}
}
public static void main(String args[])
{
Home aa=new Home();
}
}

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at shujuku.Home.actionPerformed(Home.java:137)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
每次运行的时候就会显示数据操作错误,然后增加记录之后不能修改,修改时也会显示数据操作错误,下面是修改功能代码
package shujuku;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
public class revise extends JDialog implements ActionListener {
private JTextField setName,setNumber;
private JLabel name,number;
private JButton Addyes,Addno;
private Connection con=null;
private Statement sql=null;
private ResultSet rs=null;
private String SQLname;
private int SQLnumber;
revise(Frame fck,String ckm,Boolean msck,chaxun xx,int j){
super(fck, ckm,msck);
if(j==-1){
JOptionPane.showMessageDialog(null,"请选中要修改的行");
return;
}
setName=new JTextField(15);
setName.setText((String)xx.getValueAt(j,0));
setNumber=new JTextField(15);
setNumber.setText((String)xx.getValueAt(j,1).toString());
setNumber.setEditable(false);
name=new JLabel("用户名");
number=new JLabel("密码");
Addyes=new JButton("确定");
Addno=new JButton("取消");
this.add(name);this.add(setName);
this.add(number);this.add(setNumber);
this.add(Addyes); this.add(Addno);
this.Addyes.addActionListener(this);
this.Addno.addActionListener(this);
this.setSize(350,400);
this.setLocation(100, 260);
this.setLayout(new GridLayout(3,2));
this.setVisible(true);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==Addyes){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=kuangjia","sa","123456");
String recode = " update dbo.Table_1 set name=? where number=?";
PreparedStatement input=con.prepareStatement(recode);
SQLnumber=Integer.parseInt(setNumber.getText());
input.setString(1, setName.getText());
input.setInt(2, SQLnumber); //修改的条件
input.executeUpdate();
JOptionPane.showMessageDialog(null, "修改成功");
this.dispose();
}
catch(NumberFormatException nu){
JOptionPane.showMessageDialog(null,"你还没有输入","错误",JOptionPane.ERROR_MESSAGE);
}
catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
}
catch(SQLException sqle){
sqle.printStackTrace();
JOptionPane.showMessageDialog(null,"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
finally
{
try {
if(rs!=null)
{rs.close();
}
if(sql!=null)
{
sql.close();
}
if(con!=null)
{
con.close();
}

} catch (Exception e1){}
}
}
if(e.getSource()==Addno){
this.dispose();
}
}
}







...全文
226 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hugongwang 2017-07-13
  • 打赏
  • 举报
回复
引用 10 楼 qq_39205291 的回复:
在 rs = pstm.executeQuery(); Object[][] info = new Object[count][2]; count = 0; while(rs.next()){ info[count][0] = rs.getString("name"); info[count][1] = Integer.valueOf(rs.getInt("number")); count++; } String[] title = {"用户名","密码"}; this.tabDemo = new JTable(info,title); 下面加一句 tabDemo=this.tabDemo
感觉没有用啊,还是没啥变化
qq_39205291 2017-07-13
  • 打赏
  • 举报
回复
在 rs = pstm.executeQuery(); Object[][] info = new Object[count][2]; count = 0; while(rs.next()){ info[count][0] = rs.getString("name"); info[count][1] = Integer.valueOf(rs.getInt("number")); count++; } String[] title = {"用户名","密码"}; this.tabDemo = new JTable(info,title); 下面加一句 tabDemo=this.tabDemo
qq_39205291 2017-07-13
  • 打赏
  • 举报
回复
你的这个tabDemo应该要写成全局变量,下面才能用吧
hugongwang 2017-07-13
  • 打赏
  • 举报
回复
引用 7 楼 pany1209 的回复:
[quote=引用 2 楼 hugongwang 的回复:] public void actionPerformed(ActionEvent e) { if(e.getSource()==(anAdd)){ Add a=new Add(this,"添加学生信息",true); re=new chaxun(); tabDemo.setModel(re); } 标红的这一行
tabDemo.setModel(re); 注释掉。。。 打印tabDemo是不是null...先[/quote] 对的,那具体怎么改呢?不怎么会。
李德胜1995 2017-07-13
  • 打赏
  • 举报
回复
引用 2 楼 hugongwang 的回复:
public void actionPerformed(ActionEvent e) { if(e.getSource()==(anAdd)){ Add a=new Add(this,"添加学生信息",true); re=new chaxun(); tabDemo.setModel(re); } 标红的这一行
tabDemo.setModel(re); 注释掉。。。 打印tabDemo是不是null...先
hugongwang 2017-07-13
  • 打赏
  • 举报
回复
引用 4 楼 qq_39205291 的回复:
应该是查询语句出错了,tabDemo对象空了吧,你打印tabDemo看看应该是空的
public void actionPerformed(ActionEvent e) { if(e.getSource()==(anAdd)){ Add a=new Add(this,"添加学生信息",true); re=new chaxun(); System.out.println(tabDemo); tabDemo.setModel(re); } nullException in thread "AWT-EventQueue-0" java.lang.NullPointerException at shujuku.Home.actionPerformed(Home.java:138) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source)
hugongwang 2017-07-13
  • 打赏
  • 举报
回复
没有吧,我这个是查询,然后引用的是name的值
qq_39205291 2017-07-13
  • 打赏
  • 举报
回复
应该是查询语句出错了,tabDemo对象空了吧,你打印tabDemo看看应该是空的
qq_39205291 2017-07-13
  • 打赏
  • 举报
回复
"select * from dbo.Table_1 where name='"+name+"'";感觉你多写了“”号查出来空了吧
hugongwang 2017-07-13
  • 打赏
  • 举报
回复
public void actionPerformed(ActionEvent e) { if(e.getSource()==(anAdd)){ Add a=new Add(this,"添加学生信息",true); re=new chaxun(); tabDemo.setModel(re); } 标红的这一行
李德胜1995 2017-07-13
  • 打赏
  • 举报
回复
at shujuku.Home.actionPerformed(Home.java:137)。。。。actionPerformed方法137行空指针。。是哪一行??

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧