JavaWeb代码复用

servlet部分,可能用得到的复用的代码:

 

DriverClass="com.mysql.jdbc.Driver"

 

url="jdbc:mysql://localhost:3306/info?useUnicode=true&setCharacterEncoding=UTF-8";

 

1、dopost设置字符

 

request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");

 

2、SQL语句:

增加:

String SQL_ADD="INSERT INTO book(bookid,bookname,bookauthor,pressname,availablenum) VALUE(?,?,?,?,?);";

preparedStatement=connection.prepareStatement(SQL_ADD);
connection.setAutoCommit(false);


preparedStatement.setString(1,bookid);
preparedStatement.setString(2,bookname);
preparedStatement.setString(3,bookauthor);
preparedStatement.setString(4,pressname);
preparedStatement.setString(5,availablenum);


int flag=preparedStatement.executeUpdate();


connection.commit();

 

删除:

String SQL_DELETE="DELETE FROM book where bookid=?";

preparedStatement=connection.prepareStatement(SQL_DELETE);
preparedStatement.setString(1, bookid);


int flag=preparedStatement.executeUpdate();


if(flag>0)
{
System.out.println("成功删除"+flag+"条图书信息!");
}else {
System.out.println("遇到问题,删除图书信息失败!");
}

 

修改:

SQL="UPDATE book SET availablenum=? WHERE bookid=?;";

preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setString(1,strnum);
preparedStatement.setString(2, bookid);

查询:

1、查询全部:

String  SQL_SELECTALL = "SELECT *FROM book";

 

preparedStatement = connection.prepareStatement(SQL_SELECTALL);
resultSet = preparedStatement.executeQuery();

 

while(resultSet.next()) {


String bookid=resultSet.getString("bookid");
String bookname=resultSet.getString("bookname");
String bookauthor=resultSet.getString("bookauthor");
String pressname=resultSet.getString("pressname");
String availablenum=resultSet.getString("availablenum");


newbook=new NewBook(bookid,bookname,bookauthor,pressname,availablenum);
list.add(newbook);
}

2、条件查询(模糊查询)

 

String SQL="SELECT * FROM book where bookname LIKE ?";

 

preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setString(1, "%"+bookname+"%");


connection.commit();
resultSet = preparedStatement.executeQuery();

 

前台jsp页面

 

1、使用jstl标签

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

2、默认界面

<table align="center" border="1" cellpadding="3" cellspacing="1"></table>

<c:forEach items="${list}" var="book"></c:forEach>

 

前台调用servlet:

 

<a href="${pageContext.request.contextPath}/bookservlet?method=deletebook&bookid=${book.bookid}">删除</a>

 

${pageContext.request.contextPath}/

 

3、验证(servlet传一个message)

 

<%
    String message = (String)request.getAttribute("message");
    if(message!=null){
%>
<script type="text/javascript">
  alert(" <%=request.getAttribute("message")%> ");
</script>

<%} %>

 

 

<td><input type="submit" value="提交" onclick='return checkauthor(bookauthorform)'></td>

<script type="text/javascript">
  function checkbook(form1){
  var f1=form1.bookname.value;
if(f1==""){
  alert("查询书名不能为空!");
  return false;
}
  return true;
}
function checkauthor(){
  var f1=form1.bookauthor.value;
if(f2==""){
  alert("查询作者名不能为空!");
return false;
}
return true;
}
</script>

 

posted @ 2020-12-15 14:15  靠谱杨  阅读(169)  评论(0编辑  收藏  举报