Student.java
~~~
package zyw.student;
import zyw.tools.DataBase;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class Student extends JPanel implements ActionListener
{ //創建一個上下方向分割的JSplitPane對象
private JSplitPane jsp=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
private JPanel jpt=new JPanel();
String[]str1=new String[7];//聲明字符串數組
String sql;
DataBase db;
private JLabel[] jlArray=new JLabel[]{//聲明標簽數組
new JLabel(" 學 號"),
new JLabel(" 姓 名"),
new JLabel(" 性 別"),
new JLabel(" 班 級"),
new JLabel(" 院 系"),
new JLabel(" 密 碼"),
new JLabel(" 借書權限")
};
private JTextField[] jtxtArray=new JTextField[]{//聲明文本框數組
new JTextField(),new JTextField(),
new JTextField(),new JTextField(),
new JTextField(),new JTextField()
};
private String[] str={"是","否"};//創建下拉列表框數據模型的字符串數組
private JComboBox jcp=new JComboBox(str);//創建下拉列表框
private JButton[] jbArray={//設置JButton按鈕的文本
new JButton("添加學生信息"),new JButton("刪除學生信息"),
new JButton("修改學生信息"),new JButton("查找學生信息")
};
Vector<String> head = new Vector<String>();
{//創建標題
head.add("學號");head.add("姓名");
head.add("性別");head.add("班級");
head.add("院系");head.add("密碼");
head.add("借書權限");
}
Vector<Vector> data=new Vector<Vector>();//在下部子窗口中設置表格
DefaultTableModel dtm=new DefaultTableModel(data,head);//創建表格模型
JTable jt=new JTable(dtm);//創建JTable對象
JScrollPane jspn=new JScrollPane(jt);//將JTable封裝到滾動窗格
public Student()
{
this.setLayout(new GridLayout(1,1));//聲明本界面為網格布局
jpt.setLayout(null);//設置面板的上部分為空布局管理器
jsp.setDividerLocation(130);//設置jspt中分割條的初始位置
jsp.setDividerSize(4);//設置分隔條的寬度
jsp.setTopComponent(jpt);
jsp.setBottomComponent(jspn);
for(int i=0;i<6;i++){//將文本框添加進上部面板
jpt.add(jtxtArray[i]);
}
for(int i=0;i<7;i++){
jpt.add(jlArray[i]);
if(i<3)
{//對界面上的第一行標簽和文本框大小位置進行設置
jlArray[i].setBounds(20+i*200,10,100,20);
jtxtArray[i].setBounds(120+i*200,10,120,20);
jtxtArray[i].addActionListener(this);
}
else if(i>2&&i<6)
{//對第二行標簽和文本框大小位置進行設置
jlArray[i].setBounds(20+(i-3)*200,50,100,20);
jtxtArray[i].setBounds(120+(i-3)*200,50,120,20);
jtxtArray[i].addActionListener(this);
}
else
{//對最下面的顯示標簽進行設置
jlArray[i].setBounds(620,10,100,20);
}
}
this.add(jsp);
jpt.add(jcp);
jsp.setBottomComponent(jspn);//設置下部子窗格
jcp.setBounds(720,10,100,20);
for(int i=0;i<4;i++)
{//將JButton添加進jpt
jpt.add(jbArray[i]);
jbArray[i].setBounds(170+112*i,90,112,25);
jbArray[i].addActionListener(this); //設置監聽器
}
//設置窗體的大小位置及可見性
this.setBounds(5,5,600,500);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{//為文本框設置焦點
if(e.getSource()==jtxtArray[0])
{
jtxtArray[1].requestFocus();
}
if(e.getSource()==jtxtArray[1])
{
jtxtArray[2].requestFocus();
}
if(e.getSource()==jtxtArray[2])
{
jtxtArray[3].requestFocus();
}
if(e.getSource()==jtxtArray[3])
{
jtxtArray[4].requestFocus();
}
if(e.getSource()==jtxtArray[4])
{
jtxtArray[5].requestFocus();
}
//當點擊"添加學生信息"按鈕是將執行添加功能,將文本框的學生信息添加進STUDENT表中
if(e.getSource()==jbArray[0])
{
this.insertStudent();
}
//當點擊"刪除學生信息"按鈕是將執行刪除功能,將學號為學號框的學生信息從STUDENT表中刪除
if(e.getSource()==jbArray[1])
{
this.deleteStudent();
}
//當點擊"修改學生信息"按鈕是將執行修改功能,將信息為學號框的學生信息在STUDENT表中更新
if(e.getSource()==jbArray[2])
{
this.updateStudent();
}
//當點擊"查找學生信息"按鈕是將執行查找功能,將從STUDENT表中查找學號為學號框的學生信息
if(e.getSource()==jbArray[3])
{
this.searchStudent();
}
}
public void insertStudent(){
for(int i=0;i<6;i++){//聲明文本框輸入信息
str1[i]=jtxtArray[i].getText().trim();
}
if(str1[0].equals("")&&str1[1].equals("")&&str1[2].equals("")
&&str1[3].equals("")&&str1[4].equals("")&&str1[5].equals(""))
{//當各文本框為空提示
JOptionPane.showMessageDialog(this, "學生信息不能為空!!!",
"消息",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(!str1[0].equals("")&&!str1[1].equals("")&&!str1[2].equals("")
&&!str1[3].equals("")&&!str1[4].equals("")&&!str1[5].equals(""))
{//當在文本框輸入信息
str1[6]=jcp.getSelectedItem().toString();
sql="insert into STUDENT(StuNO,StuName,StuSex,Class,Department,"
+"Password,Permitted) values('"+str1[0]+"','"+str1[1]+"','"
+ str1[2] + "',' "+str1[3]+"','"+
str1[4]+"','"+str1[5]+"','"+str1[6]+"')";
db=new DataBase();
Vector<String> v = new Vector<String>();
for(int i=0;i<=6;i++){//將每列添加到臨時數組v
v.add(str1[i]);
if(i<6){jtxtArray[i].setText("");}
}
data.add(v);
dtm.setDataVector(data,head);//更新table并顯示
jt.updateUI();
jt.repaint();
return;
}
}
public void deleteStudent(){
String stuno = jtxtArray[0].getText().trim();
if(stuno.equals("")){//當學號輸入為空提示
JOptionPane.showMessageDialog(this, "學號不能為空!!!",
"消息",JOptionPane.INFORMATION_MESSAGE);
return;
}
sql="select * from STUDENT where StuNO="+Integer.parseInt(stuno);
db=new DataBase();
sql="delete from STUDENT where StuNO="+Integer.parseInt(stuno);
}
public void updateStudent(){
String str[]=new String[7];
int row = jt.getSelectedRow();//聲明所選行號
if(row>=0){//選擇了表格中的某行
for(int i=0;i<7;i++){str[i]=jt.getValueAt(row,i).toString();}
sql="update STUDENT set StuName='"+str[1]+"',StuSex='"+str[2]+"',Class='"
+str[3]+"',Department='"+str[4]+"',Permitted='"+str[5]+"',Password='"+str[6]
+"' where StuNO="+Integer.parseInt(str[0].trim());
db=new DataBase();
JOptionPane.showMessageDialog(this,"修改成功!!",
"消息!!",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(row==-1){//當沒有選擇就點擊'修改信息'按鈕 ,提示
JOptionPane.showMessageDialog(this,"請點擊'查找'按鈕,在下部更改,再選中所改行,點擊'修改信息'按鈕",
"Warning!!",JOptionPane.INFORMATION_MESSAGE);
return;
}
}
public void searchStudent(){
if(jtxtArray[0].getText().equals("")){//
JOptionPane.showMessageDialog(this,"輸入不能為空,請重新輸入!!!",
"信息",JOptionPane.INFORMATION_MESSAGE);
return;
}
sql="select * from STUDENT where StuNO="+Integer.parseInt(jtxtArray[0].getText().trim());
db=new DataBase();
try{//對結果集進行異常處理
int k=0;
Vector<Vector> vtemp = new Vector<Vector>();
dtm.setDataVector(vtemp,head);
jt.updateUI();
jt.repaint();
}
catch(Exception e){e.printStackTrace();}
}
}
~~~