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();}
}
}