JSP代码 MVC编程开发实例
程序员文章站
2023-01-29 16:52:40
我之前一直搞不明白javabean在mvc中到底充当一个什么角色。现在来看一个实例定义一个javabeanpackage com.bean;public class user { &nbs...
我之前一直搞不明白javabean在mvc中到底充当一个什么角色。现在来看一个实例
定义一个javabean
package com.bean;
public class user {
private string username;
private string userpwd;
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
}
public string getuserpwd() {
return userpwd;
}
public void setuserpwd(string userpwd) {
this.userpwd = userpwd;
}
}
javabean的作用:是一个po对象之前我以为是vo对象。搞错了!
恩我们可以这样来理解javabean的作用其实就是用来传递数据的一个东西。在我们这次phpcms项目中相当于一个结果集的传输。控制器接收到一个来自m层的数据之后如何传递到视图层作渲染处理。可以依据这样的方式。
再来看我们的action类
package com.dao;
import java.sql.*;
import com.bean.user;
public class dao {
connection conn = null;
statement stmt = null;
resultset rs = null;
preparedstatement pstmt = null
public dao(){
try {
class.forname("com.mysql.jdbc.driver");
conn = getconnection();
} catch (classnotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
public connection getconnection(){
string url = "jdbc:mysql://localhost:3306/person";
string user = "root";
string password = "123456";
try {
conn = drivermanager.getconnection(url, user, password);
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return conn;
}
public int addperson(user user){
int flag = 0;
string sql = "insert into user(user_name,user_pwd) values(?,?)";
try {
pstmt = conn.preparestatement(sql);
pstmt.setstring(1, user.getusername());
pstmt.setstring(2, user.getuserpwd());
flag = pstmt.executeupdate();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return flag;
}
public resultset showperson(){
string sql = "select * from user";
try {
pstmt = conn.preparestatement(sql);
rs = pstmt.executequery();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return rs;
}
public void close(){
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
}
这个类把连接操作也放在这里了。其实是不正确的处理方式。正确的方式应当是将数据库层单独写到一个class类里面去。但这里面我们来看一下:
public int addperson(user user){
int flag = 0;
string sql = "insert into user(user_name,user_pwd) values(?,?)";
try {
pstmt = conn.preparestatement(sql);
pstmt.setstring(1, user.getusername());
pstmt.setstring(2, user.getuserpwd());
flag = pstmt.executeupdate();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return flag;
}
这个dao对象它的作用是接收来自javabean层的数据。
再看是如何set值到这个bean的
package com.servlet;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import com.bean.user;
import com.dao.dao;
public class adduser extends httpservlet {
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
string username = request.getparameter("username");
string userpwd = request.getparameter("userpwd");
user user = new user();
user.setusername(username);//依据的是set与get方式了吧!!!!
user.setuserpwd(userpwd);
dao dao = new dao();
定义一个javabean
package com.bean;
public class user {
private string username;
private string userpwd;
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
}
public string getuserpwd() {
return userpwd;
}
public void setuserpwd(string userpwd) {
this.userpwd = userpwd;
}
}
javabean的作用:是一个po对象之前我以为是vo对象。搞错了!
恩我们可以这样来理解javabean的作用其实就是用来传递数据的一个东西。在我们这次phpcms项目中相当于一个结果集的传输。控制器接收到一个来自m层的数据之后如何传递到视图层作渲染处理。可以依据这样的方式。
再来看我们的action类
package com.dao;
import java.sql.*;
import com.bean.user;
public class dao {
connection conn = null;
statement stmt = null;
resultset rs = null;
preparedstatement pstmt = null
public dao(){
try {
class.forname("com.mysql.jdbc.driver");
conn = getconnection();
} catch (classnotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
public connection getconnection(){
string url = "jdbc:mysql://localhost:3306/person";
string user = "root";
string password = "123456";
try {
conn = drivermanager.getconnection(url, user, password);
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return conn;
}
public int addperson(user user){
int flag = 0;
string sql = "insert into user(user_name,user_pwd) values(?,?)";
try {
pstmt = conn.preparestatement(sql);
pstmt.setstring(1, user.getusername());
pstmt.setstring(2, user.getuserpwd());
flag = pstmt.executeupdate();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return flag;
}
public resultset showperson(){
string sql = "select * from user";
try {
pstmt = conn.preparestatement(sql);
rs = pstmt.executequery();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return rs;
}
public void close(){
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
}
这个类把连接操作也放在这里了。其实是不正确的处理方式。正确的方式应当是将数据库层单独写到一个class类里面去。但这里面我们来看一下:
public int addperson(user user){
int flag = 0;
string sql = "insert into user(user_name,user_pwd) values(?,?)";
try {
pstmt = conn.preparestatement(sql);
pstmt.setstring(1, user.getusername());
pstmt.setstring(2, user.getuserpwd());
flag = pstmt.executeupdate();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return flag;
}
这个dao对象它的作用是接收来自javabean层的数据。
再看是如何set值到这个bean的
package com.servlet;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import com.bean.user;
import com.dao.dao;
public class adduser extends httpservlet {
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception {
string username = request.getparameter("username");
string userpwd = request.getparameter("userpwd");
user user = new user();
user.setusername(username);//依据的是set与get方式了吧!!!!
user.setuserpwd(userpwd);
dao dao = new dao();
上一篇: Linux之linux基础命令2