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 @   靠谱杨  阅读(172)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示