Using DBHelper to implement query database function based on "MVC" frame set design pattern

Source: Internet
Author: User

using DBHelper to implement query database function

(1) Connect the MySQL database, if unable to connect, first open the MySQL service. Create a new database with the name text and create a new data table with the name user, add three fields: User ID, user name, and password.

(2) Create a new Web project Usermanager, based on the MVC model design, build four packages: Beans, DAO, DBHelper, servlet. Import the required dbhelper into the dbhelper of the new Web project, and be careful to keep the database name consistent. Import the Mysql-connector-java-5.1.24-bin.jar database connection driver into the Web-inf Lib directory of the project.

The MVC----(model View Controller) design models mentioned here:

M: Represents business data and business rules. Includes DAO (beans), DBHelper (DBHelper), used to encapsulate database connections, and business database processing.

V: The interface that the user sees and interacts with, such as a JSP that often acts as a Web application view.

C: The receiving user is the input and calls the model and view to complete the user requirements.

(3) Create a new class in the beans package, name user, build a private entity class, and match the field names in the database to add the required get and set methods.

 Packagebeans; Public classuser{PrivateString UserID; PrivateString UserName; PrivateString password;  Public voidSetuserid (String userID) { This. userid=UserID; }
     PublicString GetUserID () {returnUserID; }
     Public voidsetusername (String userName) { This. username=UserName; }
     PublicString GetUserName () {returnUserName; }
     Public voidSetPassword (String password) { This. password=password; }
     PublicString GetPassword () {returnpassword; }
}


(4) in DAO, you need to create a new class to execute the SQL statement and to define a List result set to receive individual properties of the entity.

 PackageDAO; ImportJava.sql.*;
ImportJava.util.*; ImportDbhelper.dbhelper;Importbeans.*;  Public classUserdao { PublicList Getalluser () {Try{String SQL= "SELECT * from User"; ResultSet RS=Dbhelper.getresultset (SQL); List Users=NewArrayList ();  while(Rs.next ()) {User U=NewUser (); U.setuserid (Rs.getstring ("UserID")); U.setusername (Rs.getstring ("UserName")); U.setpassword (Rs.getstring ("Password"));
        Users.add (U); }
        returnusers; }
        Catch(Exception ex) {ex.printstacktrace (); return NULL;} }
}


(5) The new Userservlet in the servlet defines the necessary methods of its own, but because it has only the characteristics of handling transactions and does not have the function of display, the redundant output statements are removed and the Doget method is executed Dopost method as well. When debugging code, if the servlet is not responding, you can first write the code comments in a servlet and print a word in order to detect if the servlet is valid.

 Packageservlet; Importjava.io.IOException;ImportJava.io.PrintWriter;Importjava.util.List; Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; ImportDao.userdao;  Public classUserservletextendsHttpServlet { PublicUserservlet () {Super(); }
     Public voiddestroy () {Super. Destroy (); }

     Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doPost (request, response); }
     Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {Userdao dao=NewUserdao (); List Users=Dao.getalluser (); Request.getsession (). SetAttribute ("Users", users); Response.sendredirect (".. /user.jsp "); }
     Public voidInit ()throwsservletexception {}}


(6) JSP page: The main is to create a form, using the list loop, receive servlet processed forms and fields.

<body> <%List<User> users= (list<user>) session.getattribute ("Users"); Out.print ("<table border=1>"); Out.print ("<tr><td> user id</td><td> user name </td><td> password </td></tr>");  for(User u:users) {out.print ("<tr>"); Out.print ("<td>" +u.getuserid () + "</td>"); Out.print ("<td>" +u.getusername () + "</td>"); Out.print ("<td>" +u.getpassword () + "</td>"); Out.print ("</tr>"); } out.print ("</table>"); %> </body>


(7) Start running (commissioning)

Run Userservlet, he will automatically jump to the user.jsp, if the direct execution of user.jsp, the results can not be transferred, then no content appears. The running result, consistent with the contents of the database, indicates success.

Using DBHelper to implement query database function based on "MVC" frame set design pattern

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.