zoukankan      html  css  js  c++  java
  • JavaWeb代码复用

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

    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、默认界面

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

    前台调用servlet:

    删除

    ${pageContext.request.contextPath}/

    3、验证(servlet传一个message)

    <%
        String message = (String)request.getAttribute("message");
        if(message!=null){
    %>

    <%} %>

  • 相关阅读:
    java获得文件的最后修改时间
    【Tomcat】解决Tomcat catalina.out 不断成长导致档案过大的问题
    mysql报错Packet for query is too large (12238 > 1024). You can change this value
    【Tomcat】linux下实时查看tomcat运行日志
    linux下面MySQL变量修改及生效
    【Vim命令大全】史上最全的Vim命令
    (总结)Linux的chattr与lsattr命令详解
    MySql将查询结果插入到另外一张表
    dos中定义变量与获取常见的引用变量以及四则运算、备份文件(set用法)
    批处理BAT替换与截取字符串的用法t1=%a:~3%是什么意思
  • 原文地址:https://www.cnblogs.com/wwyydd/p/14140335.html
Copyright © 2011-2022 走看看