I decided to use IDEA to write Java programs. At first, I thought I would use file system to manage data. I thought the operation of file system would be easier to start compared with the operation of database. But after reading the instructions, I found that I had to use a database, which can also act as a server for storing data. So that’s going to correspond to the problem set. The database is chosen to do the class Settings are used in MySQL, MySQL itself is relatively easy to install, I found online tutorials to install it.
After installing the database, of course, the first step is to create a new project, but we need to connect the database this time, so we need to connect IDEA with the database. This step took me some time, because AFTER I connected the database with IDEA, I found that I could not perform the operation of adding, deleting, checking and changing the database, so I checked some data. I am roughly sure that I did not import the JAR package of Connect J. I need to download the JAR package of Connect J from the official website and import the JAR package into IDEA. Following the method found on the Internet, IMPORT the JAR package into IDEA and try again to see if I can add, delete, check and modify the established table. So far, the most basic set of course is the core of the database of the operation can determine how to write.
When the class starts, the first class will write the right class for database operations.
Mysql > insert into studentInfo1; mysql > insert into studentInfo1; mysql > insert into studentInfo1; Insert into studentinfo1 values (xx, xx, xx,…..) If you just insert one row of data, you can write the data you want to add to the value behind the parentheses, but we need many different rows of data, so we wrap this insert function into a function. When we insert, we call the function, pass different arguments to the function, and insert multiple different rows of data. Because I’m building studentInfo1, I’m using (name char (10),no int, Python int, Java int,db int), so I need to insert a value of that type. So the wrapper function is to set the format of the formal parameters to String A,int B,int C,int D,int E, and insert these parameters into the position of the placeholder in order, we have completed the most important part of the insert function, so that when the function is called, we only need to pass in different parameters, we can insert different data. Finally, upload the SQL statement to the database.
The second thing to write is to delete the data, this is also the same reason, of course, I am based on the student id delete information, so only set an int parameter on the line.
1 2 3 4 5 6 7 8 9 10
publicvoiddelete(int b)throws Exception{ PreparedStatementps=null; Class.forName("com.mysql.jdbc.Driver"); Stringjdbc="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8"; Connectionconn= DriverManager.getConnection(jdbc, "root", "root"); Stringsql="delete from studentinfo1 where no = ?"; ps = conn.prepareStatement(sql); ps.setInt(1,b); intinsertCount= ps.executeUpdate(); conn.close();}
The third to write is the query data, again the same reason, of course, I am still based on the student number query information, so parameter only set an int on the line. But before the data query and the difference is the information that we need to return the query, so you need to by the return value, we need to put the query after setting a good container, at the same time I hope on the basis of student id, can return to give me a name, three grades of data types, and get String function to get the value of the database are type String, So it’s just a good idea to put these values in an array, just do it, go through all the rows in the database, and when the student number is equal to the parameter that you passed in, put that row in the array, and then the query is done.
Finally, to complete the last modification operation, which I still change the value of the data according to the student number, my first thought is to set the placeholder to “set? =? Where no =?” After the actual operation, the modification operation cannot be realized. After checking the data, I do not know why it cannot be operated, but WHEN I was debugging before, I used “set name =? Where no =?””Set python =? Where no =?” I then changed the idea to use an if statement, as shown below
publicvoidupdate(String a,int b,int c)throws Exception{ PreparedStatementps=null; Class.forName("com.mysql.jdbc.Driver"); Stringjdbc="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8"; Connectionconn= DriverManager.getConnection(jdbc, "root", "root"); if (a.equals("python")) { Stringsql="update studentinfo1 set python = ? where no = ? "; ps = conn.prepareStatement(sql); ps.setInt(1, b); ps.setInt(2, c); intinsertCount= ps.executeUpdate(); } elseif(a.equals("java")) { Stringsql="update studentinfo1 set java = ? where no = ? "; ps = conn.prepareStatement(sql); ps.setInt(1, b); ps.setInt(2, c); intinsertCount= ps.executeUpdate(); } elseif(a.equals("db")) { Stringsql="update studentinfo1 set db = ? where no = ? "; ps = conn.prepareStatement(sql); ps.setInt(1, b); ps.setInt(2, c); intinsertCount= ps.executeUpdate(); } conn.close(); }
This also indirectly implements the modification function, but requires an additional operation to change the name because of data type limitations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicvoidupdatename(String a,String b,int c)throws Exception{ PreparedStatementps=null; Class.forName("com.mysql.jdbc.Driver"); Stringjdbc="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8"; Connectionconn= DriverManager.getConnection(jdbc, "root", "root"); if (a.equals("姓名")) { Stringsql="update studentinfo1 set name = ? where no = ? "; ps = conn.prepareStatement(sql); ps.setString(1, b); ps.setInt(2, c); intinsertCount= ps.executeUpdate(); } conn.close(); }
The core of the database to add and delete check and change the operation is finished, then began to write the “front end”, first write the initial interface of each system ———— login interface.
Import our most commonly used Swing components and AWT, and the writing of the GUI begins.
Set the familiar components JTextfield, JButton, JLabel, JPasswordfield to fill it, first set the window center, and window ban maximization.
JLabell1=newJLabel("身份:"); //t1 = new JTextField(20);
JLabell2=newJLabel("姓名:"); //JTextField t2 = new JTextField(20);
JLabell3=newJLabel("密码:"); //JTextField t3 = new JTextField(20); //t3.setEchoChar('*');
JButtonb1=newJButton("确定");
Next, add event listeners for the buttons, and add event listeners for the register and login buttons. Click Register to store information in loguser. TXT. When storing loguser. TXT, I use the contents obtained from the text box in the form of “identity, username, password” to store the file line by line. Loguser. TXT file, judge whether the content of the text box is equal one by one, if you can find the same data, you can login successfully, login successfully can enter the corresponding interface, that is, instantiate classes such as student. Of course, in the design of the system, we default six login opportunities, if the login failed for six times, it will automatically exit the system.
That’s it for the login screen and functionality. All that’s left to do is add components to the panel and add background images to the window. Just find a picture on the Internet. Below is the code snippet for the entire interface and functionality. Of course, I set the login screen as the main class of the entire class, and the rest of the classes are instantiated in the main class.
Next we need to write the classes we want to instantiate student.class and teacher.class. It still inherits the JFrame interface. Using the teacher.class example, it is still necessary to add an event listener to the component. By the component to monitor whether the occurrence of events, in the corresponding corresponding operations. For the store button, all we need to do is instantiate dbopra, call insert, and store the data to the database. You can insert the data you entered in the text box by taking the text entered in the text box as an argument to the insert function. Query, delete are the same reason, just pass in different parameters.
For the modification action, I wrote an action to modify the panel, which is done by Teacherup.class. If you click Modify on the teacherup.clas interface, you can instantiate Teacherup. clas and jump out of the modify interface. To complete the modification operation. Below are the renderings.
So basically finished, the student side and the teacher side basically no difference, is to reduce the modification, storage, delete permissions, only have query function, I will not put the code, you can delete according to the teacher side.
Wrote it. The basic system is already in place. Teachers have the function of adding, deleting, checking and changing, and students have the function of querying. If you need to add some analysis functions of results, you can add them by yourself.
I’ll leave it there for now. Vegetable chicken 1, if there is a mistake, hope to correct.