Create the LYYP project and add the MySQL jar package to the project.
Mysql-connector-java-5.1.7-bin.jar this package into the project Webcontent-web-inf-lib, right-click the project to select Build Path-java build Path-libraries,add JARs ... Locate the jar package that was introduced in the project in the previous step, OK. Java resources-src Create dbconfig.properties file code:
Driver=com.mysql.jdbc.Driverdburl=jdbc:mysql://localhost:3306 /lyyp_dbuser=rootpassword=******
Java RESOURCES-SRC Create system Generic Tool class package Name: Utilname "class name": ConnectionFactory code:
//Authors/buddha Packageutil;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.util.Properties; Public classConnectionFactory {Private StaticString driver;//Database Load Driver Private StaticString Dburl;//Link Address Private StaticString user;//User name Private StaticString password;//Password//private static Connection factory Private Static FinalConnectionFactory factory =NewConnectionFactory (); PrivateConnection Conn;//Connection//read code for configuration information Static{Properties prop=NewProperties ();//defining classes, handling key-value pairs Try { //get configuration File "Dbconfig.properties"//gets the contents of the property file, first gets the class loader, and then reads the contents of the properties file, reading the list of key-value pairs from the input stream//input stream = Connection factory. Class. Gets the class loader. Get the resource as a stream (resource name)InputStream in = ConnectionFactory.class. getClassLoader (). getResourceAsStream ("Dbconfig.properties"); Prop.load (in); } Catch(Exception e) {System.out.println ("= = = Config file read error = = ="); } Driver= Prop.getproperty ("Driver"); Dburl= Prop.getproperty ("Dburl"); User= Prop.getproperty ("User"); Password= Prop.getproperty ("Password"); } //the default constructor Privateconnectionfactory () {} Public Staticconnectionfactory getinstance () {returnFactory; } //get the connection method for a database PublicConnection makeconnection () {Try{class.forname ("Driver"); Conn=drivermanager.getconnection (Dburl,user,password); } Catch(Exception e) {e.printstacktrace (); } returnConn; }}
Java RESOURCES-SRC Create Entity abstract class Package Name: EntityName "Class name": Identity code:
//Authors/buddha PackageEntity//Entity class Package//Abstract class Public Abstract classIdEntity {protectedLong ID; PublicLong getId () {returnID; } Public voidsetId (Long id) { This. ID =ID; }}
Create User class Name "class name" under the entity package: Userbrowse ... After entering the identity selection, declaring the field, Source-generate Getters Setters ... "Create Get,set method" Source-generte toString () ... "" Except for the original tick, add Inherited Fields-id check. Code:
//Authors/buddha PackageEntity//Entity class Package//user inherits from identity abstract entity class Public classUserextendsIdEntity {PrivateString uname; PrivateString Upassword; //Get Set Method PublicString Getuname () {returnuname; } Public voidsetuname (String uname) { This. uname =uname; } PublicString Getupassword () {returnUpassword; } Public voidSetupassword (String upassword) { This. Upassword =Upassword; } @Override PublicString toString () {return"User [uname=" + uname + ", upassword=" + Upassword + ", id=" + ID + "]"; }}
Java RESOURCES-SRC Create Interface package name:d aoname "interface name": Userdao code:
//Authors/buddha PackageDao//Interface PackageImportjava.sql.Connection;Importjava.sql.SQLException;Importentity. User;//user Interface Public InterfaceUserdao {//Save Public voidSave (Connection conn, user user)throwsSQLException; //Update Public voidUpdate (Connection conn, Long ID, user user)throwsSQLException; //Delete Public voidDelete (Connection conn, user user)throwsSQLException;}
The DAO package creates an interface to implement the class package "package name":d Ao.impl "interface specific implementation Class" Name "Class name": Userdaoimpladd "Add" Userdao Interface code:
//Authors/buddha PackageDao.impl;//Package Name "interface-specific implementation class package"Importjava.sql.Connection;Importjava.sql.SQLException;ImportJava.sql.PreparedStatement;//the package introduced here takes note of changesImportDAO. Userdao;Importentity. User;//interface-specific implementation class Public classUserdaoimplImplementsUserdao {//Save user Information@Override Public voidSave (Connection conn, user user)throwsSQLException {preparedstatement ps= Conn.preparecall ("INSERT into Lyyp_common_user (Uname,upassword) VALUES (?,?)"); Ps.setstring (1, User.getuname ()); Ps.setstring (2, User.getupassword ()); Ps.execute (); } //Update user information based on user-specified ID@Override Public voidUpdate (Connection conn, Long ID, user user)throwsSQLException {String updatesql= "UPDATE lyyp_common_user SET UName =?, Upassword =?" WHERE id =? "; PreparedStatement PS=conn.preparestatement (Updatesql); Ps.setstring (1, User.getuname ()); Ps.setstring (2, User.getupassword ()); Ps.setlong (3, id); Ps.execute (); } //Delete the specified user information@Override Public voidDelete (Connection conn, user user)throwsSQLException {preparedstatement ps= Conn.preparestatement ("DELETE from lyyp_common_user WHERE id =?")); Ps.setlong (1, User.getid ()); Ps.execute (); }}
Development process Record (waste)