MySQL output to page MVC mode

Source: Internet
Author: User

Previous article I mentioned in the JSP page bad

This article is about interface and code separation, can initially achieve two or three people to work together to complete a project

Okay, no more nonsense, go to the chase.

This is four steps.

First step, new project, new entity class

Second step, create a new database, follow the entity class to build the database

Step three, create a new servlet and JSP (this step is in order)

The first step, according to the figure is good, as for those four taglibs beginning of the package, you can see here: http://www.cnblogs.com/feathe/p/6946750.html

There's also a JDBC previous article that has detailed steps to download, and here's not much to say

Util that bag is in its own package (if you have to change, you use their own, if not use my name also line)

The class name (Connmanager) code is as follows:

 Packageutil;Importjava.sql.Connection;ImportJava.sql.DriverManager; Public classConnmanager {//Database 5 Large parameters    Private Static FinalString IP = "127.0.0.1"; Private Static FinalString PORT = "3306"; Private Static FinalString database_name = "one"; Private Static FinalString user_name = "root"; Private Static FinalString PASSWORD = "123"; Private Static FinalString DRIVER = "Org.gjt.mm.mysql.Driver";  Public StaticConnection getconnection ()throwsException {String URL= "jdbc:mysql://" +ip+ ":" +port+ "/" +database_name+ "? user=" +user_name+ "&password=" +password+ "";        Class.forName (DRIVER); Connection Conn=drivermanager.getconnection (URL); System.out.println ("Connmanager has created a connection:" +conn); returnConn; }}

Entity Class Code:

Class Name: Entity.java

 Packageentity;Importjava.sql.Date; Public classEntity {PrivateInteger ID; PrivateString name; PrivateDate Birt;
PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicDate Getbirt () {returnBirt; } Public voidSetbirt (Date Birt) { This. Birt =Birt; } }

Build database Code (Admin Doc interface operation):

int, nam varchar (+), Birt DATE);
INSERT into INF values (' 1 ', ' one number ', ' 2000-1-1 '), (' 2 ', ' second ', ' 2000-1-2 ');

INSERT into INF values (' 3 ', ' third ', ' 2000-1-3 '), (' 4 ', ' fourth ', ' 2000-1-4 ');

SELECT * from INF;

Create a new JSP with the following code:

JSP name (test.jsp)

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "  Prefix= "C"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Servlet Code:

Name (Action.java)

 PackageAction;Importjava.io.IOException;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.util.ArrayList;Importjava.util.List;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjava.sql.Connection;Importentity. Entity;Importutil. Connmanager; @WebServlet ("/action") Public classActionextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {Try{Connection Co=connmanager.getconnection (); String SQL= "SELECT * from INF"; PreparedStatement PA=co.preparestatement (SQL); ResultSet RS=Pa.executequery (); List<Entity> list =NewArraylist<entity>();  while(Rs.next ()) {Integer ID=rs.getint ("id"); String name=rs.getstring ("name"); Java.sql.Date Birt=rs.getdate ("Birt"); Entity en=NewEntity ();            En.setid (ID);            En.setname (name);            En.setbirt (Birt);        List.add (en);        } rs.close ();        Pa.close ();        Co.close (); Request.setattribute ("List", list); Request.getrequestdispatcher ("Test.jsp"). Forward (request, response); }Catch(Exception e) {System.out.println ("Exception occurred" +e.getmessage ());//If there is an error, this line of code will work, remember to look at the console, there will be surprise}}protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method Stubdoget (request, response); }}

This is a relatively complete project, followed by run as, note that the whole project is running, do not run a single JSP or Java, not here,

If you just write a single Java file, run it alone.

The effect is as follows:

In front of me talking about the initial teamwork, I believe you see here, the team to complete a project in the heart of a little idea.

Team work must have backbone, backbone decision, teammates to cooperate, no discussion.

For this project, I think, first of all,

The captain defines something first,

Database name, table name, field name and type, database user name, password, JSP name, collection name.

Define these things so that you can almost do your own piece, and finally integrate the code and run it again. This is my one-sided understanding.

Details are as follows (teacher's tips):

1. Modeling First (Build database tables and entity classes)
2, to understand the business process, who jumps to who, how many pages, how many classes
3, think good page jump, pass what data, show what data, etc.

4, team development, to have the group leader assigned tasks, who do what, as well as the public agreed variable name needs to be clear, and then to achieve simultaneous development.
5, the team development process, to calculate the time, statistical team strength is greater than the individual.

In contrast, mine is still not comprehensive.

Well, the article ends here and I hope it helps you ...

MySQL output to page MVC mode

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.