housonjia 2015-06-03 05:48 采纳率: 33.3%
浏览 2888
已采纳

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();}
          }
      }
  • 写回答

5条回答

  • 程序猿小亮 Java领域优质创作者 2015-06-03 06:26
    关注

    应该是更新数据时,某一个参数空值了,你调试,看一下哪个是空值就行了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?