Web三层架构
- Web层——转发jsp页面,封装数据传给Service层处理
- Service层——接受Web层传来的数据,处理业务逻辑
- Dao层——实现对数据库的操作,提供接口给Service层调用
三层架构有利于更好的解耦,不同层各司其职。
开发顺序
这里我是置底向上的开发。
目录结构
cn.login.dao.impl
cn.login.dao
cn.login.exception
cn.login.impl
cn.login.service
cn.login.utils
cn.login.web
jdbc工具类
封装了常用的操作jdbc的方法,以便dao层来调用.使用了java中的.properties文件来设置配置信息
public class JdbcUtils{
private static String url ;
private static String dirver ;
private static String user ;
private static String password ;
static{//在类第一次加载时,会执行块中的代码
Properties properties =new Properties();
try(InputStream in = new FileInputStream((String.valueOf(JdbcUtils.class.getResource("db.properties").getPath())))) //自动关闭资源,非常重要
{ //加载配置文件
properties.load(in);
}
catch (IOException e) {
e.printStackTrace();
}
//获取配置文件的信息
url = properties.getProperty("URL");
dirver = properties.getProperty("DRIVER");
user = properties.getProperty("USER");
password = properties.getProperty("PASSWORD");
try {
Class.forName(dirver);//加载驱动
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//静态方法就不需要创建实例来调用了
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
public static void release(Connection connection, Statement statement, ResultSet resultSet){
if(resultSet != null)
try{