• 简单的封装数据的模板


     

       public boolean delete(int a ) {
            boolean flag=false;//删除  搜索并删除表 no1 中 A 列为 a 的那一行
            Connection conn =link.Link.getConnection();
            Statement st=null;
            try {
                st=conn.createStatement();
                st.executeUpdate("delete from no1 where A='"+a+"'");
                flag=true;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                link.Link.close(st, conn);
            }
            return flag;
        }
        
        public boolean update(Bean bean) {
            boolean flag=false;//更改  更改表 no1 中 A 列为 bean.getA() 的那一行
            Connection conn=link.Link.getConnection();
            Statement st=null;
            try {
                st=conn.createStatement();
                st.executeUpdate("update no1 set A='"+bean.geta()+"',B='"+bean.getb()+"',C='"+bean.getc()+"'where A='"+bean.geta()+"'");
                flag=true;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return flag;
        }
        
        public List<Bean> list(){
            Connection conn=link.Link.getConnection();
            Statement st=null;//浏览 表 no1 中 A 列  
            List<Bean> list=new ArrayList<>();
            ResultSet rs=null;
            Bean bean=null;
            try {
                st=conn.createStatement();
                rs=st.executeQuery("select * from no1 order by A");
                while(rs.next()) {
                    int A=rs.getInt("A");
                    String B = rs.getString("B");
                    String C = rs.getString("C");
                    bean=new Bean(A,B,C);
                    list.add(bean);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally {
                link.Link.close(rs, st, conn);
            }
            return list;
        }
        
        public List<Bean> searchByName(String str) throws SQLException{
            Connection conn=link.Link.getConnection();
            Statement st=null;//查询 表 no1 中 B 或 C 列中存在 str 的  
            PreparedStatement pt = conn.prepareStatement("select * from no1 where(B like '%"+str+"%' or C like '%"+str+"%')");
            List<Bean> search=new ArrayList<>();
            ResultSet rs=null;
            Bean bean=null;
            try {
                rs=pt.executeQuery();
                while(rs.next()) {
                	int A=rs.getInt("A");
                    String B = rs.getString("B");
                    String C = rs.getString("C");
                    bean=new Bean(A,B,C);
                    search.add(bean);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally {
                link.Link.close(rs, st, conn);
            }
            return search;
        }   
        
    }
    

      Link

    package link;
    
    import java.sql.*;
    
    public class Link {
        private static String url = "jdbc:mysql://localhost:3306/end?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
        private static String user = "root";
        private static String password = "123456";
        public static  Connection getConnection() {
            Connection con=null;
            try {
                con=DriverManager.getConnection(url, user, password);
                System.out.println("连接成功");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return con;
        }
        public static void close(Connection con) {
            if(con!=null)
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }       
        }
        public static void close(Statement state, Connection conn) {
            if(state!=null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }  
        public static void close(ResultSet rs, Statement state, Connection conn) {
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(state!=null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

      Servlet.java

    package servlet;
    import java.io.*;
    import java.util.List;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import bean.Bean;
    import dao.Dao;
    @WebServlet("/servlet")
    
    public class Servlet extends HttpServlet {
        Dao Dao=new dao.Dao();
        private static final long serialVersionUID = 1L;
        public Servlet() {
            super();
        } 
        
        private void insert(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            request.setCharacterEncoding("utf-8");
            int A = Integer.parseInt(request.getParameter("A"));
            String B = request.getParameter("B");
            String C = request.getParameter("C");
            Bean bean=new Bean(A,B,C);
            if(Dao.insert(bean)) {
                request.setAttribute("message", "添加成功");
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
        }
        
        private void delete(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
            request.setCharacterEncoding("UTF-8");
            int A=Integer.parseInt(request.getParameter("A"));
            Dao.delete(A);
            request.setAttribute("message", "删除成功");
            request.getRequestDispatcher("servlet?method=list").forward(request, response);
        }  
        
        private void list(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
            request.setCharacterEncoding("UTF-8");
            List<Bean> list = Dao.list();
            request.setAttribute("list", list);
            request.getRequestDispatcher("list.jsp").forward(request, response);
        }
        
        private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            int A = Integer.parseInt(request.getParameter("A"));
            String B = request.getParameter("B");
            String C = request.getParameter("C");
            Bean bean=new Bean(A,B,C);
            Dao.update(bean);
            request.setAttribute("message", "修改成功");
            request.getRequestDispatcher("servlet?method=list").forward(request, response);
        }
        
        private void search(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
            request.setCharacterEncoding("UTF-8");
            request.setAttribute("search", "查询成功");
            request.getRequestDispatcher("list.jsp").forward(request, response);
        }
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            String method=request.getParameter("method");
            if("insert".equals(method)) {
                insert(request,response);           
            }else if("delete".equals(method)) {
                try {
                    delete(request,response);
                }catch (Exception e) {
                    e.printStackTrace();
                }          
            }else if("update".equals(method)) {
                update(request,response);
            }else if("list".equals(method)) {
                try {
                    list(request,response);
                }catch (Exception e) {
                    e.printStackTrace();
                }     
            }else if("search".equals(method)) {
                try {
                    search(request,response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }    
        }
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);     
        }
    }
    

      

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>这里是标题</title>
    </head>
    <body>
        <%
            Object message = request.getAttribute("message");
            if (message != null && !"".equals(message)) {
        	%>
        	<script type="text/javascript">   alert("<%=request.getAttribute("message")%>");    </script>
        	<%}%>
    <div align="center">
    	<h1>表名</h1>
      	<a href="A.jsp">跳转A</a>     
      <form action="servlet?method=insert" method="post">
          <table id="addTable" class="table table-bordered  ">
            	<tr> 
                	<td class="col-sm-2">输入框A</td>
                    <td class="col-sm-4"><input type="text" class="form-control"  name="输入框A" id="输入框A" ></td>
               	<tr>
                    <td class="col-sm-2">输入框B</td>
                    <td><input type="text" class="form-control"  name="输入框B" id="输入框B" placeholder="输入框B"></td>
                <tr>
                    <td class="col-sm-2">选择框</td>
                    <td class="col-sm-4">
                      	<input type="radio"  name="选择框" id="选择框" value="内容A">内容A
                      	<input type="radio"  name="选择框" id="选择框" value="内容B">内容B
                      	</td>  <tr>  <td></td>  <td>
                      	<input type="radio"  name="选择框" id="选择框" value="内容C">内容C
                    </td>
                <tr>
                    <td>下拉框</td>
                    <td colspan="3">
                      	<select class="form-control" id="下拉框" name="下拉框">
                        	<option value="下拉框A">下拉框A</option>
                        	<option value="下拉框B">下拉框B</option>
                        	<option value="下拉框C">下拉框C</option>
                      	</select>
                    </td>
                <tr>
          </table>
          <input type="submit" value="确认"  onclick= "return check()" /> 
      </form> 
    </div>
    </body>
    <script type="text/javascript">
            function check()                   
            {
            	if(document.getElementById('输入框A').value=='') {            
            		alert('输入框A不能为空!');
            		document.getElementById('输入框A').focus();
            		return false;
                }else if(document.getElementById('输入框A').value%1!=0){
            		alert('输入框A不是整数!');
            		document.getElementById('输入框A').focus();
            		return false;
            	}        	
            	if(document.getElementById('输入框B').value=='') {            
            		alert('输入框B不能为空!');
            		document.getElementById('输入框B').focus();
            		return false;
                }else if(document.getElementById('输入框B').value.length!=18) {
          		  alert('输入框B位数错误!');
          		  document.getElementById('输入框B').focus();
          		  return false;
          		}
            }      
        </script>
    </html>
    

      

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>这里是标题</title>
    </head>
    <body>
    <% 
    Object message =request.getAttribute("message");
    if(message!=null&&!"".equals(message)){
    	%>
        <script type="text/javascript">alert("<%=request.getAttribute("message")%>");</script>
    	<%}%>	
    	
        <p style="text-align:center;color:black;font-family:宋体,serif; font-size:30px">   
        	这里是介绍
        </p>
        <p style="text-align:center"> 
                <br>
                <input type="button" value="跳转A" onclick="location.href='A.jsp'" />
                <br><br>
                <input type="button" value="跳转B" onclick="location.href='B.jsp'" /> 
                <br><br> 
                <input type="button" value="返回上一页" onclick="javascript:history.back(-1)" />
        </p>
    </body>
    </html>
    

      

  • 相关阅读:
    IntelliJ Idea使用代码折叠
    c# 文件属性读取操作及文件之间操作
    c#文件操作
    c++头文件 #include<iostream>
    基本SQL语句
    dd
    c#属性中的get和set属性
    c#
    c#运算表达式
    c#方法
  • 原文地址:https://www.cnblogs.com/da48/p/14229068.html
Copyright © 2020-2023  润新知