Database +java course Design Personnel Management System (i)

Source: Internet
Author: User
Tags decrypt stmt

I. Cooperation between Java and the database

The development language is Java, the database driver used is Mysql-connector-java-5.1.8-bin.jar

The first step is to connect the MySQL database with Java (driver download: https://dev.mysql.com/downloads/connector/j/)

Copy the downloaded Mysql-connector-java-5.1.8-bin.jar to the native downloaded Java folder (the author is C:\Program Files (x86) \java\jdk1.7.0_07)

Then in Eclipse, the mouse selected personnel management Systems Engineering, right click on the build path, select Configure build path, will jump out of a property block diagram. Select Libraries under Java Build Path to see if there is a mysql-connector-java-5.1.8-bin.jar.

If not, click Add External JARs, browse to the JDBC MySQL driver jar package, click OK, and import it into the project.

The second step is to run a test code under the project to detect if the database is linked successfully

1  Public  classTest () {2          Public Static voidMain (String []args) {3 Try {4             //MySQL Database setup driver type5Class.forName ("Com.mysql.jdbc.Driver"); 6SYSTEM.OUT.PRINTLN ("MySQL database driver loading succeeded");7             8             //SQL Server Database setup driver type9             //class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");Ten             //System.out.println ("SQL Server database driver loading succeeded"); One  A         } -         Catch(java.lang.ClassNotFoundException e) { - e.printstacktrace (); the         } -     } -}

The third step: After the driver link succeeds, it is possible to establish a basic table for operation in the database.

Personnel Management System Main Table is (Employee Information table and personnel change form)

Basic operations on tables include querying and updating data in tables with SQL statements (add and remove)

First create a Connection object and then use the SQL statement to manipulate the database code as follows

Package exercise3;
Import java.sql.*;
Import java.util.ArrayList;
Import java.util.List;


public class dbprocess{


StaticConnection Connection =NULL;//linked Objects StaticResultSet rs=NULL;//Post-operation result set//MySQL Database URL StaticString usermysql= "root";//Database user name StaticString passwordmysql= "a123456";//Password StaticString urlmysql = "jdbc:mysql://localhost:3306/staffdb?user=" +usermysql+ "&password=" +passwordmysql+ "& USEUNICODE=TRUE&CHARACTERENCODING=GBK ";//linking the database to Java PublicDbprocess () {dbprocess//constructorTry { //MySQL Database setup driver typeClass.forName ("Com.mysql.jdbc.Driver"); System.out.println ("MySQL Database driver loading succeeded"); } Catch(java.lang.ClassNotFoundException e) {e.printstacktrace (); } } Public Static voidConnect () {//Connection Try{ //MySQL DatabaseConnection =drivermanager.getconnection (Urlmysql); if(connection!=NULL) {System.out.println ("Database connection succeeded"); } } Catch(Exception e) {e.printstacktrace (); } } Public Static voidDisconnect () {//Disconnect Connection Try{ if(Connection! =NULL) {connection.close (); Connection=NULL; } } Catch(Exception e) {e.printstacktrace (); } } Public StaticResultSet executeQuery (String sql) {//Putthe query SQL statement is routed to the database and executes and then returns the result set of the query Try{System.out.println ("ExecuteQuery (). sql = "+SQL); PreparedStatement pstm= connection.preparestatement (SQL);//PreparedStatement: The object used to execute the SQL statement//get using the connection PreparedStatement (SQL) methodRS=Pstm.executequery (); //ResultSet Rw=rs; //While (Rs.next ()) {//System.out.println (rs.getstring ("PNo")); // } } Catch(SQLException ex) {ex.printstacktrace (); } returnrs; } //Insert//The return value of Executeupdate is an integer that indicates the number of rows affected (that is, the update count). //executeupdate used to execute INSERT, UPDATE, or DELETE statements//and SQL DDL (data definition language) statements, such as CREATE table and DROP table. //methods of executing the increment, delete and change statements Public Static intexecuteupdate (String sql) { //Send update SQL statement to database execution intCount = 0; Connect (); Try{Statement stmt=connection.createstatement (); Count=stmt.executeupdate (SQL); } Catch(SQLException ex) {System.err.println (Ex.getmessage ()); } disconnect (); returncount; }}

such as querying the personal information of all employees

1         Try{2             //Establish query criteria3String sql = "SELECT * from person;";4System.out.println ("Queryallprocess (). sql = "+sql);5     6                 //convert the recorded data obtained by the query into the form of data that is suitable for generating jtable7 Dbprocess.connect ();8             9Dbprocess.executequery (SQL);//result set ResultSetTen          One          A              -             //convert the recorded data obtained by the query into the form of data that is suitable for generating jtable -Staffvector.clear ();//This bar is a UI part that can be used regardless of the              while(DbProcess.rs.next ()) { -Vector v =NewVector (); -System.out.println (DbProcess.rs.getString ("PName"));//Test Statement: Name of the output query -V.add (DbProcess.rs.getString ("PId")); +V.add (DbProcess.rs.getString ("PName")); -V.add (DbProcess.rs.getString ("Psex")); +V.add (Md5.decrypt (DbProcess.rs.getString ("ppasswd")));  A                  //Md5.decrypt () for my database cipher algorithm atV.add (DbProcess.rs.getString ("Pauthority")); -V.add (DbProcess.rs.getString ("Pdepartment")); -V.add (DbProcess.rs.getString ("Pjob")); -V.add (DbProcess.rs.getString ("Pedulevel")); -V.add (DbProcess.rs.getString ("Pspcialty")); -V.add (DbProcess.rs.getString ("Pbirthday")); inV.add (DbProcess.rs.getString ("Paddress")); -V.add (DbProcess.rs.getString ("Ptel")); toV.add (DbProcess.rs.getString ("Pemail")); +V.add (DbProcess.rs.getString ("PState")); -V.add (DbProcess.rs.getString ("Premark")); the                  * Staffvector.add (v); The query result set has been converted to Vector V, and V is added to my GUI for display $             }Panax NotoginsengStaffjtable.updateui ();//This article updates the UI, which can be used at this time regardless - Dbprocess.disconnect (); the}Catch(SQLException sqle) { +System.out.println ("Sqle =" +Sqle); AJoptionpane.showmessagedialog (NULL, the"Data manipulation Error", "Error", joptionpane.error_message); +}Catch(Exception e) { -System.out.println ("E =" +e); $Joptionpane.showmessagedialog (NULL, $"Data manipulation Error", "Error", joptionpane.error_message); -}

Finally preview a wave, I this course design to make the final work

User mode:

Administrator mode:

( If you have a good opinion welcome timely, if you do not reply in time can mail [email protected])

Database +java course Design Personnel Management System (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.