Java+SQLServer2012學生資訊管理系統
阿新 • • 發佈:2019-01-25
課程設計作業
在網上程式碼的基礎上做了一些改動(增加為兩個選單:分別為學生資訊管理和學生成績管理)
java程式連線操作SQLServer詳解:點選開啟連結
下載jdbc驅動:點選開啟連結
jdk環境變數配置:點選開啟連結
軟體的安裝就自己百度教程就好。
流程圖:
首先在SQLServer中建立三個表:
1.login表:學生登入賬號和密碼管理
2.stu表:學生資訊表
3.grade表:學生成績表
登入介面:
package StudentManagementSystem; import java.sql.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.Color; public class LLogin { public static void main(String[] args) { new loginFrame(); } } class loginFrame extends JFrame implements ActionListener{ Box box1,box2,baseBox; JLabel userName,userPwd,tubiao; JTextField nameField; JPasswordField pwdField; JButton button; JTabbedPane choose; JPanel panel1,panel2; loginFrame(){ setBackground(Color.orange); tubiao=new JLabel(new ImageIcon("圖片路徑")); //圖片在原有基礎上要調整大小 add(tubiao,BorderLayout.NORTH); userName=new JLabel("賬號",JLabel.CENTER); userPwd=new JLabel("密碼",JLabel.CENTER); nameField=new JTextField(8); pwdField=new JPasswordField(8); panel1=new JPanel(); panel2=new JPanel(); choose=new JTabbedPane(); choose.add("登入介面",panel1); panel1.setLayout(new GridLayout(2,2)); panel1.add(userName);panel1.add(nameField); panel1.add(userPwd);panel1.add(pwdField); add(choose,BorderLayout.CENTER); button=new JButton("登陸"); add(button,BorderLayout.SOUTH); button.addActionListener(this); //小圖示 ImageIcon tubiao=new ImageIcon("圖片路徑"); setIconImage(tubiao.getImage()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setBounds(400,150,550,400); setTitle("陳可的學生資訊管理系統"); validate(); } public void actionPerformed(ActionEvent e){ String name,pwd; name=nameField.getText(); pwd=pwdField.getText(); try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //載入資料庫驅動 } catch(ClassNotFoundException ex){ System.out.println(ex); } try{ Connection con; Statement sql; ResultSet rs; String url,userName,userPwd; // 連線資料庫的語句 url="jdbc:sqlserver://localhost:1433;DatabaseName=Student"; userName="sa"; userPwd="自己資料庫的密碼"; con=DriverManager.getConnection(url,userName,userPwd); sql=con.createStatement(); rs=sql.executeQuery("select * from login where name ='"+name+"' and pwd='"+pwd+"'");//對應自己資料庫建的表填寫 int q=0; while(rs.next()){ q++; } if(q>0){ JOptionPane.showMessageDialog(this, "登陸成功!","訊息對話方塊",JOptionPane.WARNING_MESSAGE); this.dispose(); new CommFrame(); } else JOptionPane.showMessageDialog(this, "賬號或者密碼錯誤!","訊息對話方塊",JOptionPane.WARNING_MESSAGE); } catch(SQLException exp){ System.out.println(exp); } } }
效果:
登陸成功後 點選確定 登陸介面會消失 出現學生資訊管理系統管理介面 。
一個集合所有視窗的卡片式布:所呼叫的函式和介面的基本呈現。
你可以根據要求自己新增工具選單欄。
package StudentManagementSystem; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CommFrame extends JFrame implements ActionListener { JMenuBar bar; JMenu menu,menu1; JMenuItem scanItem,deleteItem,updateItem,insertItem,searchItem; JMenuItem scanItem1,deleteItem1,updateItem1,insertItem1,searchItem1; Scan_stu scan; //檢視所有學生資訊 Delete_stu delete;//刪除學生資訊 Update_stu update;//更新學生資訊 Insert_stu insert;//插入學生資訊 Search_stu search;//查詢學生資訊 Scan_grade scan_grade; //檢視所有學生成績 Delete_grade delete_grade;//刪除學生成績 Update_grade update_grade;//更新學生成績 Insert_grade insert_grade;//插入學生成績 Search_grade search_grade;//查詢學生成績 CardLayout card=null; JPanel pCenter; CommFrame(){ setLayout(new FlowLayout()); scanItem=new JMenuItem("瀏覽"); deleteItem=new JMenuItem("刪除"); updateItem=new JMenuItem("修改"); insertItem=new JMenuItem("新增"); searchItem=new JMenuItem("查詢"); scanItem1=new JMenuItem("瀏覽"); deleteItem1=new JMenuItem("刪除"); updateItem1=new JMenuItem("修改"); insertItem1=new JMenuItem("新增"); searchItem1=new JMenuItem("查詢"); bar=new JMenuBar(); menu=new JMenu("資訊管理選單"); menu1=new JMenu("成績管理選單"); menu.add(scanItem); menu.add(deleteItem); menu.add(updateItem); menu.add(insertItem); menu.add(searchItem); menu1.add(scanItem1); menu1.add(deleteItem1); menu1.add(updateItem1); menu1.add(insertItem1); menu1.add(searchItem1); bar.add(menu); bar.add(menu1); setJMenuBar(bar); scanItem.addActionListener(this); deleteItem.addActionListener(this); updateItem.addActionListener(this); insertItem.addActionListener(this); searchItem.addActionListener(this); scanItem1.addActionListener(this); deleteItem1.addActionListener(this); updateItem1.addActionListener(this); insertItem1.addActionListener(this); searchItem1.addActionListener(this); scan=new Scan_stu(); update=new Update_stu(); delete=new Delete_stu(); insert=new Insert_stu(); search=new Search_stu(); card=new CardLayout(); scan_grade=new Scan_grade(); update_grade=new Update_grade(); delete_grade=new Delete_grade(); insert_grade=new Insert_grade(); search_grade=new Search_grade(); card=new CardLayout(); pCenter=new JPanel(); pCenter.setLayout(card); pCenter.add("scanItem", scan); pCenter.add("deleteItem",delete); pCenter.add("updateItem",update); pCenter.add("insertItem",insert); pCenter.add("searchItem",search); pCenter.add("scanItem1", scan_grade); pCenter.add("deleteItem1",delete_grade); pCenter.add("updateItem1",update_grade); pCenter.add("insertItem1",insert_grade); pCenter.add("searchItem1",search_grade); add(pCenter,BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setBounds(400,150,550,400); setTitle("學生資訊管理系統"); validate(); } public void actionPerformed(ActionEvent e){ if(e.getSource()==scanItem) card.show(pCenter, "scanItem"); else if(e.getSource()==deleteItem) card.show(pCenter, "deleteItem"); else if(e.getSource()==updateItem) card.show(pCenter, "updateItem"); else if(e.getSource()==insertItem) card.show(pCenter, "insertItem"); else if(e.getSource()==searchItem) card.show(pCenter, "searchItem"); if(e.getSource()==scanItem1) card.show(pCenter, "scanItem1"); else if(e.getSource()==deleteItem1) card.show(pCenter, "deleteItem1"); else if(e.getSource()==updateItem1) card.show(pCenter, "updateItem1"); else if(e.getSource()==insertItem1) card.show(pCenter, "insertItem1"); else if(e.getSource()==searchItem1) card.show(pCenter, "searchItem1"); } }
效果:
瀏覽學所有學生資訊:Scan_stu
package StudentManagementSystem; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class Scan_stu extends JPanel implements ActionListener{ DefaultTableModel update_table; JTable table; Query query; JButton button; Object a[][]; String b[]; Scan_stu(){ setLayout(new FlowLayout()); setBackground(Color.orange); query=new Query(); query.setTableName("stu"); a=query.getRecord(); b=query.getField(); update_table=new DefaultTableModel(a, b); table=new JTable(update_table); button=new JButton("更新"); button.addActionListener(this); JScrollPane scrollPane = new JScrollPane(table); scrollPane.setBounds(0,0,550,380); table.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()*2));//使表格出現滑動條 add(scrollPane); add(button); } public void actionPerformed(ActionEvent e){ a=null;b=null; query=new Query(); query.setTableName("stu"); a=query.getRecord(); b=query.getField(); update_table.setDataVector(a, b); } }
瀏覽所有學生成績:Scan_grade(其實和上面刪除學生資訊一樣只不過把類名和建構函式名改一下,後面的也一樣)
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Scan_grade extends JPanel implements ActionListener{
DefaultTableModel update_table;
JTable table;
Query query;
JButton button;
Object a[][];
String b[];
Scan_grade(){
setLayout(new FlowLayout());
setBackground(Color.orange);
query=new Query();
query.setTableName("grade");
a=query.getRecord();
b=query.getField();
update_table=new DefaultTableModel(a, b);
table=new JTable(update_table);
button=new JButton("更新");
button.addActionListener(this);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(0,0,550,380);
table.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()*2));//使表格出現滑動條
add(scrollPane);
add(button);
}
public void actionPerformed(ActionEvent e){
a=null;b=null;
query=new Query();
query.setTableName("grade");
a=query.getRecord();
b=query.getField();
update_table.setDataVector(a, b);
}
}
Scan_stu和Scan_grade呼叫的Query查詢函式
package StudentManagementSystem;
import java.sql.*;
public class Query {
Object a[][]=null;
String b[]=null;
String tableName="";
int 欄位個數;
public Query(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException e){
System.out.println(e);
}
}
public Object[][] getRecord(){//求表格的內容
a=null;
b=null;
Connection con;
Statement sql;
ResultSet rs;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="chenke010919chan";
con=DriverManager.getConnection(url,userName,userPwd);
int 欄位個數=getZiDuan();
int n=getAmount();
a=new Object[n][欄位個數];
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select * from "+tableName);
int m=0;
while(rs.next()){
for(int k=1;k<=欄位個數;k++){
a[m][k-1]=rs.getString(k);
}
System.out.println();
m++;
}
con.close();
}
catch(SQLException e){
System.out.println("請輸入正確的表名"+e);
}
return a;
}
public int getAmount(){//求表內容有多少行
Connection con;
Statement sql;
ResultSet rs;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己資料庫密碼";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select * from "+tableName);
rs.last();
int rows=rs.getRow();
return rows;
}
catch(SQLException exp){
System.out.println(""+exp);
return 0;
}
}
public String[] getField(){//求欄位名稱
Connection con;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己資料庫密碼";
con=DriverManager.getConnection(url,userName,userPwd);
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null, null, tableName, null);
int 欄位個數=getZiDuan();
b=new String[欄位個數];
int k=0;
while(rs1.next()){
b[k]=rs1.getString(4);
k++;
}
con.close();
}
catch(SQLException e){
System.out.println(e);
}
return b;
}
public void setTableName(String s){//初試表名
tableName=s.trim();
}
public int getZiDuan(){//求欄位個數
Connection con;
PreparedStatement sql;
ResultSet rs;
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己資料庫密碼";
con=DriverManager.getConnection(url,userName,userPwd);
DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null, null, tableName, null);
欄位個數=0;
while(rs1.next())
欄位個數++;
}
catch(SQLException e){
System.out.println(e);
}
return 欄位個數;
}
}
刪除學生資訊:Delete_stu
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Delete_stu extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Delete delete;
String tableName;
Delete_stu(){
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("學號")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel(" 單擊刪除"));
button=new JButton("刪除");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
if(field[mark].getText().toString().equals("")==true)
JOptionPane.showMessageDialog(this, "帶*號為必填內容","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
else
{
Delete delete=new Delete();
delete.setTableName("stu");
delete.setField(field);
delete.setA(a);
delete.Execute_Delete(mark);
}
}
}
刪除學生成績:Delete_grade
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Delete_grade extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Delete delete;
String tableName;
Delete_grade(){
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("學號")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel(" 單擊刪除"));
button=new JButton("刪除");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
if(field[mark].getText().toString().equals("")==true)
JOptionPane.showMessageDialog(this, "帶*號為必填內容","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
else
{
Delete delete=new Delete();
delete.setTableName("grade");
delete.setField(field);
delete.setA(a);
delete.Execute_Delete(mark);
}
}
}
呼叫的刪除函式:Delete
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Delete extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Delete(){
}
public void Execute_Delete(int n){
String SQL="";
Connection con;
Statement sql;
SQL="delete from "+tableName+" where "+a[n]+" ='"+field[n].getText().toString()+"'";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException exp){
System.out.println(exp);
}
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己資料庫密碼";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
sql.executeUpdate(SQL);
con.close();
JOptionPane.showMessageDialog(this, "刪除成功","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
for(int i=0;i<field.length;i++)
field[i].setText(null);;
}
catch(SQLException ex){
System.out.println(ex);
}
}
}
修改學生資訊函式:Update_stu
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Update_stu extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Update update;
String tableName;
Update_stu(){
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("學號")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel(" 單擊修改"));
button=new JButton("修改");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
int i;
for(i=0;i<n;i++){
if(a[i].equals("學號")==true&&field[i].getText().toString().equals("")==true){
mark=i;
JOptionPane.showMessageDialog(this, "帶*必須填寫!!!","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
break;
}
}
if(i==n){
int choose=JOptionPane.showConfirmDialog(this, "請確保你的學號是正確的,否則會更新失敗!!!如果學號錯誤"
+ "請先刪除再新增","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
if(choose==JOptionPane.YES_OPTION){
Update update=new Update();
update.setTableName("stu");
update.setField(field);
update.setA(a);
update.Execute_Update(mark);
JOptionPane.showMessageDialog(this, "更新成功!!!","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
}
}
}
}
修改學生成績:Update_grade
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Update_grade extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Update update;
String tableName;
Update_grade(){
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("學號")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel("單擊修改"));
button=new JButton("修改");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
int i;
for(i=0;i<n;i++){
if(a[i].equals("學號")==true&&field[i].getText().toString().equals("")==true){
mark=i;
JOptionPane.showMessageDialog(this, "帶*必須填寫!!!","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
break;
}
}
if(i==n){
int choose=JOptionPane.showConfirmDialog(this, "請確保你的學號是正確的,否則會更新失敗!!!如果學號錯誤"
+ "請先刪除再新增","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
if(choose==JOptionPane.YES_OPTION){
Update update=new Update();
update.setTableName("grade");
update.setField(field);
update.setA(a);
update.Execute_Update(mark);
JOptionPane.showMessageDialog(this, "更新成功!!!","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
}
}
}
}
呼叫的修改函式:Update
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Update extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Update(){
}
public void Execute_Update(int mark){
String SQL[]=new String [a.length];
Connection con;
Statement sql;
for(int i=0;i<a.length;i++)
{
if(i!=mark)
SQL[i]="update "+tableName+" set "+a[i]+" ='"+field[i].getText().toString()+"' where "+a[mark]+" ='"+field[mark].getText().toString()+"'";
}
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException exp){
System.out.println(exp);
}
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己資料庫密碼";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
for(int i=0;i<field.length;i++)
if(i!=mark&&field[i].getText().toString().equals("")==false)
sql.executeUpdate(SQL[i]);
con.close();
for(int i=0;i<field.length;i++)
field[i].setText(null);
}
catch(SQLException ex){
System.out.println(ex);
}
}
}
新增學生資訊函式:Insert_stu
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Insert_stu extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Insert insert;
String tableName;
Insert_stu(){
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("學號")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel(" 單擊新增"));
button=new JButton("新增");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
int i;
for(i=0;i<n;i++){
if(field[i].getText().toString().equals("")==true){
JOptionPane.showMessageDialog(this, "必須全部填寫","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
break;
}
if(a[i].equals("學號")==true){
mark=i;
}
}
if(i==n){
Insert insert=new Insert();
insert.setTableName("stu");
insert.setField(field);
insert.setA(a);
insert.Execute_Insert(mark);
}
}
}
新增學生成績函式:Insert_grade
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Insert_grade extends JPanel implements ActionListener{
Box box1,box2,baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark;
Insert insert1;
String tableName;
Insert_grade(){
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
box1=Box.createVerticalBox();
box2=Box.createVerticalBox();
n=a.length;
field =new JTextField[n];
for(int i=0;i<n;i++){
field[i]=new JTextField(10);
if(a[i].equals("學號")==true)
box1.add(new JLabel("* "+a[i]));
else
box1.add(new JLabel(" "+a[i]));
box1.add(Box.createVerticalStrut(8));
box2.add(field[i]);
box2.add(Box.createVerticalStrut(8));
}
box1.add(new JLabel(" 單擊新增"));
button=new JButton("新增");
button.addActionListener(this);
box2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(box1);
baseBox.add(Box.createHorizontalStrut(8));
baseBox.add(box2);
add(baseBox);
}
public void actionPerformed(ActionEvent e){
int i;
for(i=0;i<n;i++){
if(field[i].getText().toString().equals("")==true){
JOptionPane.showMessageDialog(this, "必須全部填寫","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
break;
}
if(a[i].equals("學號")==true){
mark=i;
}
}
if(i==n){
Insert insert=new Insert();
insert.setTableName("grade");
insert.setField(field);
insert.setA(a);
insert.Execute_Insert(mark);
}
}
}
Insert_stu和Insert_grade呼叫的新增函式:Insert
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Insert extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
Update update;
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Insert(){
}
public void Execute_Insert(int mark){
String SQL;
Connection con;
Statement sql;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException exp){
System.out.println(exp);
}
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己資料庫密碼";
SQL="insert "+tableName+"("+a[mark]+") values ('"+field[mark].getText().toString()+"')";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
sql.execute(SQL);
con.close();
update =new Update();
update.setTableName(tableName);
update.setField(field);
update.setA(a);
update.Execute_Update(mark);
JOptionPane.showMessageDialog(this, "新增成功!!!","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
for(int i=0;i<field.length;i++)
field[i].setText(null);;
}
catch(SQLException ex){
System.out.println(ex);
}
}
}
查詢學生資訊函式:Search_stu
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Search_stu extends JPanel implements ActionListener{
Box box[],baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark,m;
Search search;
String tableName;
DefaultTableModel search_table;
Object object[][];
String b[];
Search_stu(){
setLayout(new FlowLayout());
query=new Query();
setBackground(Color.orange);
query.setTableName("stu");
a=query.getField();
n=a.length;
box=new Box [n+1];
field =new JTextField[n];
for(int i=0;i<n;i++){
box[i]=Box.createHorizontalBox();
field[i]=new JTextField(10);
if(a[i].equals("學號")==true){
box[i].add(new JLabel(" "+a[i]));
box[i].add(Box.createHorizontalStrut(8));
box[i].add(field[i]);
}
else{
box[i].add(new JLabel(" "+a[i]));
box[i].add(Box.createHorizontalStrut(8));
box[i].add(field[i]);
}
}
object =new Object[0][0];
search_table=new DefaultTableModel(object, a);
JTable table=new JTable(search_table);
button=new JButton("查詢");
button.addActionListener(this);
box[n-1].add(new JLabel(" 單擊查詢"));
box[n-1].add(Box.createHorizontalStrut(8));
box[n-1].add(button);
baseBox=Box.createVerticalBox();
for(int i=0;i<n;i++){
baseBox.add(box[i]);
baseBox.add(Box.createVerticalStrut(8));
}
JScrollPane scrollPane=new JScrollPane(table);
scrollPane.setBounds(0,0,550,380);
table.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()*2));
baseBox.add(scrollPane);
add(baseBox);
table.revalidate();
}
public void actionPerformed(ActionEvent e){
int i,sum=0;
for(i=0;i<n;i++){
if(field[i].getText().toString().equals("")==true)
sum++;
}
System.out.println(sum);
if(sum==n)
JOptionPane.showMessageDialog(this, "你未輸入任何內容,請重新輸入!","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
else{
Search search=new Search();
search.setTableName("stu");
search.setField(field);
search.setA(a);
search.Execute_Search();
object=null;b=null;
query=new Query();
query.setTableName("stu");
object=search.getRecord();
search_table.setDataVector(object, a);
}
}
}
查詢學生成績函式:Search_grade
package StudentManagementSystem;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class Search_grade extends JPanel implements ActionListener{
Box box[],baseBox;
Query query;
JButton button;
JTextField field[]=null;
String a[]=null;
int n,mark,m;
Search search;
String tableName;
DefaultTableModel search_table;
Object object[][];
String b[];
Search_grade(){
setLayout(new FlowLayout());
query=new Query();
setBackground(Color.orange);
query.setTableName("grade");
a=query.getField();
n=a.length;
box=new Box [n+1];
field =new JTextField[n];
for(int i=0;i<n;i++){
box[i]=Box.createHorizontalBox();
field[i]=new JTextField(10);
if(a[i].equals("學號")==true){
box[i].add(new JLabel(" "+a[i]));
box[i].add(Box.createHorizontalStrut(8));
box[i].add(field[i]);
}
else{
box[i].add(new JLabel(" "+a[i]));
box[i].add(Box.createHorizontalStrut(8));
box[i].add(field[i]);
}
}
object =new Object[0][0];
search_table=new DefaultTableModel(object, a);
JTable table=new JTable(search_table);
button=new JButton("查詢");
button.addActionListener(this);
box[n-1].add(new JLabel(" 單擊查詢"));
box[n-1].add(Box.createHorizontalStrut(8));
box[n-1].add(button);
baseBox=Box.createVerticalBox();
for(int i=0;i<n;i++){
baseBox.add(box[i]);
baseBox.add(Box.createVerticalStrut(8));
}
JScrollPane scrollPane=new JScrollPane(table);
scrollPane.setBounds(0,0,550,380);
table.setPreferredSize(new Dimension(scrollPane.getWidth() - 50, scrollPane.getHeight()*2));
baseBox.add(scrollPane);
add(baseBox);
table.revalidate();
}
public void actionPerformed(ActionEvent e){
int i,sum=0;
for(i=0;i<n;i++){
if(field[i].getText().toString().equals("")==true)
sum++;
}
System.out.println(sum);
if(sum==n)
JOptionPane.showMessageDialog(this, "你未輸入任何內容,請重新輸入!","訊息對話方塊",JOptionPane.WARNING_MESSAGE);
else{
Search search=new Search();
search.setTableName("grade");
search.setField(field);
search.setA(a);
search.Execute_Search();
object=null;b=null;
query=new Query();
query.setTableName("grade");
object=search.getRecord();
search_table.setDataVector(object, a);
}
}
}
Search_stu和Search_grade呼叫的Search函式:
package StudentManagementSystem;
import javax.swing.*;
import java.sql.*;
public class Search extends JFrame{
String tableName;
JTextField field[]=null;
String a[]=null;
Object object[][];
String b[];
public void setTableName(String s){
tableName=s.trim();
}
public void setField(JTextField s[]){
field=s;
}
public void setA(String e[]){
a=e;
}
public Search(){
}
public void Execute_Search(){
String SQL="select * from "+tableName+" where ";
Connection con;
Statement sql;
ResultSet rs;
int sum=0;
for(int i=0;i<a.length;i++)
{
if(field[i].getText().toString().equals("")==false){
if(sum==0)
SQL=SQL+a[i]+" = '"+field[i].getText().toString()+"'";
else
SQL=SQL+" and "+a[i]+" = '"+field[i].getText().toString()+"'";
sum++;
}
}
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException exp){
System.out.println(exp);
}
try{
String url,userName,userPwd;
url="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
userName="sa";
userPwd="自己資料庫密碼";
con=DriverManager.getConnection(url,userName,userPwd);
sql=con.createStatement();
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery(SQL);
rs.last();
object =new Object[rs.getRow()][a.length];
rs.beforeFirst();
int ncase=0;
while(rs.next()){
for(int i=1;i<=a.length;i++){
object[ncase][i-1]=rs.getString(i);
}
ncase++;
}
con.close();
for(int i=0;i<field.length;i++)
field[i].setText(null);
}
catch(SQLException ex){
System.out.println(ex);
}
}
public Object[][] getRecord(){
return object;
}
}