Recently, I got a private activity and a warehouse receiving and picking system. The development cycle is from 30 days to 60 days.
The final decision of the entire project is implemented using the java client + mysql server. Therefore, some necessary basic processes in the project development process will be written down for your reference in the future.
After java and mysql are installed separately, install the java jdbc driver package, and the development preparation is almost the same. The jdbc package is a jar file, copied to its own java directory, and can be imported. The jdbc package is downloaded here.
The code is as follows: |
Copy code |
Class myData { String driver = "com. mysql. jdbc. Driver "; String url = "jdbc: mysql: // 127.0.0.1: 3306/test "; String user = "root "; String password = "root "; Connection conn = null; ...... If (link_ob = 0 ){ // The code written here after the database connection is successful. } Private int link_ob (){ Try { Class. forName (driver ); Conn = DriverManager. getConnection (url, user, password ); } // Catch exceptions in the driver loading Catch (ClassNotFoundException msg1 ){ System. err. println ("failed to load the JDBC/ODBC driver. "); Msg1.printStackTrace (); JOptionPane. showMessageDialog (null, "failed to load JDBC/ODBC driver. "," Failed to load the JDBC/ODBC driver. ", JOptionPane. INFORMATION_MESSAGE ); Return 1; } // Catch database connection exceptions Catch (SQLException sqlex ){ System. err. println ("unable to connect to database "); Sqlex. printStackTrace (); JOptionPane. showMessageDialog (null, "cannot connect to the database. "," Cannot connect to the database. ", JOptionPane. INFORMATION_MESSAGE ); Return 2; } Return 0; } |
Java reads the database username and password and returns the value.
The following two methods can be used to implement the response action of the button,
The code is as follows: |
Copy code |
JButton1.addActionListener (new ActionListener (){ Public void actionreceivmed (ActionEvent e) { Login (); } } ); Or // This method requires setting behavior objects in the panel. @ Action Login ();
The login () function reads the database and compares the data. If 0 is returned, the login () function logs on to the database successfully. The specific code is as follows: MyData mydb = new myData (); Int result = mydb. opendb (1, name, pwd ,"",""); If (result = 1 ){ Setstatus ("failed to load JDBC/ODBC driver. "); Return; } If (result = 2 ){ Setstatus ("unable to connect to the database. "); Return; } If (result = 3 ){ Setstatus ("user verification failed. "); Return; } Else if (result = 5 ){ Setstatus ("This user is locked. "); Return; } Else if (result = 6 ){ Setstatus ("This user is not available. "); Return; } Else { Setstatus ("user" + jTextField1.getText () + "login successful. "); Username = jTextField1.getText (); JTextField1.setText (""); JPasswordField1.setText (""); ShowMainBox (); Return; } |
After successful logon, execute the showMainBox () operation to display the main page of the program.