1. Preface
I learned JAVA language this semester. At the end of the semester, I wrote a management system with an operation interface and associated with the database to consolidate the knowledge I learned in this study.
Knowledge used: JAVA basics, JAVA interface design (GUI), Oracle database (need to master the basic operation statements of the database), and link database.
Development Tools Used: MyEclipse Professional 2014
2. Design
The attributes we manage are: project number, project name, participant, person in charge, project start time, and end time. The scientific research project system has four main functions: the addition, deletion, modification and query of scientific research projects. And the login mode designed to increase system security.
2.1 Add: Add all information about scientific research projects to the table in the database
After adding, use SQL statements to search in the console to verify that it has been added to the database.
2.2 Query: Find all information about the project by a unique project number
2.3 Modification: Select the project to be modified according to the project number, and re-enter the project information to modify it
2.4 Delete: Delete all information of the corresponding project through a unique project number
3. Form source code
3.1 Login interface
package Scientific research information management system;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class LoginWindows extends Frame implements WindowListener,ActionListener { public Label lgLabel; //Username tag public Label pwdLabel; //Password tag public TextField lgText; //Username text box public TextField pwdText; //Password text box public Button lgBt; //Login button public Button quitBt; //Exit button public LoginWindows() { super(); this.setSize(400, 300); this.setTitle("Scientific Information Management System"); this.setLayout(null); lgLabel=new Label(); lgLabel.setText("Login Account:"); lgLabel.setSize(60, 30); lgLabel.setLocation(70, 70); pwdLabel=new Label(); pwdLabel.setText("Password:"); pwdLabel.setSize(60, 30); pwdLabel.setLocation(70, 150); lgText=new TextField(); lgText.setSize(180, 30); lgText.setLocation(140, 70); pwdText=new TextField(); pwdText.setSize(180, 30); pwdText.setLocation(140, 150); lgBt=new Button(); lgBt.setLabel("Login"); lgBt.setSize(60, 30); lgBt.setLocation(120, 220); quitBt=new Button(); quitBt.setLabel("Exit"); quitBt.setSize(60,30); quitBt.setLocation(220,220); quitBt.addActionListener(this); lgBt.addActionListener(this); this.addWindowListener(this); this.add(lgLabel); this.add(pwdLabel); this.add(lgText); this.add(lgBt); this.add(quitBt); this.setVisible(true); } public static void main(String args[]) { LoginWindows main=new LoginWindows(); } @Override public void actionPerformed(ActionEvent e) { Button bt=(Button) e.getSource(); if(bt.getLabel().equals("Exit")) { System.exit(0); } else { if ((lgText.getText().equals(""))||(pwdText.getText().equals(""))) { JOptionPane.showMessageDialog(this,"Account or password is empty"); } else { if ((lgText.getText().equals("admin"))&&(pwdText.getText().equals("111"))) //if((lgText.getText().equals(""))||(pwdText.getText().equals(""))) { this.setVisible(false); // Sqlwindow sql=new Sqlwindow(); WindowsView w=new WindowsView(); w.SciencePro(); } else { JOptionPane.showMessageDialog(this, "No permission"); } }}} @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method busb } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method busb System.exit(0); } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method busb } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method busb } @Override public void windowDeiconized(WindowEvent e) { // TODO Auto-generated method busb } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method busb } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method busb }}3.2 Main form source code
package scientific research information management system; import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;import javax.swing.tree.*;public class WindowsView implements TreeSelectionListener,ActionListener //Window class { JFrame main; JPanel leftPa; JPanel upPa; JPanel downPa; ///Query control JLabel numLa; //Query JTextField numTxt; JButton numBt; JTextField nameTxt; //Show the text box that displays the project name JTextField peopleTxt; //Show the participant JTextField principalTxt; //Show the main person in charge JTextField timeStartTxt; //Show the text box that displays the start time JTextField timeEndTxt; //Show the text box that displays the expected end time //Add control JLabel anumLa; JLabel anameLa; JLabel apeopleLa; JLabel aprincipalLa; JLabel atimeStartLa; JLabel atimeEndLa; JTextField anumTxt; JTextField anameTxt; JTextField apeopleTxt; JTextField aprincipalTxt; JTextField atimeStartTxt; JTextField atimeEndTxt; JButton addBt; JTable showTable; //Delete control JLabel dnumLa; JButton dnumBt; JTextField dnumTxt; //Modify control JLabel alnumLa; JTextField allnumTxt; JButton alseBt; JLabel allnumLa; JLabel allnumLa; JLabel alnameLa; JLabel alpeopleLa; JLabel alprincipalLa; JLabel altimeStartLa; JLabel altimeEndLa; JTextField alnumTxt; JTextField alnameTxt; JTextField alpeopleTxt; JTextField alprincipalTxt; JTextField altimeStartTxt; JTextField altimeEndTxt; public void SciencePro() { main=new JFrame(); main.setSize(800,800); main.setTitle("Scientific Research Information Management"); main.setLayout(null); leftPa=new JPanel(); leftPa.setSize(150, 600); leftPa.setLocation(0, 0); leftPa.setBackground(Color.white); initLeftPanel(); main.add(leftPa); upPa=new JPanel(); upPa.setSize(650, 400); upPa.setLocation(150, 0); upPa.setBackground(Color.gray); main.add(upPa); downPa=new JPanel(); downPa.setSize(650, 400); upPa.setBackground(Color.gray); main.add(upPa); downPa=new JPanel(); downPa.setSize(650, 400); downPa.setLocation(150, 400); downPa.setBackground(Color.orange); main.add(downPa); main.setVisible(true); } private void initLeftPanel() { String[] strs={"Query","Add","Delete","Modify"}; JTree tree=new JTree(strs); tree.addTreeSelectionListener(this); leftPa.add(tree); } public void valueChanged(TreeSelectionEvent e) { JTree tree=(JTree)e.getSource(); DefaultMutableTreeNode selectionNode =(DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); String str=selectionNode.toString(); if(str.equals("query")) { initUpDownPaWhenSearch(); } if(str.equals("add")) { initUpDownPaWhenAdd(); } if(str.equals("delete")) { initUpDownPaWhenDelete(); } if(str.equals("modify")) { initUpDownPaWhenAlert(); } } private void initUpDownPaWhenSearch() { //Clear upPa.removeAll(); downPa.removeAll(); // Dynamically load the controls on the upper and lower panels upPa.setLayout(null); numLa=new JLabel(); numLa.setText("Please enter the project number"); numLa.setLocation(40, 60); numLa.setSize(100, 40); numTxt=new JTextField(); numTxt.setLocation(180, 60); numTxt.setSize(200, 30); numBt=new JButton(); numBt.setText("query"); numBt.addActionListener(this); numBt.setLocation(250, 160); numBt.setSize(60, 30); upPa.add(numLa); upPa.add(numTxt); upPa.add(numBt); upPa.validate(); upPa.repaint(); numBt.addActionListener(this); // Dynamically load the control of the panel nameTxt=new JTextField(); nameTxt.setLocation(80, 50); nameTxt.setSize(300, 30); peopleTxt=new JTextField(); peopleTxt.setLocation(80, 100); peopleTxt.setSize(300, 30); principalTxt=new JTextField(); principalTxt.setLocation(80, 150); principalTxt.setSize(300, 30); timeStartTxt=new JTextField(); timeStartTxt.setLocation(80, 200); timeStartTxt.setSize(300, 30); timeEndTxt=new JTextField(); timeEndTxt.setLocation(80, 250); timeEndTxt.setSize(300, 30); downPa.setLayout(null); downPa.add(nameTxt); downPa.add(peopleTxt); downPa.add(principalTxt); downPa.add(timeStartTxt); downPa.add(timeEndTxt); downPa.validate(); downPa.repaint(); } public void actionPerformed(ActionEvent e) { JButton bt=(JButton)e.getSource(); //bt.addActionListener(this); if(bt.getText().equals("Query")) { if(numTxt.getText().equals("")) { JOptionPane.showMessageDialog(null, "Please enter the project number"); } else { DealSearch deal=new DealSearch(); String inf=deal.findProByNum(Integer.parseInt(numTxt.getText().trim())); if((inf!=null)&&(!inf.equals("")))) { String[] strs=inf.split(","); nameTxt.setText(strs[1]); peopleTxt.setText(strs[2]); principalTxt.setText(strs[3]); timeStartTxt.setText(strs[4]); timeEndTxt.setText(strs[5]); } } } if(bt.getText().equals("New")) { if (anumTxt.getText().equals("")||anameTxt.getText().equals("")||apeopleTxt.getText().equals("")||aprincipalTxt.getText().equals("")||atimeStartTxt.getText().equals("")||atimeEndTxt.getText().equals("")) { JOptionPane.showMessageDialog(null, "There can't be a null value in the input!!"); } else { ScienceProject s=new ScienceProject(); s.SetNum(Integer.parseInt(anumTxt.getText())); s.SetName(nameTxt.getText()); s.SetPeople(apeopleTxt.getText()); s.SetLeader(aprincipalTxt.getText()); s.SetTimeStart(atimeStartTxt.getText()); s.SetTimeFinish(atimeEndTxt.getText()); DealAdd deal=new DealAdd(); deal.add(s); } } if(bt.getText().equals("Delete")) { if(Integer.parseInt(dnumTxt.getText())==0) { JOptionPane.showMessageDialog(null, "Cannot delete empty item number!!"); } else { DealDelete deal=new DealDelete(); deal.delete(Integer.parseInt(dnumTxt.getText().trim())); } } if(bt.getText().equals("Submit")) { if(Integer.parseInt(allnumTxt.getText()))==0) { JOptionPane.showMessageDialog(null, "The empty project number cannot be modified!!"); } else { DealSub deal=new DealSub(); deal.submit(Integer.parseInt(allnumTxt.getText())); } } if(bt.getText().equals("Modify")) { if (alnameTxt.getText().equals("")||alpeopleTxt.getText().equals("")||alprincipalTxt.getText().equals("")||altimeStartTxt.getText().equals("")||altimeEndTxt.getText().equals("")) { JOptionPane.showMessageDialog(null, "There can't be a null value in the input!!"); } else { ScienceProject s=new ScienceProject(); s.SetNum(Integer.parseInt(allnumTxt.getText())); s.SetName(alnameTxt.getText()); s.SetPeople(alpeopleTxt.getText()); s.SetLeader(alprincipalTxt.getText()); s.SetTimeStart(altimeStartTxt.getText()); s.SetTimeFinish(altimeEndTxt.getText()); DealAlter deal=new DealAlter(); deal.alter(s); } } } private void initUpDownPaWhenAdd() { //Clear upPa.removeAll(); downPa.removeAll(); //Dynamic loading of the upper panel upPa.setLayout(null); anumLa=new JLabel(); anumLa.setText("Please enter the item number to be added"); anumLa.setLocation(30, 50); anumLa.setSize(150, 40); anumTxt=new JTextField(); anumTxt.setLocation(200, 50); anumTxt.setSize(250, 30); anameLa=new JLabel(); anameLa.setText("Please enter the project name to be added"); anameLa.setLocation(30, 100); anameLa.setSize(150, 40); anameTxt=new JTextField(); anameTxt.setLocation(200, 100); anameTxt.setSize(250, 30); apeopleLa=new JLabel(); apeopleLa.setText("Please enter the project participant"); apeopleLa.setLocation(30, 150); apeopleLa.setSize(150, 40); apeopleTxt=new JTextField(); apeopleTxt.setLocation(200, 150); apeopleTxt.setSize(250, 30); aprincipalLa=new JLabel(); aprincipalLa.setText("Please enter the project leader"); aprincipalLa.setLocation(30, 200); aprincipalLa.setSize(150, 40); aprincipalTxt=new JTextField(); aprincipalTxt.setLocation(200, 200); aprincipalTxt.setSize(250, 30); atimeStartLa=new JLabel(); atimeStartLa.setText("Please enter the project start time"); atimeStartLa.setLocation(30, 250); atimeStartLa.setSize(150, 40); atimeStartTxt=new JTextField(); atimeStartTxt.setLocation(200, 250); atimeStartTxt.setSize(250, 30); atimeEndLa=new JLabel(); atimeEndLa.setText("Please enter the project end time"); atimeEndLa.setLocation(30, 300); atimeEndLa.setSize(150, 40); atimeEndTxt=new JTextField(); atimeEndTxt.setLocation(200, 300); atimeEndTxt.setSize(250, 30); addBt=new JButton(); addBt.setText("New"); addBt.addActionListener(this); //addBt.addMouseListener(this); addBt.setLocation(250, 340); addBt.setSize(60, 30); upPa.add(addBt); upPa.add(anumLa); upPa.add(nameLa); upPa.add(apeopleLa); upPa.add(aprincipalLa); upPa.add(atimeStartLa); upPa.add(atimeEndLa); upPa.add(atimeTxt); upPa.add(anumTxt); upPa.add(apeopleTxt); upPa.add(aprincipalTxt); upPa.add(atimeStartTxt); upPa.add(atimeEndTxt); upPa.validate(); upPa.repaint(); downPa.validate(); downPa.repaint(); downPa.repaint(); } private void initUpDownPaWhenDelete() { //Clear upPa.removeAll(); downPa.removeAll(); // Dynamically load the upper panel's control upPa.setLayout(null); numLa=new JLabel(); numLa.setText("Please enter the item number to be deleted"); numLa.setLocation(40, 60); numLa.setSize(150, 40); dnumTxt=new JTextField(); dnumTxt.setLocation(200, 60); dnumTxt.setSize(250, 30); dnumBt=new JButton(); dnumBt.setText("Delete"); dnumBt.addActionListener(this); dnumBt.setLocation(270, 170); dnumBt.setSize(60, 30); upPa.add(numLa); upPa.add(dnumTxt); upPa.add(dnumBt); upPa.validate(); upPa.repaint(); downPa.validate(); downPa.repaint(); } private void initUpDownPaWhenAlert() { //Clear upPa.removeAll() on the upper and lower panels; downPa.removeAll(); // Dynamically load the control of the upper panel upPa.setLayout(null); alnumLa=new JLabel(); alnumLa.setText("Please enter the project number to be modified"); alnumLa.setLocation(40, 60); alnumLa.setSize(150, 40); alseBt=new JButton(); alseBt.setText("Submit"); alseBt.addActionListener(this); alseBt.setLocation(270, 170); alseBt.setSize(60, 30); allnumTxt=new JTextField(); allnumTxt.setLocation(200, 60); allnumTxt.setSize(200, 30); upPa.add(alnumLa); upPa.add(alseBt); upPa.add(allnumTxt); upPa.validate(); upPa.repaint(); alBt=new JButton(); alBt.setText("Modify"); alBt.addActionListener(this); alBt.setLocation(450, 170); alBt.setSize(60, 30); /* allnumLa=new JLabel(); allnumLa.setText("Please enter the modified project number"); allnumLa.setLocation(30, 00); allnumLa.setSize(150, 40); alnumTxt=new JTextField(); alnumTxt.setLocation(180, 00); alnumTxt.setSize(250, 30);*/ alnameLa=new JLabel(); alnameLa.setText("Please enter the modified project name"); alnameLa.setLocation(30, 50); alnameLa.setSize(150, 40); alnameTxt=new JTextField(); alnameTxt.setLocation(180, 50); alnameTxt.setSize(250, 30); alpeopleLa=new JLabel(); alpeopleLa.setText("Please reset participant"); alpeopleLa.setLocation(30, 100); alpeopleLa.setSize(150, 40); alpeopleTxt=new JTextField(); alpeopleTxt.setLocation(180, 100); alpeopleTxt.setSize(250, 30); alprincipalLa=new JLabel(); alprincipalLa.setText("Please reset the project leader"); alprincipalLa.setLocation(30, 150); alprincipalLa.setSize(150, 40); alprincipalTxt=new JTextField(); alprincipalTxt.setLocation(180, 150); alprincipalTxt.setSize(250, 30); altimeStartLa=new JLabel(); altimeStartLa.setText("Please reset the project start time"); altimeStartLa.setLocation(30, 200); altimeStartLa.setSize(150, 40); altimeStartTxt=new JTextField(); altimeStartTxt.setLocation(180, 200); altimeStartTxt.setSize(250, 30); altimeEndLa=new JLabel(); altimeEndLa.setText("Please reset the project end time"); altimeEndLa.setLocation(30, 250); altimeEndLa.setSize(150, 40); altimeEndTxt=new JTextField(); altimeEndTxt.setLocation(180, 250); altimeEndTxt.setSize(250, 30); downPa.add(alBt); //downPa.add(allnumLa); downPa.add(alnameLa); downPa.add(alpeopleLa); downPa.add(alprincipalLa); downPa.add(altimeStartLa); downPa.add(altimeEndLa); downPa.add(altimeTxt); //downPa.add(alnumTxt); downPa.add(alpeopleTxt); downPa.add(alprincipalTxt); downPa.add(altimeStartTxt); downPa.add(altimeEndTxt); downPa.setLayout(null); downPa.validate(); downPa.repaint(); }}3.3 Organize SQL statements and link database parts
package Scientific research information management system; import java.sql.*;import java.util.Scanner;public class Sql { public void AddScienceProject(ScienceProject scienceProject) { try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String conStr="jdbc:oracle:thin:@localhost:1521:XE"; Connection con=DriverManager.getConnection(conStr,"system","1"); StringBuffer sql=new StringBuffer("insert into science values("+scienceProject.GetNum()+",'"+scienceProject.GetName()+"','"+scienceProject.GetPeople()+"','"+scienceProject.GetLeader()+"','"+scienceProject.GetTimeStart()+"','"+scienceProject.GetTimeFinish()+"')"); Statement st=con.createStatement(); st.execute(sql.toString()); st.close(); con.close(); } catch (Exception e) { // TODO: handle exception System.out.println(e.toString()); } } public void DelScienceProject(ScienceProject scienceProject) { try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String conStr="jdbc:oracle:thin:@localhost:1521:XE"; Connection con=DriverManager.getConnection(conStr,"system","1"); StringBuffer sql=new StringBuffer("delete from science where num="+scienceProject.GetNum()+""); Statement st=con.createStatement(); st.execute(sql.toString()); st.close(); con.close(); } catch (Exception e) { // TODO: handle exception System.out.println(e.toString()); } } public void UpdScienceProject(ScienceProject scienceProject) { try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String conStr="jdbc:oracle:thin:@localhost:1521:XE"; Connection con=DriverManager.getConnection(conStr,"system","1"); StringBuffer sql =new StringBuffer("update science set name='"+scienceProject.GetName()+"',workpeople='"+scienceProject.GetPeople()+"',manager='"+scienceProject.GetLeader()+"',timestart='"+scienceProject.GetTimeStart()+"',timeend='"+scienceProject.GetTimeFinish()+"' where num="+scienceProject.GetNum()+""); Statement st=con.createStatement(); st.execute(sql.toString()); st.close(); con.close(); } catch (Exception e) { // TODO: handle exception System.out.println("Modify exception"); System.out.println(e.toString()); } } public ScienceProject Findbynum(int num) { ScienceProject scienceProject=new ScienceProject(); try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String conStr="jdbc:oracle:thin:@localhost:1521:XE"; Connection con=DriverManager.getConnection(conStr,"system","1"); StringBuffer sql=new StringBuffer("select * from science where num="+num+""); Statement st=con.createStatement(); ResultSet rs=st.executeQuery(sql.toString()); while(rs.next()) { scienceProject.SetNum(rs.getInt(1)); scienceProject.SetName(rs.getString(2)); scienceProject.SetPeople(rs.getString(3)); scienceProject.SetLeader(rs.getString(4)); scienceProject.SetTimeStart(rs.getString(5)); scienceProject.SetTimeFinish(rs.getString(6)); } st.close(); con.close(); } catch (Exception e) { // TODO: handle exception System.out.println(e.toString()); } return scienceProject; } public ScienceProject Look() { ScienceProject scienceProject=new ScienceProject(); try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String conStr="jdbc:oracle:thin:@localhost:1521:XE"; Connection con=DriverManager.getConnection(conStr,"system","1"); StringBuffer sql=new StringBuffer("select *from science"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery(sql.toString()); while(rs.next()) { scienceProject.SetNum(rs.getInt(1)); scienceProject.SetName(rs.getString(2)); scienceProject.SetPeople(rs.getString(3)); scienceProject.SetLeader(rs.getString(4)); scienceProject.SetTimeStart(rs.getString(5)); scienceProject.SetTimeFinish(rs.getString(6)); } st.close(); con.close(); } catch (Exception ex) { // TODO: handle exception System.out.println(ex.toString()); } return scienceProject; } public static void main(String args[]) { }}3.4 Add, delete, search and modify the corresponding source code
//Troubleshoot the addition of package scientific research information management system; import javax.swing.JOptionPane;public class DealAdd { public DealAdd() {} public void add(ScienceProject s) { Sql sql=new Sql(); if(sql.Findbynum(s.GetNum()).GetNum()!=0) { JOptionPane.showMessageDialog(null, "This project already exists, please re-enter!"); } else { sql.AddScienceProject(s); JOptionPane.showMessageDialog(null,"AddSuccess"); } }} As can be seen from the design part, the project number needs to be submitted first when deleting, so the submission event needs to be processed.
//Processing the submission package scientific research information management system; import javax.swing.JOptionPane;public class DealSub{ DealSub() {} public void submit(int num) { Sql sql =new Sql(); if(sql.Findbynum(num).GetNum()==0) { JOptionPane.showMessageDialog(null, "This project does not exist"); } else { JOptionPane.showMessageDialog(null, "Please fill in the modified project information below"); } }}//Processing the deletion package scientific research information management system; import javax.swing.JOptionPane;public class DealDelete{ public DealDelete() {} public void delete(int num) { ScienceProject s=new ScienceProject(); Sql sql=new Sql(); if(sql.Findbynum(num).GetNum()==0) { JOptionPane.showMessageDialog(null, "The project does not exist~"); } else { s=sql.Findbynum(num); sql.DelScienceProject(s); JOptionPane.showMessageDialog(null, "Delete successfully~"); } }}//handle and modify package scientific research information management system; import javax.swing.JOptionPane; public class DealAlter { public DealAlter() {} public void alter(ScienceProject s) { Sql sql=new Sql(); sql.UpdScienceProject(s); JOptionPane.showMessageDialog(null,"Modify successfully"); }}//handle and query package scientific research information management system; public class DealSearch //handle and query events { public DealSearch() {} public String findProByNum(int num)//Query by project number { String result=""; ScienceProject s=new ScienceProject(); Sql sql=new Sql(); s=sql.Findbynum(num); result=s.GetNum()+","+s.GetName()+","+s.GetPeople()+","+s.GetLeader()+","+s.GetTimeStart()+","+s.GetTimeFinish(); return result; }}3.5 Main function call login window
package scientific research information management system; public class Test { public static void main(String[] args) { LoginWindows v=new LoginWindows(); }}4. Summary
There are still some defects. Due to unfamiliarity with scientific research projects, some attributes are unreasonable, and there are many things that have not been considered. In addition, unfamiliarity with GUI also limits our design on the window. It also needs to be modified in terms of project properties and windows.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.