Use the Jfinal framework to connect to the database for registration and login functions

Source: Internet
Author: User

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

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.