maxWait=3000
然后编写数据库连接工具类:
/**
- JDBC工具类 使用Durid连接池
*/
public class JDBCUtils {
private static DataSource ds ;
static {
try {
//1.加载配置文件
Properties pro = new Properties();
//使用ClassLoader加载配置文件,获取字节输入流
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream(“druid.properties”);
pro.load(is);
//2.初始化连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
- 获取连接池对象
*/
public static DataSource getDataSource(){
return ds;
}
/**
- 获取连接Connection对象
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
这里我们使用JdbcTemplate来进行数据连接操作数据库:
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
2. 实现持久层(Dao)
编写持久层接口:
/**
- 用户操作的DAO
*/
public interface UserDao {
}
实现持久层接口:
public class UserDaoImpl implements UserDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
}
3. 实现业务层(Service)
编写业务层接口:
/**
- 用户管理的业务接口
*/
public interface UserService {
}
实现业务层接口:
public class UserServiceImpl implements UserService {
private UserDao dao = new UserDaoImpl();
}
4.实现表现层功能
编写表现层:
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
- Code
*/
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException