java访问数据库sql server增删改查第二篇

1、Xsxxgl.java 

import java.awt.*;

import java.awt.event.*;
import java.sql.*;
import java.util.*;


import javax.swing.*;


public class Xsxxgl extends JFrame implements ActionListener{
JPanel mb1;
JButton an1,an2,an3,an4;
JTable bg1;
JScrollPane gd1;
Vector ziduan,jilu;
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;

public Xsxxgl(){
an1=new JButton("查询");
an1.addActionListener(this);
an1.setActionCommand("chaxun");
an2=new JButton("增加");
an2.addActionListener(this);
an2.setActionCommand("zhengjia");
an3=new JButton("修改");
an3.addActionListener(this);
an3.setActionCommand("xiugai");
an4=new JButton("删除");
an4.addActionListener(this);
an4.setActionCommand("shanchu");
mb1=new JPanel();
mb1.setLayout(new GridLayout(4,1));
mb1.add(an1);
mb1.add(an2);
mb1.add(an3);
mb1.add(an4);

ziduan=new Vector();
ziduan.add("学号");
ziduan.add("姓名");
ziduan.add("性别");
ziduan.add("年龄");

jilu=new Vector();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 ct=DriverManager.getConnection("jdbc:odbc:sql server");
 ps=ct.prepareStatement("select * from Table_3");
 rs=ps.executeQuery();
 
 while(rs.next()){
 Vector hang=new Vector();
 hang.add(rs.getString(1));
 hang.add(rs.getString(2));
 hang.add(rs.getString(3));
 hang.add(rs.getString(4));
 jilu.add(hang);
 }
}catch(Exception e){}
finally{
try{
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
if(ct!=null)
{
ct.close();
}
}catch(Exception e2){

}
}
bg1=new JTable(jilu,ziduan);
gd1=new JScrollPane(bg1);
this.add(gd1);
 
this.add(mb1,BorderLayout.WEST);
 
this.setTitle("学生管理系统");
this.setSize(500,400);
this.setLocation(201,181);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("chaxun")){
chaxun cx=new chaxun(this,"查询学生信息",true);
xsxx xsxx2=new xsxx();
bg1.setModel(xsxx2);
}
else if(e.getActionCommand().equals("zhengjia")){
zhengjia tj=new zhengjia(this,"添加学生信息",true);
xsxx xsxx2=new xsxx();
bg1.setModel(xsxx2);
}
else if(e.getActionCommand().equals("xiugai")){
int ii=this.bg1.getSelectedRow();
if(ii==-1){
JOptionPane.showMessageDialog(this,"请选中要修改的行");
return;
}
xsxx xsxx2=new xsxx();
new xiugai(this,"修改学生信息",true,xsxx2,ii);

bg1.setModel(xsxx2);
}
else if(e.getActionCommand().equals("shanchu")){
xsxx xsxx2=new xsxx();
int ii=this.bg1.getSelectedRow();
if(ii==-1){
JOptionPane.showMessageDialog(this,"请选中要删除的行");
return;
}
String st=(String)xsxx2.getValueAt(ii,0);
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
Statement sm=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ct=DriverManager.getConnection("jdbc:odbc:sql server");
ps=ct.prepareStatement("delete from Table_3 where xuehao=?");
ps.setString(1, st);
ps.executeUpdate();
}catch(Exception e2){}
finally
   {
    try {
    if(rs!=null)
{
rs.close();
}
    if(ps!=null)
{
ps.close();
}
if(ct!=null)
{
ct.close();
}

} catch (Exception e3){}
   }

bg1.setModel(xsxx2);
}
}
public static void main(String args[]){
Xsxxgl xs=new Xsxxgl();
}
} java访问数据库sql server增删改查第二篇_第1张图片

你可能感兴趣的:(java访问数据库sql server增删改查第二篇)