用java实现数据库的增删改查详解编程语言

练习学的,比较适合新手,数据库可以自己建一个,然后改一下代码就行了。

 

Home.java ~ 6KB         

package xues; 
 
 
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,AddSex,AddAge,AddPost,AddWage,AddJop; 
	 JLabel name,number,age,sex,wage,post,jop; 
	 JButton anShow,anSet,anSelsct,anClear,anAdd,Addyes,Addno; 
	 String SQLname,SQLsex,SQLpost,SQLjop; 
	 String a[]={SQLname,SQLsex,SQLpost,SQLjop}; 
	 int SQLnumber,SQLage,SQLwage; 
	 int b[]={SQLnumber,SQLage,SQLwage}; 
     
	 
	public Home(){	 
		super("学生管理系统"); 
		this.setSize(400,500); 
		this.setLayout(null); 
		this.setLocation(400,100); 
        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(); 
        AddAge=new JTextField(); 
        AddSex=new JTextField(); 
        AddPost=new JTextField(); 
        AddWage=new JTextField(); 
        AddJop=new JTextField(); 
         
        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("取消"); 
         
         
         
        //添加窗口的创建 
          Add=new  JFrame();       		         
        Add.setTitle("添加"); 
        Add.setSize(350,400); 
        Add.setLocation(100, 260); 
        Add.setLayout(new GridLayout(8,2)); 
        Add.add(number);Add.add(AddNumber); 
        Add.add(name);Add.add(AddName); 
        Add.add(sex);Add.add(AddSex); 
        Add.add(age);Add.add(AddAge); 
        Add.add(post);Add.add(AddPost); 
        Add.add(wage);Add.add(AddWage); 
        Add.add(jop);Add.add(AddJop); 
        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=people","sa","123456"); 
         // 建立查询条件 
         
         
          
 		String sql="select * from nature"; 
 
          
       
         PreparedStatement pstm = conn.prepareStatement(sql); 
         // 执行查询 
         ResultSet rs = pstm.executeQuery(); 
         // 计算有多少条记录 
         int count = 0; 
         while(rs.next()){ 
         count++; 
         } 
         rs = pstm.executeQuery(); 
         // 将查询获得的记录数据,转换成适合生成JTable的数据形式 
         Object[][] info = new Object[count][7]; 
         count = 0; 
         while(rs.next()){ 
        	 
         info[count][0] = Integer.valueOf( rs.getInt("number")); 
         info[count][1] = rs.getString("name"); 
         info[count][2] = rs.getString("sex"); 
         info[count][3] = Integer.valueOf( rs.getInt("age") ); 
         info[count][4] = rs.getString("post"); 
         info[count][5] = Integer.valueOf( rs.getInt("wage")); 
         info[count][6] = rs.getString("jop"); 
         
         count++; 
          
        
         } 
         // 定义表头 
         String[] title = {"学号","姓名","性别","年龄","职位","工资","地点"}; 
         // 创建JTable 
         this.tabDemo = new JTable(info,title); 
         // 显示表头 
         this.jth = this.tabDemo.getTableHeader(); 
         // 将JTable加入到带滚动条的面板中 
         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 nature 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); 
		} 
		 
	} 
	 
	 
	} 
	 
 
 
 
 

main.java ~ 600B         

package xues; 
 
import javax.swing.UIManager; 
 
import com.l2fprod.gui.plaf.skin.Skin; 
import com.l2fprod.gui.plaf.skin.SkinLookAndFeel; 
 
public class main  { 
	public static void main(String args[]){ 
		 try { 
		        Skin skin = SkinLookAndFeel.loadThemePack("F://н¨Îļþ¼Ð (2)/Î÷⊃1;Ï/Ƥ·ô"); 
 
		        SkinLookAndFeel.setSkin(skin); 
		        UIManager.setLookAndFeel("com.l2fprod.gui.plaf.skin.SkinLookAndFeel"); 
		    } 
		    catch (Exception ex)  { 
		        System.out.println("Ì滻Ƥ·ô´íÎó"); 
		    } 
 
		 
		 
	 Home aa=new Home(); 
	 
 
	 //System.out.println("1"); 
	} 
} 

Add.java ~ 4KB         

package xues; 
 
import java.awt.Frame; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.sql.*; 
 
import javax.swing.*; 
 
class Add extends JDialog implements ActionListener{ 
	 JTextField AddName,AddNumber,AddSex,AddAge,AddPost,AddWage,AddJop; 
	 JLabel name,number,age,sex,wage,post,jop; 
	  
	 JButton Addyes,Addno; 
	 Connection con=null; 
	 Statement sql=null; 
	   ResultSet rs=null; 
	   String SQLname,SQLsex,SQLpost,SQLjop; 
	   int SQLnumber,SQLage,SQLwage; 
	  Add(Frame fck,String ckm,Boolean msck){ 
		   
		  super(fck, ckm,msck);       		         
	     
	        
		   
		  AddName=new JTextField(); 
	        AddNumber=new JTextField(); 
	        AddAge=new JTextField(); 
	        AddSex=new JTextField(); 
	        AddPost=new JTextField(); 
	        AddWage=new JTextField(); 
	        AddJop=new JTextField(); 
	         
	        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(AddNumber); 
	        this.add(name);this.add(AddName); 
	        this.add(sex);this.add(AddSex); 
	        this.add(age);this.add(AddAge); 
	        this.add(post);this.add(AddPost); 
	        this.add(wage);this.add(AddWage); 
	        this.add(jop);this.add(AddJop); 
	        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.microsoft.sqlserver.jdbc.SQLServerDriver"); 
		          con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=people","sa","123456"); 
		         // 建立查询条件 
		        
		        
		         String recode = " insert into nature values(?,?,?,?,?,?,?)";  
		         PreparedStatement input=con.prepareStatement(recode); 
		            SQLnumber=Integer.parseInt(AddNumber.getText()); 
		          
			        SQLage= Integer.parseInt(AddAge.getText()); 
			     
			        SQLwage= Integer.parseInt(AddWage.getText()); 
			      
		         input.setInt(1, SQLnumber);  
		         input.setString(2, AddName.getText());  
		         input.setString(3,AddSex.getText());  
		         input.setInt(4,SQLage);  
		         input.setString(5,AddPost.getText());  
		         input.setInt(6,SQLwage);  
		         input.setString(7,AddJop.getText());  
		         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){ 
		        	 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(); 
		}  
		} 
	} 
 
 
 

chaxun.java ~ 3KB         

package xues; 
 
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 java.util.Vector; 
 
import javax.swing.JOptionPane; 
import javax.swing.table.AbstractTableModel; 
 
 class chaxun extends AbstractTableModel { 
      Vector ziduan,jilu; 
      Connection con=null; 
      PreparedStatement sql=null; 
 	   ResultSet rs=null; 
	@Override 
	public int getColumnCount() { 
		// TODO Auto-generated method stub 
		return this.ziduan.size(); 
	} 
 
	@Override 
	public int getRowCount() { 
		// TODO Auto-generated method stub 
		return this.jilu.size(); 
	} 
 
	@Override 
	public Object getValueAt(int hang, int lie) { 
		// TODO Auto-generated method stub 
		return ((Vector) this.jilu.get(hang)).get(lie); 
	} 
	public chaxun(){ 
		String nul="select * from nature"; 
		this.sqlly(nul); 
		 
	} 
	public chaxun(String input){ 
		this.sqlly(input); 
	} 
	public String getColumnName(int e){ 
		return (String)this.ziduan.get(e); 
	} 
	public void sqlly(String ss){ 
		ziduan=new Vector(); 
		  
		  ziduan.add("学号"); 
		  ziduan.add("姓名"); 
		  ziduan.add("性别"); 
		  ziduan.add("年龄"); 
		  ziduan.add("职位"); 
		  ziduan.add("工资"); 
		  ziduan.add("地点"); 
		  jilu=new Vector(); 
		  try{ 
			  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
		          con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=people","sa","123456"); 
		         // 建立查询条件    
		       //   sql = "select * from nature";  
		         //sql=con.prepareStatement( " insert into nature values(?,?,?,?,?,?,?)");  
		        sql=con.prepareStatement(ss); 
		      //  sql.executeUpdate(); 
		          rs=sql.executeQuery(); 
		          while(rs.next()){ 
		          Vector hang=new Vector();  
		         hang.add(rs.getInt(1)); 
		         hang.add(rs.getString(2)); 
		         hang.add(rs.getString(3)); 
		         hang.add(rs.getInt(4)); 
		         hang.add(rs.getString(5)); 
		         hang.add(rs.getInt(6)); 
		         hang.add(rs.getString(7)); 
		        
		         jilu.add(hang); 
		           
		           
		          }    
		           
		           
		  } 
		   
		  catch(NumberFormatException nu){ 
	        	 JOptionPane.showMessageDialog(null,"你还没有输入哦","错误",JOptionPane.ERROR_MESSAGE); 
	         } catch (ClassNotFoundException e) { 
			// TODO Auto-generated catch block 
	        	 JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE); 
		} catch (SQLException e) { 
				// TODO Auto-generated catch block 
			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 e){} 
		    } 
		   
	} 
     
} 

clear.java ~ 2KB         

package xues; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.Statement; 
 
import javax.swing.JOptionPane; 
 
 class clear { 
	 
	   chaxun xx; 
	    
	   public clear(int i){ 
		   xx=new chaxun(); 
		    
		  
		   if(i==-1){ 
			   JOptionPane.showMessageDialog(null,"请选中要删除的行"); 
			   return; 
		   } 
		    
		    
			   
			   Connection con=null; 
			     PreparedStatement sql=null; 
				   ResultSet rs=null; 
				   Statement sm=null; 
			    
			   String sr=(String) xx.getValueAt(i,1); 
			   try{ 
				    
					  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
			          con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=people","sa","123456"); 
			         // 建立查询条件    
			           
			        
			          String  cl = "delete  from nature where name=?";  
			        
			        
			        sql=con.prepareStatement(cl); 
			       // int ii=Integer.valueOf(sr).intValue(); 
			        sql.setString(1,sr); 
			        sql.executeUpdate(); 
			        
			         
				    
				    
				    
				    
				    
				    
				    
				    
				    
			   } 
			   catch(Exception e){ 
				   e.printStackTrace(); 
				    
			   } 
 
			    finally 
			    { 
			    	try { 
			    		if(rs!=null) 
						{ 
							rs.close(); 
						} 
			    		if(sql!=null) 
						{ 
							sql.close(); 
						} 
						if(con!=null) 
						{ 
							con.close(); 
						} 
						 
					} catch (Exception e3){}		 
			    }  
			    
			    
			    
		   } 
		    
	   } 
 

revise.java ~ 5KB         

package xues; 
 
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.microsoft.sqlserver.jdbc.SQLServerDriver"); 
		          con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=people","sa","123456"); 
		         // 建立查询条件 
		        
		        
		         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(); 
		}  
		} 
		 
	 
 
	} 
	 
 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10426.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论