JSP代码 MVC编程开发实例
来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 14:52 点击:次
我之前一直搞不明白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 showPer
定义一个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 showPer
相关新闻>>
- 发表评论
-
- 最新评论 更多>>