The realization of the interface of the student information system-JDBCWRITER:PPRP login interface: Divided into two parts: 1, Loginframe.java:
Using Windowbuilder to set up the interface quickly, constructs the login interface, and constructs the object with the Logconnection class, carries on the processing, increases the listener.
2, Logconnection.java:
The basic JDBC steps are used to deal with the problem of determining user name and password matching.
Note the issue:
1, only after the ResultSet object is used to shut down the connection object, otherwise it will affect the transmission of data, and finally close the connection.
2, in addition to note in the use of ResultSet results query, you need to add a judgment, if (!rs.next ()) return;
3. Note that the close function should be written in logconnection and called Close in the Loginframe listener.
Loginframe.java
Package Work2;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.JButton; Import Javax.swing.jframe;import javax.swing.jlabel;import javax.swing.joptionpane;import Javax.swing.JPanel; Import Javax.swing.jpasswordfield;import Javax.swing.jtextfield;import Javax.swing.uimanager;import Javax.swing.border.emptyborder;import Work3. Studentframe; @SuppressWarnings ("Serial") public class Loginframe extends JFrame {private JPanel contentpane; Private JTextField userfiled; Private JPasswordField pwd; Private logconnection LC = NULL; public static void Main (string[] args) {try {Uimanager. Setlookandfeel ("Com.sun.jav A.swing.plaf.windows.windowslookandfeel "); } catch (Exception e) {e.printstacktrace (); } loginframe frame = new Loginframe (); Frame.setvisible (TRUE); } public Loginframe () {settitle ("Student Information Management system"); LC = new Logconnection (); SetdefaulTcloseoperation (Jframe.exit_on_close); SetBounds (100, 100, 450, 300); ContentPane = new JPanel (); Contentpane.setborder (New Emptyborder (5, 5, 5, 5)); Setcontentpane (ContentPane); Contentpane.setlayout (NULL); JLabel Lblnewlabel = new JLabel ("\u7528\u6237\u540d\uff1a"); Lblnewlabel.setbounds (63, 68, 54, 15); Contentpane.add (Lblnewlabel); JLabel lblnewlabel_1 = new JLabel ("\u5bc6 \u7801\uff1a"); Lblnewlabel_1.setbounds (63, 128, 54, 15); Contentpane.add (Lblnewlabel_1); userfiled = new JTextField (); Userfiled.setbounds (127, 65, 224, 23); Contentpane.add (userfiled);//Userfiled.setcolumns (10); PWD = new JPasswordField (); Pwd.setbounds (127, 124, 224, 23); Contentpane.add (PWD); JButton loginbtn = new JButton ("\u786e\u5b9a"); Loginbtn.setbounds (63, 204, 93, 23); Contentpane.add (LOGINBTN); JButton cancelbtn = new JButton ("\u53d6\u6d88");Cancelbtn.setbounds (268, 204, 93, 23); Contentpane.add (CANCELBTN); Loginbtn.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent ARG0) {//TODO auto-generated method stub String name = Userfiled.gettext (). Trim (); String Pwdtext = string.valueof (Pwd.getpassword ()). Trim (); if (LC. Judge (name, Pwdtext)) {Joptionpane.showmessagedialog (Loginframe.this, "Welcome," + name); @SuppressWarnings ("unused") studentframe tmp = new Studentframe (); Tmp.setvisible (TRUE); } else {Joptionpane.showmessagedialog (loginframe.this, "User name or password error"); } } }); Cancelbtn.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent ARG0) {//TODO auto-generated Method Stub system.exit (0); Lc.close (); } }); Joptionpane.showmessagedialog (Loginframe.this, "Welcome to the Student Information Management system, please enter the account password"); }}
Logconnection.java
Package Work2;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.resultset;import Java.sql.sqlexception;import Java.sql.statement;public class Logconnection {Connection con = null; Statement sql = null; Public logconnection () {//1 drive try {class.forname ("com.mysql.jdbc.Driver"); SYSTEM.OUT.PRINTLN ("Driver loading succeeded"); } catch (ClassNotFoundException e) {System.out.println ("Driver load Failed"); E.printstacktrace (); }//2 connect String url = "Jdbc:mysql://localhost/test?usessl=true"; String user = "root"; String password = "root"; try {con = drivermanager.getconnection (url, user, password); System.out.println ("link succeeded"); } catch (SQLException e) {System.out.println ("link failed"); E.printstacktrace (); }//3 Statement object try {sql = con.createstatement (); System.out.println ("Successfully generated statement object "); } catch (SQLException e) {System.out.println ("failed to generate statement object"); E.printstacktrace (); }} public boolean Judge (string name, String passwd) {ResultSet rs = null; String str = "SELECT * from log where logname = '" + name + "'"; try {rs = sql.executequery (str); if (!rs.next ()) return false; } catch (SQLException e) {e.printstacktrace (); return false; } try {String cmppwd = rs.getstring (3); if (Cmppwd.equals (passwd)) return true; } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } return false; } public void Close () {try {con.close (); System.out.println ("close success"); } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); System.out.println ("Shutdown failed"); } }}
Implementation of the query interface: requirements:
Design Student information Management system.
Using the Navicat test database, create a student table that contains student number, name, and age information. Write the appropriate function according to the following functions:
① according to the number, you can check the name and age of students;
② the number, name and age of a given student, add a line of information to the table;
③ a given student's number, you can delete the student's information from the table;
The design of student information management system using graphical interface for task three.
Analysis:
Use the login interface of the previous section, in the login interface of the ActionListener processing, if successful login, then open the query, update, delete interface to operate.
Student.java: This is about JDBC programming content, designed for three requirements of three functions, respectively, the implementation of three functional requirements.
Studentframe.java: interface design, for four button registration listener, integrated implementation of three functions.
Knowledge Points:
1, the use of the SQL statement is more, pay attention to the use of the time need to add double quotation marks when appropriate, for example:
"INSERT into Student (sname,age)
VALUES (' "+name+" ', "+age+") "
Where both name and age are variables.
2. Note that the number in the database is starting from 1, not starting from 0.
3, executeupdate statement: is to perform additions and deletions of the operation.
ExecuteQuery statement: Performs a query operation.
Loginframe.java
Package Work2;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.JButton; Import Javax.swing.jframe;import javax.swing.jlabel;import javax.swing.joptionpane;import Javax.swing.JPanel; Import Javax.swing.jpasswordfield;import Javax.swing.jtextfield;import Javax.swing.uimanager;import Javax.swing.border.emptyborder;import Work3. Studentframe; @SuppressWarnings ("Serial") public class Loginframe extends JFrame {private JPanel contentpane; Private JTextField userfiled; Private JPasswordField pwd; Private logconnection LC = NULL; public static void Main (string[] args) {try {Uimanager. Setlookandfeel ("Com.sun.jav A.swing.plaf.windows.windowslookandfeel "); } catch (Exception e) {e.printstacktrace (); } loginframe frame = new Loginframe (); Frame.setvisible (TRUE); } public Loginframe () {settitle ("Student Information Management system"); LC = new Logconnection (); SetdefaulTcloseoperation (Jframe.exit_on_close); SetBounds (100, 100, 450, 300); ContentPane = new JPanel (); Contentpane.setborder (New Emptyborder (5, 5, 5, 5)); Setcontentpane (ContentPane); Contentpane.setlayout (NULL); JLabel Lblnewlabel = new JLabel ("\u7528\u6237\u540d\uff1a"); Lblnewlabel.setbounds (63, 68, 54, 15); Contentpane.add (Lblnewlabel); JLabel lblnewlabel_1 = new JLabel ("\u5bc6 \u7801\uff1a"); Lblnewlabel_1.setbounds (63, 128, 54, 15); Contentpane.add (Lblnewlabel_1); userfiled = new JTextField (); Userfiled.setbounds (127, 65, 224, 23); Contentpane.add (userfiled);//Userfiled.setcolumns (10); PWD = new JPasswordField (); Pwd.setbounds (127, 124, 224, 23); Contentpane.add (PWD); JButton loginbtn = new JButton ("\u786e\u5b9a"); Loginbtn.setbounds (63, 204, 93, 23); Contentpane.add (LOGINBTN); JButton cancelbtn = new JButton ("\u53d6\u6d88");Cancelbtn.setbounds (268, 204, 93, 23); Contentpane.add (CANCELBTN); Loginbtn.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent ARG0) {//TODO auto-generated method stub String name = Userfiled.gettext (). Trim (); String Pwdtext = string.valueof (Pwd.getpassword ()). Trim (); if (LC. Judge (name, Pwdtext)) {Joptionpane.showmessagedialog (Loginframe.this, "Welcome," + name); @SuppressWarnings ("unused") studentframe tmp = new Studentframe (); Tmp.setvisible (TRUE); } else {Joptionpane.showmessagedialog (loginframe.this, "User name or password error"); } } }); Cancelbtn.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent ARG0) {//TODO auto-generated Method Stub system.exit (0); Lc.close (); } }); Joptionpane.showmessagedialog (Loginframe.this, "Welcome to the Student Information Management system, please enter the account password"); }}
Logconnection.java
Package Work2;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.resultset;import Java.sql.sqlexception;import Java.sql.statement;public class Logconnection {Connection con = null; Statement sql = null; Public logconnection () {//1 drive try {class.forname ("com.mysql.jdbc.Driver"); SYSTEM.OUT.PRINTLN ("Driver loading succeeded"); } catch (ClassNotFoundException e) {System.out.println ("Driver load Failed"); E.printstacktrace (); }//2 connect String URL = "Jdbc:mysql://localhost/test?use analysis: Ssl=true"; String user = "root"; String password = "root"; try {con = drivermanager.getconnection (url, user, password); System.out.println ("link succeeded"); } catch (SQLException e) {System.out.println ("link failed"); E.printstacktrace (); }//3 Statement object try {sql = con.createstatement (); System.out.printlN ("Successfully generated statement object"); } catch (SQLException e) {System.out.println ("failed to generate statement object"); E.printstacktrace (); }} public boolean Judge (string name, String passwd) {ResultSet rs = null; String str = "SELECT * from log where logname = '" + name + "'"; try {rs = sql.executequery (str); if (!rs.next ()) return false; } catch (SQLException e) {e.printstacktrace (); return false; } try {String cmppwd = rs.getstring (3); if (Cmppwd.equals (passwd)) return true; } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); } return false; } public void Close () {try {con.close (); System.out.println ("close success"); } catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); System.out.println ("Shutdown failed"); } }}
If it helps, please feel good.
Nwafu-java Practice JDBC Practice-Student Information System interface