10 steps to teach you to learn simple MVC architecture for Java programs (for beginners)

Source: Internet
Author: User

Suitable for novice, big good under mercy, thank you!

1th Step: Install the Oracle database, the installation process does not repeat;

2nd step: Create a new account Zwp_test

Creating a temporary tablespace create temporary tablespace zwp_test_temp   tempfile ' F:\app\Administrator\oradata\zwp_test_temp.dbf '  size 50m   autoextend on   next 50m maxsize 20480m   extent management local;//creating data table space Create Tablespace ZWP_ Test_data   logging   datafile ' F:\app\Administrator\oradata\zwp_test_data.dbf '  size 50m   autoextend On   next 50m maxsize 20480m   extent management local;//creating user Create users Zwp_test identified by Zwp_testdefault Tabl Espace zwp_test_datatemporary tablespace zwp_test_temp;//grants permission grant CONNECT,RESOURCE,DBA to Zwp_test;

3rd Step: Create a new users table

CREATE TABLE Users (  userid   varchar2 (8) NOT NULL primary key, username VARCHAR2 (TEN) not NULL, password VARCHAR2 (2 0) Default ' NOT NULL, age      int default ' is not NULL, sex      varchar2 (+) NOT null) tablespace zwp_test_data;

4th step: Inserting data

Insert into users (userid,username,password,age,sex) VALUES (' 001 ', ' zhouwp ', ' 123 ', ' n ', ' man ');

5th step: Build the environment, I will not repeat, can Baidu, Google

Function Description:

The ability to do a user query

Resource structures such as:

6th step: Display Interface jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%><%@ page import= "com.web.zwp.*"%> <%@ page import= "java.util.*"%>

7th step: Link Database

Package Com.web.db;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.PreparedStatement; Import Java.sql.resultset;import Java.sql.sqlexception;public class db {private DB () {}/** * get connection * @return */publ IC static Connection getconnection () {Connection conn = null;try {class.forname ("oracle.jdbc.driver.OracleDriver"); conn = Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:drp", "Zwp_test", "Zwp_test");} catch (ClassNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (SQLException e) {//TODO Auto-generated catch Blocke.printstacktrace ();} Return conn;} public static void Close (PreparedStatement pstmt) {if (pstmt! = null) {try {pstmt.close ()} catch (SQLException e) {//TOD O auto-generated catch Blocke.printstacktrace ();}}} public static void Close (Connection conn) {if (conn! = null) {try {conn.close ()} catch (SQLException e) {//TODO Auto-gen Erated catch Blocke.printstacktrace ();}}} public static void Close (ResultSetRS) {if (rs! = null) {try {rs.close ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} public static void Main (string[] args) {db.getconnection ();}}

8th step: Create the User object class

Package Com.web.zwp;public class User {private string Userid;private string Username;private string Password;private int< C0/>age;private string Sex;public string GetUserName () {return UserName;} public void Setusername (String userName) {userName = UserName;} Public String GetPassword () {return PassWord;} public void SetPassword (String passWord) {PassWord = PassWord;} public int getage () {return age;} public void Setage (int.) {age = age;} Public String Getsex () {return Sex;} public void Setsex (String sex) {sex = sex;} Public String GetUserId () {return UserId;} public void Setuserid (String userId) {userid = userid;}}

9th step: Create a new user management class Usermanager, implement the Query method in the class

Package Com.web.zwp;import Java.sql.*;import Com.web.db.db;import Com.web.zwp.user;public class UserManager {private Static Usermanager instance = new Usermanager (); Usermanager () {}public static Usermanager getinstance () {return instance;} /* * Query all */public User findalluser () {String sql = "SELECT * from users"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; User User = null;try {conn = db.getconnection ();p stmt = conn.preparestatement (sql); rs = Pstmt.executequery (); if (Rs.next ( ) {user = new user (), User.setuserid (rs.getstring ("userid")), User.setusername (rs.getstring ("username")); User.setpassword (rs.getstring ("password")), User.setage (Rs.getint ("Age")), User.setsex (rs.getstring ("Sex"));}} catch (SQLException e) {e.printstacktrace ();} Finally {Db.close (RS);D b.close (pstmt);D b.close (conn);} return user;} /* * Query by ID */public User Findalluserbyid (String userId) {String sql = "SELECT * from Users where userid=?"; Connection conn = null; PreparedStatement pstmt = null; ResUltset rs = null; User User = null;try {conn = db.getconnection ();p stmt = conn.preparestatement (sql);p stmt.setstring (1, userId); rs = pstmt. ExecuteQuery (), if (Rs.next ()) {user = new user (), User.setuserid (rs.getstring ("userid")), User.setusername ( Rs.getstring ("username")), User.setpassword (rs.getstring ("password")), User.setage (Rs.getint ("Age")); user.setsex (Rs.getstring ("Sex"));}} catch (SQLException e) {e.printstacktrace ();} Finally {Db.close (RS);D b.close (pstmt);D b.close (conn);} return user;}

10th step: Implement the Control section by Httpservet m

Package Com.web.zwp;import Java.io.ioexception;import Java.util.arraylist;import java.util.list;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class ShowUsers extends HttpServlet {/** * *///private static final long serialversionuid = 3434710866509057630l;public void DoPost (httpservletre Quest request, HttpServletResponse response) throws Servletexception, IOException {String userid=request.getparameter ( "UserID"); if (Userid==null | | Userid.trim (). Length () ==0) {User user=usermanager.getinstance (). Findalluser (); list<user> userlist = new arraylist<user> (); Userlist.add (User); Request.setattribute ("UserList", userlist) Request.getrequestdispatcher ("/showusers.jsp"). Forward (request, response);} Else{user user=usermanager.getinstance (). Findalluserbyid (userid); list<user> userlist = new arraylist<user> (); Userlist.add (user);//Get data from the database to User in the Request objectThe List property is forwarded to the foreground Jsprequest.setattribute ("UserList", userlist); Request.getrequestdispatcher ("/showusers.jsp"). Forward (request, response);}} public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { This.dopost (request, Response);}}


Final Display Interface style:








10 steps to teach you to learn simple MVC architecture for Java programs (for beginners)

Related Article

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.