怎么创建音乐网站东莞企业网站模板建站
准备环境
1. 添加web
- 点击项目
- 右键——>选择**添加框架**
- 选择**web应用程序**
2.创建lib目录
- 在web应用程序的**WEB-INF目录下**创建lib目录
- 添加jar包(5个)
- 解压:右键——>选择**添加库**
3.创建Dao层
- 在src目录下创建包com.zmq
- 在该包下创建dao层
- 添加工具类BaseDao
- 创建相应的类,继承BaseDao
4. 添加属性文件
- 在src目录下添加属性文件
5. 创建实体层entity
- 在com.zmq包下创建entity包
- 在该包下创建相应的实体类
6. 添加配置
选择tomcat 服务器——>选择本地——>启动服务器
1.登录
UserDao——封装方法
//登录public User selectByNameAndPwd(String name,String password){User user=null;try {getCon();String sql="select * from user where u_name=?&&password=?";ps=con.prepareStatement(sql);ps.setObject(1,name);ps.setObject(2,password);rs=ps.executeQuery();while(rs.next()){user=new User();user.setId(rs.getInt("id"));user.setName(rs.getString("u_name"));user.setPassword(rs.getString("password"));}} catch (Exception e) {throw new RuntimeException(e);} finally {closeAll();}return user;}
TextUserDao——单元测试
@Testpublic void testSelectByNAP(){userDao.selectByNameAndPwd("张四","123456");}
login.jsp——登录页面
<body><%//获取error的值String error = request.getParameter("error");//判断,如果error为1,表示输入的密码或用户名有误if("1".equals(error)){out.print("<font color='red'>用户名或密码有误</font>");}%><form action="loginDo.jsp" method="post">账号:<input type="text" name="name"/><br>密码:<input type="password" name="password"/><br><input type="submit" value="登录"/><input type="button" onclick="register()" value="注册"/></form></body>
loginDo.jsp——登录处理页面
<body>
<%//设置编码request.setCharacterEncoding("utf-8");//接收数据String name = request.getParameter("name");String password = request.getParameter("password");//调用UserDao中的方法UserDao userDao=new UserDao();User user = userDao.selectByNameAndPwd(name, password);//依据返回值user是否为空判断输入密码,用户名是否正确if(user!=null){response.sendRedirect("success.jsp");}else {response.sendRedirect("login.jsp?error=1");}
%>
</body>
2. 注册
UserDao——封装方法
//注册public int add(String name,String password){String sql="insert into user values(null,?,?)";return edit(sql,name,password);}
TestUserDao——单元测试
UserDao userDao=new UserDao();@Testpublic void testAdd(){userDao.add("张数","123456");}
login.jsp——登录页面
<body><%//获取error的值String error = request.getParameter("error");//判断,如果error为1,表示输入的密码或用户名有误if("1".equals(error)){out.print("<font color='red'>用户名或密码有误</font>");}%><form action="success.jsp" method="post">账号:<input type="text" name="name"/><br>密码:<input type="password" name="password"/><br><input type="submit" value="登录"/><input type="button" onclick="register()" value="注册"/></form></body>
<script>function register(){location.href="register.jsp";}
</script>
register.jsp——注册页面
<body>
<form action="registerDo.jsp" method="post">账号:<input type="text" name="name1"/><br>密码:<input type="text" name="password1"/><br><input type="submit" value="注册"/>
</form>
registerDo.jsp——注册处理页面
<body>
<%//设置编码request.setCharacterEncoding("utf-8");//接收数据String name1 = request.getParameter("name1");String password1 = request.getParameter("password1");//调用UserDao中的方法UserDao userDao=new UserDao();int add = userDao.add(name1, password1);if(add>0){response.sendRedirect("login.jsp");}else {response.sendRedirect("register.jsp");}
%>
</body>
3. 查询全部并显示
BooksDao——封装方法
//查询所有图书信息public List<Books> selectAll(){List<Books> list=new ArrayList<>();try {getCon();String sql="select * from books";ps=con.prepareStatement(sql);rs= ps.executeQuery();while (rs.next()){Books books=new Books();books.setId(rs.getInt("book_id"));books.setType(rs.getString("type_id"));books.setName(rs.getString("book_name"));books.setAuthor(rs.getString("author"));books.setPublisher(rs.getString("publisher"));books.setPrice(rs.getInt("price"));list.add(books);}} catch (Exception e) {throw new RuntimeException(e);} finally {closeAll();}return list;}
TestBooksDao——单元测试:输出书本名字
BooksDao booksDao=new BooksDao();@Testpublic void testSelectAll(){List<Books> list = booksDao.selectAll();for (Books b: list) {System.out.println(b.getName());}}
success.jsp——登录成功后的显示页面
<head><title>Title</title><style>div{color: black;text-align: center;}a{text-decoration: none;}</style>
</head>
<body>
<%BooksDao booksDao=new BooksDao();List<Books> list = booksDao.selectAll();
%>
<div>登录成功页面</div>
<button>添加</button><br>
<table border="1px" align="center" width="70%" cellspacing="0"><tr><th>编 号</th><th>类 型</th><th>名 称</th><th>作 者</th><th>出版社</th><th>价 格</th><th>操 作</th></tr><%for (Books b:list) {%><tr><td><%=b.getId()%></td><td><%=b.getType()%></td><td><%=b.getName()%></td><td><%=b.getAuthor()%></td><td><%=b.getPublisher()%></td><td><%=b.getPrice()%></td><td><a href="#">编辑</a><a href="#">删除</a></td></tr><%}%>
</table></body>
4. 增加
BooksDao——封装方法
//增加public int add(String type,String name,String author,String publisher,Integer price){String sql="insert into books values(null,?,?,?,?,?)";return edit(sql,type,name,author,publisher,price);}
TestBooksDao——单元测试
@Testpublic void testAdd(){booksDao.add("CMP","活着","余华","清华大学出版社",23);}
success.jsp——登录成功后的显示页面
<button onclick="add()">添加</button><br>
<script>function add(){location.href="add.jsp";}
</script>
add.jsp添加页面
<body>
<form action="addDo.jsp" method="post">类 型:<input type="text" name="types"/><br>名 称:<input type="text" name="names"/><br>作 者:<input type="text" name="authors"/><br>出版社:<input type="text" name="publishers"/><br>价 格:<input type="number" name="prices"/><br><input type="submit" value="确认添加"/>
</form></body>
addDo.jsp添加处理页面
<body>
<%//设置编码格式request.setCharacterEncoding("utf-8");//接收数据String types = request.getParameter("types");String names = request.getParameter("names");String authors = request.getParameter("authors");String publishers = request.getParameter("publishers");String prices = request.getParameter("prices");//调用BooksDao中的方法BooksDao booksDao=new BooksDao();int add = booksDao.add(types, names, authors, publishers, Integer.parseInt(prices));//判断if(add>0){response.sendRedirect("success.jsp");}else {response.sendRedirect("add.jsp");}%>
</body>
5. 删除
BooksDao——封装方法
//删除public int delete(Integer id){String sql="delete from books where book_id=?";return edit(sql,id);}
TestBooksDao——单元测试
@Testpublic void testDel(){booksDao.delete(4);}
success.jsp——登录成功后的显示页面
<a href="delDo.jsp?id=<%=b.getId()%>">删除</a>
deleteDo.jsp删除操作页面
<body>
<%//接收数据String id = request.getParameter("id");//调用BooksDao中的方法BooksDao booksDao=new BooksDao();int delete = booksDao.delete(Integer.parseInt(id));if(delete>0){out.print("<script>alert('删除成功');location.href='success.jsp'</script>");}else{out.print("<script>alert('删除失败');location.href='success.jsp'</script>");}
%>
</body>
6. 修改
6.1数据回显
BooksDao——封装方法
//根据id查询public Books selectById(Integer id){Books books=null;try {getCon();String sql="select * from books where book_id=?";ps=con.prepareStatement(sql);ps.setObject(1,id);rs=ps.executeQuery();while (rs.next()){books=new Books();books.setId(rs.getInt("book_id"));books.setType(rs.getString("type_id"));books.setName(rs.getString("book_name"));books.setAuthor(rs.getString("author"));books.setPublisher(rs.getString("publisher"));books.setPrice(rs.getInt("price"));}} catch (Exception e) {throw new RuntimeException(e);} finally {closeAll();}return books;}
TestBooksDao——单元测试
@Testpublic void testSelectById(){booksDao.selectById(1);}
success.jsp——登录成功后的显示页面
<a href="update.jsp?id=<%=b.getId()%>">编辑</a>
update.jsp修改页面,根据id查询并数据回显
<body><%//接收数据String id = request.getParameter("id");//调用BooksDao中的方法BooksDao booksDao=new BooksDao();Books books = booksDao.selectById(Integer.parseInt(id));%>
<form action="updateDo.jsp" method="post"><input type="hidden" name="id" value="<%=id%>"><br>类 型:<input type="text" name="type" value="<%=books.getType()%>"/><br>名 称:<input type="text" name="name" value="<%=books.getName()%>"/><br>作 者:<input type="text" name="author" value="<%=books.getAuthor()%>"/><br>出版社:<input type="text" name="publisher" value="<%=books.getPublisher()%>"/><br>价 格:<input type="number" name="price" value="<%=books.getPrice()%>"/><br><input type="submit" value="确认修改"/>
</form>
</body>
6.2确认修改
BooksDao——封装方法
//修改public int update(String type,String name,String author,String publisher,Integer price,Integer id){String sql="update books set type_id=?,book_name=?,author=?,publisher=?,price=? where book_id=?";return edit(sql,type,name,author,publisher,price,id);}
TestBooksDao——单元测试
@Testpublic void testUpdate(){booksDao.update("CMP","活着","余华","天津大学出版社",19,309108);}
updateDo.jsp修改操作页面:修改数据信息并同步到数据库
<body>
<%//设置编码request.setCharacterEncoding("utf-8");//接收数据String id = request.getParameter("id");String type = request.getParameter("type");String name = request.getParameter("name");String author= request.getParameter("author");String publisher= request.getParameter("publisher");String price= request.getParameter("price");//调用BooksDao的方法BooksDao booksDao=new BooksDao();booksDao.update(type,name,author,publisher,Integer.parseInt(price),Integer.parseInt(id));response.sendRedirect("success.jsp");
%>
</body>
7. 测试功能是否正确
测试写的功能有两种方法:
- 使用主函数
- 使用junit单元测试
步骤:
- 加入单元测试的依赖——junit的jar包
- 创建一个测试类——包含相应的测试方法
//注意:该方法没有参数,也没有返回值。StudentDao studentDao=new StudentDao();@Testpublic void testFindAll(){List<Student> list = studentDao.findAll();for(Student s:list){System.out.println(s.getName()+"--->"+s.getAge());}}
注意:测试类中的方法没有参数,也没有返回值,要加@Test注解,测试时:选中方法名——>右键选择Run
8.注意
1. 关于时间类型
数据库与Java类型的一一对照:
data——data:表示日期
time——time:表示时间
timestamp——timestamp:表示日期时间毫秒
- 获取数据:getDate、getTime、getTimestamp
- 数据库获取现在的时间:now()
2. 关于强转
String类型转换为Integer类型:Integer. parseInt(变量)
3.关于性别
当数据库中使用1、2表示男女时,在前端页面显示可以使用**三目运算符**
s.getGender=="1"?"男":"女";
4.关于超链接传参
?key=value
<a href="delDo.jsp?id=<%=s.getId()%>删除</a>"
5. 关于删除成功后弹出提示信息
if(delete>0){out.print("<script>alert('删除成功');location.href='success.jsp'</script>");}else{out.print("<script>alert('删除失败');location.href='success.jsp'</script>");}
使用script和alert
6.关于修改时的id
因为修改时id不可变,且需要传递id值,所以需要对id进行处理,两种方法
- 使用隐藏域hidden将id在前端页面隐藏
- 读取出id并设置为只读模式readonly
7. 单选框、复选框的默认
单选框:checked
复选框:selected