目錄
引言
一、定義學生類
二、編寫學生接口
三、連接配接資料庫,編寫JDBC的工具類
三、編寫接口實作類
四、下載下傳WindowBuilder插件并安裝,建立一個JFram檔案,命名為StudentInformationFrame,在design界面中設定如下布局:
設定好布局之後,點選Source就會自動生成如下代碼:
引言
筆者最近剛學完java程式設計的基礎,是以嘗試做了一個簡單的學生管理系統,主要實作了學生資訊表的增删改查,如有不足之處,敬請各位讀者批評指正。如下是效果圖:
大緻思路如下:1、建立一個學生類 2、編寫學生類的是實作接口 3、編寫接口實作類 4、連接配接資料庫 5、使用SWI制作界面
一、定義學生類
public class Student {
private int Sno;//學号
private String Sname;//姓名
private int age;//年齡
private String gender;//性别
private String department;//系别
private String courseSelected;//選課情況
public Student() {
super();
}
public Student(int sno, String sname, int age, String gender, String department, String courseSelected) {
super();
this.Sno = sno;
this.Sname = sname;
this.age = age;
this.gender = gender;
this.department = department;
this.courseSelected = courseSelected;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSno() {
return Sno;
}
public void setSno(int sno) {
this.Sno = sno;
}
public String getSname() {
return Sname;
}
public void setSname(String sname) {
this.Sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getCourseSelected() {
return courseSelected;
}
public void setCourseSelected(String courseSelected) {
this.courseSelected = courseSelected;
}
@Override
public String toString() {
return "Student [Sno=" + Sno + ", Sname=" + Sname + ", age=" + age + ", gender=" + gender + ", department="
+ department + ", courseSelected=" + courseSelected + "]";
}
}
二、編寫學生接口
public interface IStudentDao {
//查詢所有學生的資訊
public ArrayList<Student> selectStudentAll();
//查詢單個學生的資訊
public Student selectStudent(int sno);
//插入一條學生記錄
public boolean insertStudent(Student s);
//删除一條學生記錄
public boolean deleteStudent(int sno);
//修改某條學生記錄的資訊
public boolean updateStudent(Student s);
}
三、連接配接資料庫,編寫JDBC的工具類
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBUtil {
private static Properties proper=new Properties();
/**
* 靜态代碼塊
*/
static {
try {
//加載properties檔案
proper.load(new FileInputStream("config/db.properties"));
//1 加載外部JDBC驅動程式
Class.forName(proper.getProperty("driverClassName"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 擷取連接配接
* @return
*/
public static Connection getConnection() {
try {
//建立連接配接
Connection con=DriverManager.getConnection(proper.getProperty("url")
,proper.getProperty("username"),proper.getProperty("password"));
return con;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
*擷取Statement對象
*/
public static Statement getStatement(Connection con) {
try {
return con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 擷取PreparedStatement對象
*/
public static PreparedStatement getPreparedStatement(Connection con,String sql) {
try {
return con.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 關閉資源
*/
public static void close(Connection con,Statement stat,ResultSet rs) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stat!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection con,Statement stat) {
if(stat!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection con) {
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection con,PreparedStatement stat,ResultSet rs) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stat!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection con,PreparedStatement stat) {
if(stat!=null) {
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
三、編寫接口實作類
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.yf.bean.Student;
import com.yf.utils.DBUtil;
public class StudentDaoImpl implements IStudentDao{
@Override
public ArrayList<Student> selectStudentAll() {
Connection con = null;
Statement stat = null;
ResultSet rs = null;
try {
//1、擷取連接配接
con = DBUtil.getConnection();
//2、建立Statement對象
stat = DBUtil.getStatement(con);
//3、定義SQL語句
String sql = "select * from Student order by Sno";
//傳回查詢到的結果集
rs = stat.executeQuery(sql);
ArrayList<Student> stuList = new ArrayList<Student>();
while(rs.next()) {
Student stu = new Student();
stu.setSno(rs.getInt("Sno"));
stu.setSname(rs.getString("Sname"));
stu.setAge(rs.getInt("age"));
stu.setGender(rs.getString("gender"));
stu.setDepartment(rs.getString("department"));
stu.setCourseSelected(rs.getString("courseSelected"));
stuList.add(stu);
}
return stuList;
} catch (SQLException e) {
e.printStackTrace();
} finally {
//關閉資源
DBUtil.close(con, stat, rs);
}
return null;
}
@Override
public Student selectStudent(int sno) {
Connection con = null;
PreparedStatement stat = null;
ResultSet rs = null;
try {
//建立連接配接
con = DBUtil.getConnection();
//定義SQL語句
String sql = "select * from Student where sno = ?";
//建立預編譯對象
stat = con.prepareStatement(sql);
stat.setInt(1,sno);
rs = stat.executeQuery();
//6、結果處理
Student stu = new Student();
while(rs.next()) {
stu.setSno(rs.getInt("Sno"));
stu.setSname(rs.getString("Sname"));
stu.setAge(rs.getInt("age"));
stu.setGender(rs.getString("gender"));
stu.setDepartment(rs.getString("department"));
stu.setCourseSelected(rs.getString("courseSelected"));
}
return stu;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(con, stat, rs);
}
return null;
}
@Override
public boolean insertStudent(Student s) {
Connection con = null;
PreparedStatement stat = null;
try {
con = DBUtil.getConnection();
String sql = "insert into Student(Sno,Sname,age,gender,department,courseSelected) values(?,?,?,?,?,?)";
stat = con.prepareStatement(sql);
stat.setInt(1,s.getSno());
stat.setString(2,s.getSname());
stat.setInt(3,s.getAge());
stat.setString(4,s.getGender());
stat.setString(5,s.getDepartment());
stat.setString(6,s.getCourseSelected());
if(stat.executeUpdate()>0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(con, stat);
}
return false;
}
@Override
public boolean deleteStudent(int sno) {
Connection con = null;
PreparedStatement stat = null;
con = DBUtil.getConnection();
String sql = "delete from Student where Sno = ?";
try {
stat = con.prepareStatement(sql);
stat.setInt(1,sno);
if(stat.executeUpdate()>0) {
//System.out.println("删除成功");
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(con, stat);
}
return false;
}
@Override
public boolean updateStudent(Student s) {
Student stu = selectStudent(s.getSno());
Connection con = null;
PreparedStatement stat = null;
if(stu == null) {
return false;
}else {
try {
con = DBUtil.getConnection();
String sql = "update Student set Sname=?,age = ?,gender=?,department=?,courseSelected=? where Sno = ?";
stat = con.prepareStatement(sql);
stat.setString(1,s.getSname());
stat.setInt(2,s.getAge());
stat.setString(3,s.getGender());
stat.setString(4,s.getDepartment());
stat.setString(5,s.getCourseSelected());
stat.setInt(6,s.getSno());
if(stat.execute()){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(con, stat);
}return false;
}
}
}
四、下載下傳WindowBuilder插件并安裝,建立一個JFram檔案,命名為StudentInformationFrame,在design界面中設定如下布局:
設定好布局之後,點選Source就會自動生成如下代碼:
public class StudentInformationFrame extends JFrame {
private JPanel contentPane;
private JTable table;
private JTextField selectstubo;
private JTextField stuNo;//學生編号
private JTextField stuName_2;//
private JTextField stuAge;
private JTextField stuSex;
private JTextField stuDepart;
private JTextField select;
private JTable table_1;
private JTable table_2;
private JTextField updatestuno;
private JTextField updatestuname;
private JTextField updatestusex;
private JTextField updatestuage;
private JTextField updatestudept;
private JTextField updateselected;
private JTable table_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StudentInformationFrame frame = new StudentInformationFrame(true,"");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @param usertype
*/
public StudentInformationFrame() {
setFont(null);
setBackground(new Color(240, 240, 240));
setIconImage(Toolkit.getDefaultToolkit().getImage("image/stop.gif"));//視窗左上角圖示
this.setVisible(true);//設定視窗顯示隐藏
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);//設定視窗位置、尺寸(統一800*600)
setResizable(false);//設定視窗不可縮放
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);//設定視窗顔色
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//左欄
JPanel Left = new JPanel();
Left.setBackground(Color.PINK);
Left.setBounds(0, 0, 154, 572);
contentPane.add(Left);
//右欄查詢頁
JPanel Right1 = new JPanel();
Right1.setBounds(152, 0, 642, 572);
contentPane.add(Right1);
//右欄插入頁
JPanel Right2 = new JPanel();
Right2.setBounds(152, 0, 642, 572);
contentPane.add(Right2);
//右欄删除頁
JPanel Right3 = new JPanel();
Right3.setBounds(152, 0, 642, 572);
contentPane.add(Right3);
//右欄修改頁
JPanel Right4 = new JPanel();
Right4.setBounds(152, 0, 642, 572);
contentPane.add(Right4);
//菜單1:查詢,點選即可查詢全部學生記錄的按鈕
JButton menu1 = new JButton("查詢");
menu1.setFont(new Font("宋體", Font.PLAIN, 16));
menu1.setBounds(10, 10, 134, 50);
menu1.addActionListener(new ActionListener() {
Object[][] stuArray;
public void actionPerformed(ActionEvent e) {
StudentDaoImpl stuAll = new StudentDaoImpl();
ArrayList<Student> stulist = stuAll.selectStudentAll();
for (Student student : stulist) {
student.toString();
}
Object[] array = stulist.toArray();
stuArray = new Object[stulist.size()][6];
for(int i=0;i<array.length;i++) {
Student student = (Student)array[i];
stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
}
table.setModel(new DefaultTableModel(
stuArray,
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
}
));
Right1.setVisible(true);
Right2.setVisible(false);
Right3.setVisible(false);
Right4.setVisible(false);
}
});
Left.setLayout(null);
Left.add(menu1);
//菜單2:插入,點選即可跳轉至插入界面
JButton menu2 = new JButton("插入");
menu2.setBounds(10, 105, 134, 50);
menu2.setFont(new Font("宋體", Font.PLAIN, 16));
menu2.addActionListener(new ActionListener() {
Object[][] stuArray;
public void actionPerformed(ActionEvent e) {
if(!usertype) {
JOptionPane.showMessageDialog(null, "目前使用者權限不足!", "權限警告", JOptionPane.WARNING_MESSAGE);
return;
}
StudentDaoImpl stuAll = new StudentDaoImpl();
ArrayList<Student> stulist = stuAll.selectStudentAll();
for (Student student : stulist) {
student.toString();
}
Object[] array = stulist.toArray();
stuArray = new Object[stulist.size()][6];
for(int i=0;i<array.length;i++) {
Student student = (Student)array[i];
stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
}
table_1.setModel(new DefaultTableModel(
stuArray,
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
}
));
Right2.setVisible(true);
Right3.setVisible(false);
Right1.setVisible(false);
Right4.setVisible(false);
}
});
Left.add(menu2);
//菜單3:點選即可進入删除界面
JButton menu3 = new JButton("删除");
menu3.setFont(new Font("宋體", Font.PLAIN, 16));
menu3.setBounds(10, 217, 134, 50);
menu3.addActionListener(new ActionListener() {
Object stuArray [][];
public void actionPerformed(ActionEvent e) {
if(!usertype) {
JOptionPane.showMessageDialog(null, "目前使用者權限不足!", "權限警告", JOptionPane.WARNING_MESSAGE);
return;
}
StudentDaoImpl stuAll = new StudentDaoImpl();
ArrayList<Student> stulist = stuAll.selectStudentAll();
for (Student student : stulist) {
student.toString();
}
Object[] array = stulist.toArray();
stuArray = new Object[stulist.size()][6];
for(int i=0;i<array.length;i++) {
Student student = (Student)array[i];
stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
}
table_2.setModel(new DefaultTableModel(
stuArray,
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
}
));
//boolean flag = stu.deleteStudent();
Right3.setVisible(true);
Right1.setVisible(false);
Right2.setVisible(false);
Right4.setVisible(false);
}
});
Left.add(menu3);
//菜單4:點選即可進入修改界面
JButton menu4 = new JButton("修改");
menu4.setFont(new Font("宋體", Font.PLAIN, 16));
menu4.addActionListener(new ActionListener() {
Object stuArray [][];
public void actionPerformed(ActionEvent arg0) {
if(!usertype) {
JOptionPane.showMessageDialog(null, "目前使用者權限不足!", "權限警告", JOptionPane.WARNING_MESSAGE);
return;
}
StudentDaoImpl stuAll = new StudentDaoImpl();
ArrayList<Student> stulist = stuAll.selectStudentAll();
for (Student student : stulist) {
student.toString();
}
Object[] array = stulist.toArray();
stuArray = new Object[stulist.size()][6];
for(int i=0;i<array.length;i++) {
Student student = (Student)array[i];
stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
}
table_3.setModel(new DefaultTableModel(
stuArray,
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
}
));
Right4.setVisible(true);
Right1.setVisible(false);
Right2.setVisible(false);
Right3.setVisible(false);
}
});
menu4.setBounds(10, 310, 134, 50);
Left.add(menu4);
//菜單5:主菜單界面
JButton menu5 = new JButton("主菜單");
menu5.setFont(new Font("宋體", Font.PLAIN, 16));
menu5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//根據usertype使用者類型切換主菜單
if(usertype) {
new TeacherMenuFrame(usertype,uname);
}else {
new StudentMenuFrame(usertype,uname);
}
StudentInformationFrame.this.dispose();
}
});
menu5.setBounds(10, 410, 134, 50);
Left.add(menu5);
//菜單6:退出按鈕
JButton menu6 = new JButton("\u9000\u51FA");
menu6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 退出
if (JOptionPane.showConfirmDialog(null, "确認退出?", "确認", JOptionPane.OK_CANCEL_OPTION) == 0) {
System.exit(0);
}
}
});
menu6.setFont(new Font("宋體", Font.PLAIN, 16));
menu6.setBounds(10, 510, 134, 50);
Left.add(menu6);
Right3.setLayout(null);
/*
* 每個頁面中的按鈕的相應操作
*/
//查詢頁面的按鈕------------------------------------------------------------------------------------------------------
//搜尋按鈕,查詢單個學生的記錄
JButton btnNewButton = new JButton("搜尋");
btnNewButton.addActionListener(new ActionListener() {
Object[][] stuArray;
public void actionPerformed(ActionEvent e) {
StudentDaoImpl stu = new StudentDaoImpl();
String stuId = selectstubo.getText();
if(stuId.equals("")) {
JOptionPane.showMessageDialog(null, "搜尋内容不能為空", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
int stuid = Integer.parseInt(selectstubo.getText());
Student student = stu.selectStudent(stuid);
stuArray = new Object[1][6];
stuArray[0] = new Object[] {student.getSno(),student.getSname(),student.getGender(),student.getDepartment(),student.getAge(),student.getCourseSelected()};
Right1.setVisible(true);
Right2.setVisible(false);
Right3.setVisible(false);
Right4.setVisible(false);
table.setModel(new DefaultTableModel(
stuArray,
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
}
));
}
});
btnNewButton.setBounds(421, 33, 78, 23);
Right1.add(btnNewButton);
//插入資料按鈕,點選可實作頁面跳轉至插入操作
JButton btnNewButton_6 = new JButton("插入資料");
btnNewButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!usertype) {
JOptionPane.showMessageDialog(null, "目前使用者權限不足!", "權限警告", JOptionPane.WARNING_MESSAGE);
return;
}
Right2.setVisible(true);
Right1.setVisible(false);
Right4.setVisible(false);
Right3.setVisible(false);
}
});
btnNewButton_6.setBounds(68, 495, 93, 23);
Right1.add(btnNewButton_6);
//删除資料按鈕,點選跳轉是删除資料操作
JButton btnNewButton_7 = new JButton("\u5220\u9664\u6570\u636E");
btnNewButton_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!usertype) {
JOptionPane.showMessageDialog(null, "目前使用者權限不足!", "權限警告", JOptionPane.WARNING_MESSAGE);
return;
}
Right3.setVisible(true);
Right1.setVisible(false);
Right4.setVisible(false);
Right2.setVisible(false);
}
});
btnNewButton_7.setBounds(190, 495, 93, 23);
Right1.add(btnNewButton_7);
//修改資料按鈕,點選某條記錄後再點選此按鈕即可跳轉至修改資料按鈕
JButton btnNewButton_8 = new JButton("修改資料");
btnNewButton_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!usertype) {
JOptionPane.showMessageDialog(null, "目前使用者權限不足!", "權限警告", JOptionPane.WARNING_MESSAGE);
return;
}
int row = table.getSelectedRow();
String str = table.getValueAt(row,0).toString();
String str1 = table.getValueAt(row,1).toString();
String str2 = table.getValueAt(row,2).toString();
String str3 = table.getValueAt(row,3).toString();
String str4 = table.getValueAt(row,4).toString();
String str5 = table.getValueAt(row,5).toString();
updatestuno.setText(str);
updatestuno.setEditable(false);
updatestuname.setText(str1);
updatestusex.setText(str2);
updatestuage.setText(str3);
updatestudept.setText(str4);
updateselected.setText(str5);
Right4.setVisible(true);
Right3.setVisible(false);
Right1.setVisible(false);
Right2.setVisible(false);
}
});
btnNewButton_8.setBounds(306, 495, 93, 23);
Right1.add(btnNewButton_8);
//删除頁面---------------------------------------------------------------------------------------------------------
//點選删除按鈕,删除單個記錄
JButton btnNewButton_2 = new JButton("删除資料");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table_2.getSelectedRow();
String str = table_2.getValueAt(row,0).toString();
int sno = Integer.valueOf(str);
StudentDaoImpl stu = new StudentDaoImpl();
boolean flag = stu.deleteStudent(sno);
if(flag) {
System.out.println("删除成功");
}else {
System.out.println("删除失敗");
}
}
});
btnNewButton_2.setBounds(104, 54, 134, 23);
Right3.add(btnNewButton_2);
//點選重新整理按鈕,可重新整理記錄
JButton btnNewButton_3 = new JButton("\u5237\u65B0\u8BB0\u5F55");
btnNewButton_3.addActionListener(new ActionListener() {
Object stuArray [][];
public void actionPerformed(ActionEvent e) {
StudentDaoImpl stuAll = new StudentDaoImpl();
ArrayList<Student> stulist = stuAll.selectStudentAll();
for (Student student : stulist) {
student.toString();
}
Object[] array = stulist.toArray();
stuArray = new Object[stulist.size()][6];
for(int i=0;i<array.length;i++) {
Student student = (Student)array[i];
stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
}
table_2.setModel(new DefaultTableModel(
stuArray,
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
}
));
}
});
btnNewButton_3.setBounds(337, 54, 134, 23);
Right3.add(btnNewButton_3);
JScrollPane scrollPane_2 = new JScrollPane();
scrollPane_2.setBounds(54, 110, 527, 325);
Right3.add(scrollPane_2);
//删除操作的表
table_2 = new JTable();
table_2.setRowHeight(30);
scrollPane_2.setViewportView(table_2);
table_2.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
},
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u9009\u8BFE\u60C5\u51B5"
}
));
Right1.setLayout(null);
JScrollPane scrollPane_4 = new JScrollPane();
scrollPane_4.setBounds(68, 85, 475, 379);
Right1.add(scrollPane_4);
JScrollPane scrollPane = new JScrollPane();
scrollPane_4.setViewportView(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
table.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
},
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u9009\u8BFE\u60C5\u51B5"
}
));
table.getColumnModel().getColumn(0).setPreferredWidth(85);
table.getColumnModel().getColumn(2).setPreferredWidth(76);
table.getColumnModel().getColumn(4).setPreferredWidth(92);
table.setRowHeight(35);
JLabel lblNewLabel = new JLabel("\u8F93\u5165\u67E5\u8BE2\u5B66\u751F\u7684\u7F16\u53F7");
lblNewLabel.setBounds(73, 35, 140, 18);
Right1.add(lblNewLabel);
selectstubo = new JTextField();
selectstubo.setBounds(204, 34, 163, 21);
Right1.add(selectstubo);
selectstubo.setColumns(10);
Right1.setVisible(true);
Right2.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("\u5B66\u751F\u7F16\u53F7");
lblNewLabel_1.setBounds(70, 60, 81, 15);
Right2.add(lblNewLabel_1);
stuNo = new JTextField();
stuNo.setBounds(128, 57, 109, 21);
Right2.add(stuNo);
stuNo.setColumns(10);
JLabel label_1 = new JLabel("\u5B66\u751F\u59D3\u540D");
label_1.setBounds(247, 60, 66, 15);
Right2.add(label_1);
stuName_2 = new JTextField();
stuName_2.setColumns(10);
stuName_2.setBounds(305, 57, 109, 21);
Right2.add(stuName_2);
JLabel label_2 = new JLabel("\u5E74 \u9F84");
label_2.setBounds(424, 60, 48, 15);
Right2.add(label_2);
stuAge = new JTextField();
stuAge.setColumns(10);
stuAge.setBounds(482, 57, 109, 21);
Right2.add(stuAge);
JLabel label_3 = new JLabel("\u6027 \u522B");
label_3.setBounds(70, 92, 67, 15);
Right2.add(label_3);
stuSex = new JTextField();
stuSex.setColumns(10);
stuSex.setBounds(128, 89, 109, 21);
Right2.add(stuSex);
stuDepart = new JTextField();
stuDepart.setColumns(10);
stuDepart.setBounds(305, 85, 109, 21);
Right2.add(stuDepart);
select = new JTextField();
select.setColumns(10);
select.setBounds(482, 88, 109, 21);
Right2.add(select);
JLabel label_4 = new JLabel("\u6240\u5C5E\u9662\u6821");
label_4.setBounds(247, 85, 66, 15);
Right2.add(label_4);
JLabel label_5 = new JLabel("\u9009\u8BFE\u60C5\u51B5");
label_5.setBounds(423, 85, 66, 15);
Right2.add(label_5);
//點選添加記錄
JButton btnNewButton_1 = new JButton("\u6DFB\u52A0");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int sno = Integer.parseInt(stuNo.getText());
String sname = stuName_2.getText();
int sage = Integer.parseInt(stuAge.getText());
String sgender = stuSex.getText();
String sdepart = stuDepart.getText();
String selected = select.getText();
Student student = new Student(sno,sname,sage,sgender,sdepart,selected);
StudentDaoImpl stu = new StudentDaoImpl();
boolean flag = stu.insertStudent(student);
if(flag) {
System.out.println("插入成功");
}else {
System.out.println("插入失敗");
}
}
});
btnNewButton_1.setBounds(371, 138, 101, 23);
Right2.add(btnNewButton_1);
//重新整理記錄
JButton button = new JButton("\u5237\u65B0\u8BB0\u5F55");
button.addActionListener(new ActionListener() {
Object[][] stuArray;
public void actionPerformed(ActionEvent e) {
StudentDaoImpl stuAll = new StudentDaoImpl();
ArrayList<Student> stulist = stuAll.selectStudentAll();
for (Student student : stulist) {
student.toString();
}
Object[] array = stulist.toArray();
stuArray = new Object[stulist.size()][6];
for(int i=0;i<array.length;i++) {
Student student = (Student)array[i];
stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
}
table_1.setModel(new DefaultTableModel(
stuArray,
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
}
));
}
});
button.setBounds(490, 138, 101, 23);
Right2.add(button);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(71, 181, 520, 320);
Right2.add(scrollPane_1);
table_1 = new JTable();
scrollPane_1.setViewportView(table_1);
table_1.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
},
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u6821", "\u9009\u8BFE\u60C5\u51B5"
}
));
table_1.setRowHeight(30);
Right2.setVisible(false);
Right4.setLayout(null);
JLabel lblNewLabel_3 = new JLabel("\u5B66\u751F\u7F16\u53F7");
lblNewLabel_3.setBounds(76, 34, 66, 15);
Right4.add(lblNewLabel_3);
updatestuno = new JTextField();
updatestuno.setBounds(147, 31, 178, 21);
Right4.add(updatestuno);
updatestuno.setColumns(10);
updatestuname = new JTextField();
updatestuname.setBounds(147, 59, 178, 21);
Right4.add(updatestuname);
updatestuname.setColumns(10);
JLabel lblNewLabel_5 = new JLabel("\u4FEE\u6539\u6027\u522B");
lblNewLabel_5.setBounds(76, 90, 70, 15);
Right4.add(lblNewLabel_5);
updatestusex = new JTextField();
updatestusex.setBounds(147, 87, 178, 21);
Right4.add(updatestusex);
updatestusex.setColumns(10);
JLabel lblNewLabel_6 = new JLabel("\u4FEE\u6539\u5E74\u9F84");
lblNewLabel_6.setBounds(76, 115, 66, 15);
Right4.add(lblNewLabel_6);
updatestuage = new JTextField();
updatestuage.setBounds(147, 112, 178, 21);
Right4.add(updatestuage);
updatestuage.setColumns(10);
JLabel lblNewLabel_7 = new JLabel("\u4FEE\u6539\u9662\u7CFB");
lblNewLabel_7.setBounds(76, 140, 66, 15);
Right4.add(lblNewLabel_7);
updatestudept = new JTextField();
updatestudept.setBounds(147, 137, 178, 21);
Right4.add(updatestudept);
updatestudept.setColumns(10);
JLabel lblNewLabel_8 = new JLabel("\u4FEE\u6539\u9009\u8BFE\u60C5\u51B5");
lblNewLabel_8.setBounds(64, 165, 72, 15);
Right4.add(lblNewLabel_8);
updateselected = new JTextField();
updateselected.setBounds(147, 162, 178, 21);
Right4.add(updateselected);
updateselected.setColumns(10);
JLabel lblNewLabel_9 = new JLabel("");
lblNewLabel_9.setBounds(337, 37, 60, 15);
Right4.add(lblNewLabel_9);
JScrollPane scrollPane_3 = new JScrollPane();
scrollPane_3.setBounds(64, 207, 507, 328);
Right4.add(scrollPane_3);
table_3 = new JTable();
scrollPane_3.setViewportView(table_3);
table_3.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
{null, null, null, null, null, null},
},
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u9009\u8BFE\u60C5\u51B5"
}
));
table_3.setRowHeight(30);
//點選修改按鈕,修改記錄
JButton btnNewButton_4 = new JButton("\u70B9\u51FB\u4FEE\u6539");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int stuno = Integer.valueOf(updatestuno.getText());
String stuname = updatestuname.getText();
String stusex = updatestusex.getText();
int stuage = Integer.parseInt(updatestuage.getText());
//System.out.println(updatestuage.getText()+stuage);
String studept = updatestudept.getText();
String stuselected = updateselected.getText();
if(updatestuno.getText().length()==0) {
JOptionPane.showMessageDialog(null, "學号不能為空");
}
StudentDaoImpl stu = new StudentDaoImpl();
Student student = new Student();
student = stu.selectStudent(stuno);
student.setSno(stuno);
student.setSname(stuname);
student.setGender(stusex);
student.setAge(stuage);
student.setDepartment(studept);
student.setCourseSelected(stuselected);
if(!stu.updateStudent(student)) {
System.out.println("修改成功");
}else {
System.out.println("修改失敗");
}
}
});
btnNewButton_4.setBounds(358, 111, 178, 23);
Right4.add(btnNewButton_4);
//修改頁面:點選重新整理按鈕
JButton btnNewButton_5 = new JButton("\u70B9\u51FB\u5237\u65B0");
btnNewButton_5.addActionListener(new ActionListener() {
Object [][]stuArray;
public void actionPerformed(ActionEvent e) {
StudentDaoImpl stuAll = new StudentDaoImpl();
ArrayList<Student> stulist = stuAll.selectStudentAll();
for (Student student : stulist) {
student.toString();
}
Object[] array = stulist.toArray();
stuArray = new Object[stulist.size()][6];
for(int i=0;i<array.length;i++) {
Student student = (Student)array[i];
stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
}
table_3.setModel(new DefaultTableModel(
stuArray,
new String[] {
"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
}
));
}
});
btnNewButton_5.setBounds(358, 161, 178, 23);
Right4.add(btnNewButton_5);
JLabel lblNewLabel_10 = new JLabel("\u4FEE\u6539\u59D3\u540D");
lblNewLabel_10.setBounds(76, 62, 66, 15);
Right4.add(lblNewLabel_10);
JButton btnNewButton_9 = new JButton("\u70B9\u51FB\u9009\u62E9\uFF0C\u81EA\u52A8\u586B\u5165\u6570\u636E");
btnNewButton_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table_3.getSelectedRow();
String str = table_3.getValueAt(row,0).toString();
String str1 = table_3.getValueAt(row,1).toString();
String str2 = table_3.getValueAt(row,2).toString();
String str3 = table_3.getValueAt(row,3).toString();
String str4 = table_3.getValueAt(row,4).toString();
String str5 = table_3.getValueAt(row,5).toString();
updatestuno.setText(str);
updatestuno.setEditable(false);
updatestuname.setText(str1);
updatestusex.setText(str2);
updatestuage.setText(str3);
updatestudept.setText(str4);
updateselected.setText(str5);
}
});
btnNewButton_9.setBounds(358, 58, 178, 23);
Right4.add(btnNewButton_9);
Right4.setVisible(false);
Right3.setVisible(false);
setLocationRelativeTo(null);//設定視窗居中顯示
}
}