首页 > 代码库 > 10步教你学会简单的MVC架构的java程序(适合新手)
10步教你学会简单的MVC架构的java程序(适合新手)
适合新手,大拿手下留情,谢谢!
第1步:安装Oracle数据库,安装过程不赘述了;
第2步:新建一个账号zwp_test
//创建临时表空间 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; //创建数据表空间 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; //创建用户 create user zwp_test identified by zwp_test default tablespace zwp_test_data temporary tablespace zwp_test_temp; //授予权限 grant connect,resource,dba to zwp_test;
第3步:新建一个users表
create table users ( userid varchar2(8) not null primary key , username varchar2(10) not null, password varchar2(20) default '' not null , age int default '' not null , sex varchar2(20) not null ) tablespace zwp_test_data;
第4步:插入数据
insert into users(userid,username,password,age,sex) values('001','zhouwp','123',25,'man');
第5步:搭建环境,我就不赘述了,可以百度、谷歌
功能说明:
做一个用户查询的功能
资源结构如下图:
第6步:显示界面JSP
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <%@ page import="com.web.zwp.*" %> <%@ page import="java.util.*" %> <html> <head> <script type="text/javascript"> function checkAll(ifAll) { //alert(ifAll.checked); var selectFlags = document.getElementsByName("selectFlag"); for(var i=0;i<selectFlags.length;i++){ //selectFlag[i].checked=document.getElementById("ifAll").checked; selectFlags[i].checked=ifAll.checked; } } </script> </head> <body> <form action="ShowUsers"> <table width="95%" border="1" cellspacing="0" cellpadding="0" align="center"> <input type="text" name="userid" id="userid" value=http://www.mamicode.com/""/>>第7步:链接数据库
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() {} /** * 取得Connection * @return */ public 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 block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void close(PreparedStatement pstmt) { if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void close(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void close(ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { DB.getConnection(); } }第8步:创建User对象类
package com.web.zwp; public class User { private String UserId; private String UserName; private String PassWord; private int 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 = 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; } }第9步:新建一个用户管理类UserManager,类中实现查询方法
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; } /* * * 查询所有 */ public User findAllUser() { String sql = "select * from users"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; User user = null; try { conn = DB.getConnection(); pstmt = 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); DB.close(pstmt); DB.close(conn); } return user; } /* * 按照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(); pstmt = conn.prepareStatement(sql); pstmt.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); DB.close(pstmt); DB.close(conn); } return user; } }第10步:通过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(HttpServletRequest 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); //从数据库中获取数据传给request对象中的userList属性,并转发到前台jsp request.setAttribute("userList", userList); request.getRequestDispatcher("/ShowUsers.jsp").forward(request, response); } } public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
最终显示界面样式:
10步教你学会简单的MVC架构的java程序(适合新手)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。