用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

增删改查

2016-05-10 作者: 画船听雨眠举报

[java]代码库

package www.baidu.com;

import java.awt.Frame;
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.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class revise extends JDialog implements ActionListener {
	  private JTextField setName,setNumber,setSex,setAge,setPost,setWage,setJop;
	  private JLabel name,number,age,sex,wage,post,jop;
	 
	  private JButton Addyes,Addno;
	  private Connection con=null;
	  private Statement sql=null;
	  private ResultSet rs=null;
	  private String SQLname,SQLsex,SQLpost,SQLjop;
	  private int SQLnumber,SQLage,SQLwage;
	      //chaxun xx;
	
	revise(Frame fck,String ckm,Boolean msck,chaxun xx,int j){
					  
			  super(fck, ckm,msck); 
			  
			  if(j==-1){
				  JOptionPane.showMessageDialog(null,"请选中要修改的行");
				 return;
				  
			  }
			  
			  
			  
			  
		    	//xx=new chaxun();	      			  
			    setNumber=new JTextField(5);
			    setNumber.setText((String)xx.getValueAt(j,0).toString());			 
			    setNumber.setEditable(false);//设置为不能修改,防止出错
		        setName=new JTextField(5);
		        setName.setText((String)xx.getValueAt(j,1));
		        setAge=new JTextField(5);
		        setAge.setText((String)xx.getValueAt(j,3).toString());
		        setSex=new JTextField(5);
		        setSex.setText((String)xx.getValueAt(j,2));
		        setPost=new JTextField(5);
		        setPost.setText((String)xx.getValueAt(j,4));
		        setWage=new JTextField(5);
		        setWage.setText((String)xx.getValueAt(j,5).toString());
		        setJop=new JTextField(5);
		        setJop.setText((String)xx.getValueAt(j,6));
		        
		        name=new JLabel("姓名");
		        number=new JLabel("学号");
		        age=new JLabel("年龄");
		        sex=new JLabel("性别");
		        post=new JLabel("职位");
		        wage=new JLabel("工资");
		        jop=new JLabel("地点");
		        		        
		        Addyes=new JButton("确定");
		        Addno=new JButton("取消");
		        this.add(number);this.add(setNumber);
		        this.add(name);this.add(setName);
		        this.add(sex);this.add(setSex);
		        this.add(age);this.add(setAge);
		        this.add(post);this.add(setPost);
		        this.add(wage);this.add(setWage);
		        this.add(jop);this.add(setJop);
		        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(8,2));
	              this.setVisible(true);
		        this.setResizable(false);
		  }

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==Addyes){
			
			
			 try{
		        	// 获得连接
		        	Class.forName("com.mysql.jdbc.Driver");
		          con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nature","root","root");
		         // 建立查询条件
		       
		       
		         String recode = " update nature set name=?,sex=?,age=?,post=?,wage=?,jop=? where number=?"; 
		         PreparedStatement input=con.prepareStatement(recode);
		           SQLnumber=Integer.parseInt(setNumber.getText());
		         
			        SQLage= Integer.parseInt(setAge.getText());
			    
			        SQLwage= Integer.parseInt(setWage.getText());
			     
		       //  input.setInt(1, SQLnumber); 
		         input.setString(1, setName.getText()); 
		         input.setString(2,setSex.getText()); 
		         input.setInt(3,SQLage); 
		         input.setString(4,setPost.getText()); 
		         input.setInt(5,SQLwage); 
		         input.setString(6,setJop.getText()); 
		         input.setInt(7, 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();
		} 
		}
		
	

	}
	

[源代码打包下载]




网友评论    (发表评论)

共4 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...