A small Dao user login module-Struts and Dao

Source: Internet
Author: User

In the afternoon, I couldn't help looking at seek and searching for Java. I found a lot of work that made people drool. But I told me to hold on to it. I don't need to submit my resume for a while. I still have 40 days of training. After training, I started to invest.

After training for a few days, I felt that I had made a lot of improvement in my skills. Many of the positions I didn't invest in before now seem confident.

We hope to lock a globle company in time.

Continue with today's learning. After reading the basics of JPA, switch back to struts. In this way, you can continue Struts-> hibernate-> spring and start the project.

This is the simplest Struts framework, and the program is a small module for managing users (actually far from that simple, this is just a framework ). The database uses the simplest prototype of the user module of the sxt DRP project.

In the past, companies would not use such a simple framework, but it is suitable to describe struts applications.

The most basic DAO design

From the code point of view, it is mainly used as follows:

First, look for all users in JSP,

In struts:

Public class listuseraction extends action {
Public actionforward execute (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {
// Call the business logic operation
List userlist =Usermanager. getinstance (). findalluserlist ();
Request.Setattribute ("userlist", userlist);
Return Mapping. findforward ("success ");
}
}

Usermanager:

Public list findalluserlist (){
List userlist = new arraylist ();
Try {
Userlist =Userdao. findalluserlist ();
} Catch (exception e ){
E. printstacktrace ();
}
Return userlist;
}

In the key implementation class

Public list findalluserlist (){
StringSQL = "select * From t_user where user_id <> 'root' order by user_id ";
System. Out. println ("userdao4mysqlimpl => findalluserlist => SQL =" + SQL );
Connection conn = NULL;
Statement stmt = NULL;
Resultset rs = NULL;
List userlist = new arraylist ();
Try {
Conn = dB. getconn ();
Stmt = conn. createstatement ();
Rs = stmt.exe cutequery (SQL );
While (Rs. Next ()){
User user = new user ();
User. setuserid (Rs. getstring ("user_id "));
User. setusername (Rs. getstring ("user_name "));
User. setpassword (Rs. getstring ("password "));
User. setcontacttel (Rs. getstring ("contact_tel "));
User. setemail (Rs. getstring ("email "));
User. setcreatedate (Rs. gettimestamp ("create_date "));
Userlist. Add (User );
}
} Catch (sqlexception e ){
E. printstacktrace ();
} Finally {
DB. closers (RS );
DB. closestmt (stmt );
DB. closeconn (conn );
}
Return userlist;
}

Later, we can use struts and other ORM technologies to greatly simplify the process. The above method should be rarely used at present.

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.