JavaWeb mvc模型
mvc中m/v/c各是什么呢?
**M:**模型; //数据模型,
V: View视图,给用户看的;
C: Controller控制器,在Data进行验证(findall)
JAVABean:—;
**DAO:(Date Access Object)😗*是一个类,获取数据的模式,我要去写一个类去访问数据库,来获取数据库表的信息。
(先写一个接口,然后实现接口)
**DAO:**工厂类用DAOfactory.getDAO(配置文件)方法来实现各种各样增删的操作
JDBC
JDBCUtils(配置文件)来实现数据库的链接。
看DAY17,代码导入myeclipse
新建java项目(实例):
新建class名:User 包:edu.hdky.cs.domain
public class User
{
//用户管理,需要两个字段
private String Username;
private String Password;
//生成无参构造器
//右键选择source命令,Generate getter and sourcer ,点击OK
//就自动生成了get和set方法
pulic User---
public String getPassword()
{
return password;
}
public String getPassword()
{
this Password=Password;
}
//--------
public String getUsername()
{
return Username;
}
public String getUsername()
{
this Username=Username;
}
}
新建类UserDao
//包为java.util,list;
import java.util,list;
import edu.hdky.cs.dao
public interface UserDao{
pubilc void add(User user);
pubilc void mod(User user);
pubilc void del(String uid);
pubilc User loda(String uid);
pulic List<User> findAll();
}
new 一个class,名为UserDaoimpl,在interfaces中选择edu.hdky.cs.dao,(代码自动补齐,代码中为空)
//导入
import java.util,list;
import edu.hdky.cs.domian.User;
public void add(User user){
Connection conn=null;
PreperedStatement pstmt=null;
try{ //防止出现错误用try catch来包裹他
conn=JdbcUtils.getConnection();
String sql="insert into t_user valuse(?,?,?)";
String sql="update t_user set username=?,password=? where uid=?";
pstmt=conn.prepareStatement(sql);
pstmt.serString(1,ser.getUsername());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getUid());
pstmt.executeUpdate();
}catch(Exception e){
throw new RuntimeException(e); //数据库可能出错,出错就转到调用的类,让他解决错误
}finally{
try {
if(pstmt !=null)pstmt.close
if(conn!=null)conn.close
}catch(Exception e){}
public User loda(String uid)
{
//复制上面的语句.
Connection conn=null;
PreperedStatement pstmt=null;
ResultSet rs=null; //导入
try{ //防止出现错误用try catch来包裹他
conn=JdbcUtils.getConnection();
String sql="insert into t_user valuse(?,?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,uid);
pstmt.executeJQuery();
if(rs.next()){
return new User(rs.getString(1),rs.getString(2),rs.getString(3);
}
}catch(Exception e){
throw new RuntimeException(e); //数据库可能出错,出错就转到调用的类,让他解决错误
}finally{
try {
if(pstmt !=null)pstmt.close
if(conn!=null)conn.close
}catch(Exception e){}
}
}
导入文章写的jdbcUtils.java
new一个DVO工厂类 UserDAOfactory
puclic class UserDAOfactory
{
private static User userrDao;
static {
try{InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("dao.poerties");
Properties prop =new Properties;
prop.load(in);
String ClassName=prop.getProperty("edu.hdky.cs.dao.UserDao")
Class clazz=Class.forName(ClassName)//映射技术
userDao=(UserDao)clazz.newInstance();
}catch(Exception e)
{
throw new RuntimeException(e);
}
}
pulic static UserDao getUserDao()
{
return userDao;//调用工厂类,返回一个edu.hdky.cs.dao.UserImpl类型实例。
}
}
new 一个dao.properties在DVO工厂类文件下
// 写下 ,更新实例//edu.hdky.cs.dao.UserDao=edu.hdky.cs.dao.UserImpl;