问题描述
- eclipse对数据库sql server2005的增删改查
-
package Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.awt.*;import javax.swing.*;
public class data extends JFrame implements ActionListener{
//private static final String DriveManager = null;
JButton add,select,del,update;
JTable table;
//JComboBox fieldsbox,valuebox;
Object body[][]=new Object[50][5];
String fields[]={"sno","sname","sex","age","dept"};
Connection conn;
Statement st;
ResultSet rs;
JTabbedPane tp;public data(){ super("数据库操作"); this.setSize(400,300); this.setLocation(300, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel ps=new JPanel(); add=new JButton("添加"); select=new JButton("显示"); update=new JButton("更改"); del=new JButton("删除"); //fieldsbox=new JComboBox(fields); //valuebox=new JComboBox(); //fieldsbox.addActionListener(this); add.addActionListener(this); select.addActionListener(this); update.addActionListener(this); del.addActionListener(this); //ps.add(fieldsbox); //ps.add(valuebox); ps.add(add); ps.add(select); ps.add(update); ps.add(del); table=new JTable(body,fields); tp=new JTabbedPane(); tp.add("s表", new JScrollPane(table)); this.getContentPane().add(tp, "Center"); this.getContentPane().add(ps,"South"); this.setVisible(true); this.connection(); } public void connection(){ try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDrive"); String url="jdbc:sqlserver://localhost:1433;DatabaseName=XSGL"; //Connection conn=null; conn=DriverManager.getConnection(url, "sa", "123456"); st= conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); } catch(Exception ex){ System.out.print("连接错误!"); } } public static void main(String[] args) {data data=new data();} public void actionPerformed(ActionEvent e){ //if(e.getSource()==fieldsbox){ //fieldselect(); //} //if(e.getSource()==valuebox){ //valueselect(); //} if(e.getSource()==add){ add(); } if(e.getSource()==select){ select(); } if(e.getSource()==update){ update(); } if(e.getSource()==del){ del(); } } /*private void select() { // TODO 自动生成的方法存根 } private void add() { // TODO 自动生成的方法存根 }*/ /*public void fieldselect(){ valuebox.removeActionListener(this); valuebox.removeAllItems(); String condition=(String)fieldsbox.getSelectedItem(); String str="select distinct"+condition+"from s"; try{ ResultSet rs=st.executeQuery(str); while(rs.next());{ valuebox.addItem((String)rs.getString(1)); } valuebox.addActionListener(this); }catch(Exception e3){ e3.printStackTrace();} }
public void valueselect(){
String val=(String)valuebox.getSelectedItem();
String str="select * from s where"+fieldsbox.getSelectedItem()+"='"+val+"'";
filltable(str);
}*/
public void del(){
try{
int row=table.getSelectedRow();
String str="delete s where SNO='"+body[row][0]+"'";
st.executeUpdate(str);
JOptionPane.showMessageDialog(null, "数据已成功删除");
this.select();
}catch(SQLException ex){
JOptionPane.showMessageDialog(null,"删除数据错误!");
}
}public void update(){ try{ int row=table.getSelectedRow(); JTextField t[]=new JTextField[8]; t[0]=new JTextField("输入姓名:"); t[0].setEditable(false); t[1]=new JTextField(); t[1].setText((String)body[row][1]); t[2]=new JTextField("输入性别:"); t[2].setEditable(false); t[3]=new JTextField(); t[3].setText((String)body[row][2]); t[4]=new JTextField("输入年龄:"); t[4].setEditable(false); t[5]=new JTextField(); t[5].setText((String)body[row][3]); t[6]=new JTextField("输入专业:"); t[6].setEditable(false); t[7]=new JTextField(); t[7].setText((String)body[row][4]); String but[]={"确定","取消"}; int go=JOptionPane.showOptionDialog(null, t,"修改信息",JOptionPane.YES_OPTION,JOptionPane.INFORMATION_MESSAGE,null,but,but[0]); if(go==0){ String nName=(String)t[1].getText(); String nsex=(String)t[3].getText(); int nage=Integer.parseInt(t[5].getText()); String ndept=(String)t[7].getText(); String str="update s set sname='"+nName+"',sex='"+nsex+"',age='"+nage+"',dept='"+ndept+"'where sno='"+body[row][0]+"'"; st.executeUpdate(str); JOptionPane.showMessageDialog(null, "修改数据成功!"); this.select(); } /* String str="insert into S values('"+nsno+"','"+nName+"','"+nsex+nsex+"','"+nage+"','"+ndept+"')"; st.executeUpdate(str); JOptionPane.showMessageDialog(null, "数据已成功插入!"); }catch(Exception ee){ JOptionPane.showInternalMessageDialog(null, "插入数据错误!"); } }*/ }catch(Exception ex){ ex.printStackTrace(); JOptionPane.showMessageDialog(null, "更新数据失败!"); } } public void select(){ String str=("select * from S "); filltable(str); } public void add(){ try{ JTextField t[]=new JTextField[10]; t[0]=new JTextField("输入学号:"); t[0].setEditable(false); t[1]=new JTextField(); t[2]=new JTextField("输入姓名:"); t[2].setEditable(false); t[3]=new JTextField(); t[4]=new JTextField("输入性别:"); t[4].setEditable(false); t[5]=new JTextField(); t[6]=new JTextField("输入年龄:"); t[6].setEditable(false); t[7]=new JTextField(); t[8]=new JTextField("输入专业:"); t[8].setEditable(false); t[9]=new JTextField(); String but[]={"确定","取消"}; int go=JOptionPane.showOptionDialog(null, t,"插入信息",JOptionPane.YES_OPTION,JOptionPane.INFORMATION_MESSAGE,null,but,but[0]); if(go==0){ try{ String nsno=(String)t[1].getText(); String nName=(String)t[3].getText(); int nsex=Integer.parseInt(t[5].getText()); int nage=Integer.parseInt(t[7].getText()); String ndept=(String)t[9].getText(); String str="insert into s values('"+nsno+"','"+nName+"','"+nsex+nsex+"','"+nage+"','"+ndept+"')"; st.executeUpdate(str); JOptionPane.showMessageDialog(null, "数据已成功插入!"); }catch(Exception ee){ JOptionPane.showInternalMessageDialog(null, "插入数据错误!"); } } }catch(Exception ex){} } public void filltable(String s){ try{ for(int x=0;x<body.length;x++){ body[x][0]=null; body[x][1]=null; body[x][2]=null; body[x][3]=null; body[x][4]=null; } int i=0; rs=st.executeQuery(s); while(rs.next()){ body[i][0]=rs.getString("SNO"); body[i][1]=rs.getString("SNAME"); body[i][2]=rs.getString("SEX"); body[i][3]=rs.getString("AGE"); body[i][4]=rs.getString("DEPT"); i=i+1; } this.repaint(); }catch(SQLException ex){ ex.printStackTrace();} } }
解决方案
应该是更新数据时,某一个参数空值了,你调试,看一下哪个是空值就行了
解决方案二:
帮看一下问题出现在哪,如果可以帮忙改改。
解决方案三:
你不将错误信息贴出来,弄一大堆代码是没有人愿意看的
解决方案四:
错误信息是
连接错误!Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Frame.data.filltable(data.java:232)
at Frame.data.select(data.java:180)
at Frame.data.actionPerformed(data.java:79)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
解决方案五:
某一个参数空值了,你调试,看一下哪个是空值就行了