62,614
社区成员
发帖
与我相关
我的任务
分享
package StudentInfo;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.sql.*;
public class StudentInfo {
JFrame f=new JFrame("学生信息");
JPanel p1=new JPanel(new GridLayout(3,1,20,10));
JPanel p11=new JPanel();
JPanel p12=new JPanel();
JPanel p13=new JPanel();
JLabel l1=new JLabel("学号:");
JLabel l2=new JLabel("姓名:");
JLabel l3=new JLabel("专业:");
JTextField tf1=new JTextField(10);
JTextField tf2=new JTextField(10);
JTextField tf3=new JTextField(10);
String[] colname={"学号","姓名","专业"};
DefaultTableModel mod=new DefaultTableModel(null,colname);
JTable tab=new JTable(mod);
JScrollPane sp=new JScrollPane(tab);
JPanel p2=new JPanel(new GridLayout(4,1,20,20));
JButton b1=new JButton("插入");
JButton b2=new JButton("更新");
JButton b3=new JButton("删除");
JButton b4=new JButton("查询");
PreparedStatement ps1=null,ps2=null,ps3=null;
Statement sm=null;
Connection conn=null;
ResultSet rs=null;
StudentInfo(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=StudentInfo","sa","12348765");
sm=conn.createStatement();
ps1=conn.prepareStatement("insert into Student values(?,?,?)");
ps3=conn.prepareStatement("select * from Student where id like ? or name like ? or profession like ?");
ps2=conn.prepareStatement("delete from Student where id=?");
init();
}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public void init(){
f.setSize(500, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocation(600,200);
f.setVisible(true);
f.setResizable(false);
f.add("North",p1);
f.add("Center",sp);
f.add("East",p2);
p11.add(l1); p11.add(tf1);
p12.add(l2); p12.add(tf2);
p13.add(l3); p13.add(tf3);
p1.add(p11); p1.add(p12); p1.add(p13);
p2.add(b1); p2.add(b2); p2.add(b3); p2.add(b4);
Action act=new Action();
b1.addActionListener(act);
b2.addActionListener(act);
b3.addActionListener(act);
b4.addActionListener(act);
mod.addTableModelListener(new TAction());
}
private class TAction implements TableModelListener{
public void tableChanged(TableModelEvent e){
if(e.getType()==0){
/*try{
int n=tab.getEditingRow();
int c=tab.getEditingColumn();
String v=tab.getValueAt(n, 0).toString();
String va=tab.getValueAt(n, c).toString();
String str="update Student set "+tab.getEditingColumn()+"="+va+"where id="+v;
sm.executeUpdate(str);
}catch(SQLException e2){
e2.printStackTrace();
}
}*/
}
}
private class Action implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
if(e.getSource()==b1){
Object[] data={tf1.getText(),tf2.getText(),tf3.getText()};
mod.addRow(data);
ps1.setInt(1, Integer.parseInt(tf1.getText()));
ps1.setString(2, tf2.getText());
ps1.setString(3, tf3.getText());
ps1.executeUpdate();
}else if(e.getSource()==b2){
int i=0;
rs=sm.executeQuery("select * from Student");
while(rs.next()){
if(i>mod.getRowCount()-1)
mod.setRowCount(mod.getRowCount()+1);
mod.setValueAt(rs.getInt("id"),i,0);
mod.setValueAt(rs.getString("name"),i,1);
mod.setValueAt(rs.getString("profession"), i, 2);
i++;
}
}else if(e.getSource()==b3){
ps2.setInt(1,Integer.parseInt(tab.getValueAt(tab.getSelectedRow(), 0).toString()));
ps2.executeUpdate();
mod.removeRow(tab.getSelectedRow());
}else if(e.getSource()==b4){
if(tf1.getText()==""){
ps3.setInt(1, 00000000000000000);
}
else{
ps3.setInt(1, Integer.parseInt(tf1.getText()));
}
if(tf2.getText().matches("\\s+"))
ps3.setString(2, "");
else
ps3.setString(2, tf2.getText());
if(tf3.getText().matches("\\s+"))
ps3.setString(3, "");
else
ps3.setString(3, tf3.getText());
rs=ps3.executeQuery();
mod.setRowCount(0);
int i=0;
while(rs.next()){
if(i>mod.getRowCount()-1)
mod.setRowCount(mod.getRowCount()+1);
mod.setValueAt(rs.getInt("id"),i,0);
mod.setValueAt(rs.getString("name"),i,1);
mod.setValueAt(rs.getString("profession"), i, 2);
i++;
}
}
}catch(SQLException e1){
e1.printStackTrace();
}finally{
try{
if(rs!=null)
rs.close();
rs=null;
}catch(SQLException e1){
e1.printStackTrace();
}
}
}
}
public static void main(String[] args){
new StudentInfo();
}
}