zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    Kali下mMetasploit数据连不上
    win10wifi消失
    cmd命令ipconfig或者ping无法使用
    WebFuzzing方法
    SyntaxError: Non-ASCII character 'xe5' in file c:/Users/Administrator/Desktop/1.py on line 6, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
    Hash传递攻击的本质、缓解方式及部分绕过手段
    python常見報錯
    How to Hack APIs in 2021(API漏洞利用)
    Burp Suite中文乱码
    RancherOS安装(方便漏洞复现)
  • 原文地址:https://www.cnblogs.com/rainbow-1/p/14138274.html
Copyright © 2011-2022 走看看