您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么使用Java+mysql實(shí)現(xiàn)學(xué)籍管理系統(tǒng)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“怎么使用Java+mysql實(shí)現(xiàn)學(xué)籍管理系統(tǒng)”吧!
項(xiàng)目使用mysql數(shù)據(jù)庫(kù),有2張表格。一張用戶表用于登錄驗(yàn)證,一張學(xué)生表,用于增刪改查。
creat table t_user( id int primary key auto_increment, login_name varchar(255), login_pwd varchar(255), real_name varchar(255), ); insert into t_user(id,login_name,login_pwd,real_name) values('akm',"123",'蘿卜蹲'); CREATE TABLE t_user( id char(12) PRIMARY KEY, name char(6), pwd varchar(255), );
1.1登錄界面
在用戶爛輸入:akm
密碼欄輸入:123
點(diǎn)擊登錄按鈕,就可以直接進(jìn)入系統(tǒng)。
如果輸入錯(cuò)誤,狀態(tài)欄會(huì)顯示登錄失敗,并清空登錄賬戶和密碼。
1.2系統(tǒng)主界面
系統(tǒng)主界面由5個(gè)按鈕組成
添加學(xué)生信息,在主界面中選擇添加按鈕并點(diǎn)擊進(jìn)入添加界面,如上圖所示。在界面中添加相應(yīng)的學(xué)生信息,id,姓名,年齡 學(xué)籍等。
1.3查詢信息
通過(guò)id查詢學(xué)生信息。
1.4遍歷信息
1.5 刪除信息
輸入id直接刪除。
1.6 更新信息
項(xiàng)目只有一個(gè)test文件,沒(méi)有封裝,有需要的小伙伴可以自己進(jìn)行封裝。
代碼如下:
package com.company; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; import java.sql.Statement; import java.util.Scanner; public class Test { static Connection conn ; static Statement statement; public static void main(String[] args) { Scanner in = new Scanner(System.in); login(); } public static void control() { JFrame jf = new JFrame("學(xué)生學(xué)籍管理系統(tǒng)"); jf.setLayout(new FlowLayout(FlowLayout.LEFT)); jf.setBounds(400, 300, 300, 200); JButton button = new JButton("更新"); JButton button1=new JButton("遍歷"); JButton button2=new JButton("刪除"); JButton button3=new JButton("添加"); JButton button4=new JButton("查詢"); jf.add(button); jf.add(button1); jf.add(button2); jf.add(button3); jf.add(button4); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { update(); } }); button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { query(); } }); button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { delete(); } }); button3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { insert(); } }); button4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {onequery();} }); jf.setVisible(true); jf.setResizable(false); button.setSize(40, 20); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void update() { conn = getConnection(); JFrame jf = new JFrame("學(xué)生學(xué)籍管理系統(tǒng)"); jf.setLayout(new FlowLayout(FlowLayout.LEFT)); jf.setBounds(400, 300, 300, 200); JLabel label1 = new JLabel("年齡"); JTextField agetext = new JTextField("", 10); JLabel label2 = new JLabel("id"); JTextField idtext = new JTextField("", 10); JLabel label3 = new JLabel("學(xué)籍"); JTextField addresstext = new JTextField("", 10); JLabel label4 = new JLabel("姓名"); JTextField nametext = new JTextField("", 5); JTextField out = new JTextField("更新狀態(tài)", 20); JButton button = new JButton("更新"); jf.add(label1); jf.add(agetext); jf.add(label2); jf.add(idtext); jf.add(label3); jf.add(addresstext); jf.add(label4); jf.add(nametext); jf.add(out); jf.add(button); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { PreparedStatement ps=null; String age = agetext.getText(); String id = idtext.getText(); String address= addresstext.getText(); String name=nametext.getText(); try { // 更新數(shù)據(jù)的sql語(yǔ)句 String sql = "update student set age =? , address =?, name=? where id = ?"; ps=conn.prepareStatement(sql); ps.setString(1,agetext.getText()); ps.setString(2,addresstext.getText()); ps.setString(3,nametext.getText()); ps.setString(4,idtext.getText()); int count = ps.executeUpdate();//記錄操作次數(shù) // 輸出插入操作的處理結(jié)果 System.out.println("user表中更新 " + count + " 條數(shù)據(jù)"); ps.close(); //關(guān)閉數(shù)據(jù)庫(kù)連接 conn.close(); out.setText("更新成功?。。。。。。?!"); // 創(chuàng)建用于執(zhí)行靜態(tài)sql語(yǔ)句的Statement對(duì)象,st屬局部變量 } catch (SQLException a) { System.out.println("更新數(shù)據(jù)失敗"); } } }); jf.setVisible(true); jf.setResizable(false); button.setSize(40, 20); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void query() { PreparedStatement ps=null; conn = getConnection(); JFrame jf = new JFrame("學(xué)生學(xué)籍管理系統(tǒng)"); jf.setLayout(null); jf.setBounds(400, 300, 350, 200); JButton button = new JButton("查詢"); JTextArea jm=new JTextArea("ID\t姓名\t年齡\t學(xué)籍");//顯示界面 jm.setBounds(10,50,350,100);//定義顯示界面位置 jf.add(button); jf.add(jm); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { PreparedStatement ps=null; try { String sql = "select * from Student"; //創(chuàng)建用于執(zhí)行靜態(tài)sql語(yǔ)句的Statement對(duì)象,statement屬局部變量 statement = conn.createStatement();//獲取操作對(duì)象 ResultSet resultSet = statement.executeQuery(sql);// executeQuery執(zhí)行單個(gè)SQL語(yǔ)句,返回單個(gè)ResultSet對(duì)象是 while (resultSet.next())//循環(huán)沒(méi)有數(shù)據(jù)的時(shí)候返回flase退出循環(huán) { Integer Id = resultSet.getInt("id");//resultSet.next()是一個(gè)光標(biāo) String name = resultSet.getString("name");//getString返回的值一定是string Integer age = resultSet.getInt("age"); String address=resultSet.getString("address"); //String adress = resultSet.getString("adress"); //輸出查到的記錄的各個(gè)字段的值 jm.append("\n"+Id + "\t" + name + "\t" + age+ "\t" + address ); } statement.close(); conn.close(); }catch (SQLException b){ System.out.println("查詢失?。。。。。。。。?quot;); } } }); jf.setVisible(true); jf.setResizable(false); button.setSize(40, 20); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void delete() { conn = getConnection(); JFrame jf = new JFrame("學(xué)生學(xué)籍管理系統(tǒng)"); jf.setLayout(new FlowLayout(FlowLayout.LEFT)); jf.setBounds(400, 300, 300, 200); JLabel label2 = new JLabel("id"); JTextField idtext = new JTextField("", 10); JTextField out = new JTextField("刪除狀態(tài)", 20); JButton button = new JButton("刪除"); jf.add(label2); jf.add(idtext); jf.add(out); jf.add(button); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String id = idtext.getText(); PreparedStatement ps=null; try { // 刪除數(shù)據(jù)的sql語(yǔ)句 String sql = "delete from Student where id = ?"; ps=conn.prepareStatement(sql); ps.setString(1,idtext.getText()); int count = ps.executeUpdate();//記錄操作次數(shù) // 輸出插入操作的處理結(jié)果 System.out.println("student表中刪除 " + count + " 條數(shù)據(jù)"); ps.close(); out.setText("刪除成功?。。。。。。。?quot;); // 關(guān)閉數(shù)據(jù)庫(kù)連接 conn.close(); } catch (SQLException c) { System.out.println("刪除數(shù)據(jù)失敗"); } } }); jf.setVisible(true); jf.setResizable(false); button.setSize(40, 20); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void insert() { // 首先要獲取連接,即連接到數(shù)據(jù)庫(kù) conn = getConnection(); JFrame jf = new JFrame("學(xué)生學(xué)籍管理系統(tǒng)"); jf.setLayout(new FlowLayout(FlowLayout.LEFT)); jf.setBounds(400, 300, 300, 200); JLabel label3 = new JLabel("id"); JTextField idtext = new JTextField("", 10); JLabel label1 = new JLabel("年齡"); JTextField agetext = new JTextField("", 10); JLabel label2 = new JLabel("姓名"); JTextField nametext = new JTextField("", 10); JLabel label4 = new JLabel("學(xué)籍"); JTextField addresstext = new JTextField("", 5); JTextField out = new JTextField("添加狀態(tài)", 20); JButton button = new JButton("添加"); jf.add(label3); jf.add(idtext); jf.add(label1); jf.add(agetext); jf.add(label2); jf.add(nametext); jf.add(label4); jf.add(addresstext); jf.add(out); jf.add(button); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String age = agetext.getText(); String name = nametext.getText(); String id=idtext.getText(); String address=addresstext.getText(); try { PreparedStatement ps=null; // 插入數(shù)據(jù)的sql語(yǔ)句 String sql = "INSERT INTO Student( id,age,name,address) VALUES ( ?,?,?,?)"; ps=conn.prepareStatement(sql); ps.setString(1,idtext.getText()); ps.setString(2,agetext.getText()); ps.setString(3,nametext.getText()); ps.setString(4,addresstext.getText()); int count = ps.executeUpdate();//記錄操作次數(shù) // 輸出插入操作的處理結(jié)果 System.out.println("向user表中插入 " + count + " 條數(shù)據(jù)"); ps.close(); out.setText("添加成功?。。。。。。?!"); // 關(guān)閉數(shù)據(jù)庫(kù)連接 conn.close(); } catch (SQLException d) { System.out.println("插入數(shù)據(jù)失敗" + d.getMessage()); } } }); jf.setVisible(true); jf.setResizable(false); button.setSize(40, 20); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static boolean login(){ conn = getConnection(); JFrame jf = new JFrame("學(xué)生學(xué)籍管理系統(tǒng)"); jf.setLayout(new FlowLayout(FlowLayout.LEFT)); jf.setBounds(400, 300, 300, 200); JLabel label1 = new JLabel("用戶名"); JTextField usernametext = new JTextField("", 20); JLabel label2 = new JLabel("密碼"); JPasswordField pwdtext = new JPasswordField("", 20); JTextField out = new JTextField("登錄狀態(tài)", 20); JButton button = new JButton("登錄"); jf.add(label1); jf.add(usernametext); jf.add(label2); jf.add(pwdtext); jf.add(out); jf.add(button); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 插入數(shù)據(jù)的sql語(yǔ)句 PreparedStatement ps=null; try { statement = conn.createStatement();//獲取操作對(duì)象 String x = usernametext.getText(); String y = pwdtext.getText(); String sql ="select * from user"; ResultSet resultSet = statement.executeQuery(sql); while(resultSet.next()){ String a=resultSet.getString("login_name"); String b=resultSet.getString("login_pwd"); if(a.equals(x)&&b.equals(y)) { control(); out.setText("!!!!!登錄成功!!!!!"); } else if(x!=a&&y!=b){ out.setText("登錄失敗,請(qǐng)重新輸入"); } } usernametext.setText(""); pwdtext.setText(""); } catch (SQLException throwables) { throwables.printStackTrace(); } } }); jf.setVisible(true); jf.setResizable(false); button.setSize(40, 20); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); boolean ok = true; return ok; } public static void onequery() { PreparedStatement ps=null; conn = getConnection(); JFrame jf = new JFrame("學(xué)生學(xué)籍管理系統(tǒng)"); jf.setLayout(new FlowLayout(FlowLayout.LEFT)); jf.setBounds(400, 300, 350, 200); JLabel label3 = new JLabel("id"); JTextField idtext = new JTextField("", 10); JLabel label1 = new JLabel("條件"); JTextField atext = new JTextField("", 10); JButton button = new JButton("查詢"); //JTextArea jm=new JTextArea("ID\t姓名\t年齡\t學(xué)籍");//顯示界面 //jm.setBounds(10,50,350,100);//定義顯示界面位置 jf.add(label3); jf.add(idtext); jf.add(button); //jf.add(jm); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { PreparedStatement ps=null; String id=idtext.getText(); conn = getConnection(); try { String sql = "select * from student where id = ?"; //創(chuàng)建用于執(zhí)行靜態(tài)sql語(yǔ)句的Statement對(duì)象,statement屬局部變量 ps=conn.prepareStatement(sql);//獲取操作對(duì)象 ps.setString(1,idtext.getText().toString()); ResultSet resultSet = ps.executeQuery();// executeQuery執(zhí)行單個(gè)SQL語(yǔ)句,返回單個(gè)ResultSet對(duì)象是 while (resultSet.next())//循環(huán)沒(méi)有數(shù)據(jù)的時(shí)候返回flase退出循環(huán) { Integer Id = resultSet.getInt("id");//resultSet.next()是一個(gè)光標(biāo) String name = resultSet.getString("name");//getString返回的值一定是string Integer age = resultSet.getInt("age"); String address=resultSet.getString("address"); System.out.println(Id + " " + name + " " + age + " "+ address + " " ); //輸出查到的記錄的各個(gè)字段的值 //jm.append("\n"+Id + "\t" + name + "\t" + age+ "\t" + address ); } statement.close(); conn.close(); }catch (SQLException m){ System.out.println(m.getMessage()); System.out.println("查詢失敗?。。。。。。?!"); } } }); jf.setVisible(true); jf.setResizable(false); button.setSize(40, 20); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static Connection getConnection(){ //創(chuàng)建用于連接數(shù)據(jù)庫(kù)的Connection對(duì)象 Connection connection = null; try { // 加載Mysql數(shù)據(jù)驅(qū)動(dòng) Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("數(shù)據(jù)庫(kù)驅(qū)動(dòng)加載成功"); String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; // 創(chuàng)建數(shù)據(jù)連接 connection = DriverManager.getConnection(url, "root", "root"); System.out.println("數(shù)據(jù)庫(kù)連接成功"); }catch (ClassNotFoundException | SQLException e){ System.out.println("數(shù)據(jù)庫(kù)連接失敗" + e.getMessage());//處理查詢結(jié)果 } return connection; } }
感謝各位的閱讀,以上就是“怎么使用Java+mysql實(shí)現(xiàn)學(xué)籍管理系統(tǒng)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)怎么使用Java+mysql實(shí)現(xiàn)學(xué)籍管理系統(tǒng)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。