Use the Jfinal framework to connect to the database for registration and login functions
1. Create new dynamic Web Project projects in Eclipse
2, Import Jfinal-2.2-bin-with-src.jar, C3p0-0.9.5.1.jar, Mchange-commons-java-0.2.10.jar, Mysql-connector-java-5.1.20-bin.jar Bag
3. Modify Web. XML
4, new Com.common package in src directory, new Mainconfig.java in package, inherit Jfinalconfig
5. Create a new user folder in the Webroot directory, create a new login.jsp, register.jsp in the user folder
login.jsp
register.jsp
In the Configconstant method in Mainconfig.java, add:
Me.setviewtype (viewtype.jsp);
In the Configroute method in Mainconfig.java, add:
me.add ("/user", usercontroller.class);
6. Open the MySQL database visualizer and create a new Customer table in the database
7. Connect to MySQL database using C3P0 data source
1) Create a new config.properties text (file) file in the SRC directory
Jdbcurl = jdbc:mysql://localhost:3306/database name
user = root
Password = root
2) in the Configconstant method in Mainconfig.java, add:
Propkit.use ("Config.properties");
In the Configplugin method, add:
C3p0plugin C3p0plugin = Newc3p0plugin (Propkit.get ("Jdbcurl"), Propkit.get ("user"), Propkit.get ("password"));
Activerecordplugin arp = new Activerecordplugin (c3p0plugin);
Arp.setshowsql (TRUE);
Arp.addmapping ("Customer", user.class);
Me.add (C3p0plugin);
Me.add (ARP);
3) Create a new Com.model package, create a new User.java in the package, and add it in User.java:
public static final User DAO = new user ();
8, new Com.controller package, new Usercontroller.java in the package, inherit controller, add code:
public void Register () {
Render ("register.jsp");
}
Public Voidlogin () {
Render ("login.jsp");
}
Public Voidsubmit () {
String name= Getpara ("username");
Stringpassword = Getpara ("password");
List Users =user.dao.find ("SELECT * FROM customer WHERE User = '" +name+ "' Andpassword = '" +password+ "'");
SetAttr ("Users", users);
if (users.size () > 0) {
SYSTEM.OUT.PRINTLN ("Login Successful");
}else{
SYSTEM.OUT.PRINTLN ("Login Failed");
}
Login ();
}
Public Voidadd () {
User User =getmodel (user.class, "user");
User.save ();
SYSTEM.OUT.PRINTLN ("registered success");
Login ();
}
9. Test Run
Use the Jfinal framework to connect to the database for registration and login functions