芥末果酱

导航

12.9自习

三小时理解后台应用方式方法

主要学习页面,写了添加用户页面,为每个用户职位设置编号,登录时输入用户密码,后台确认用户职位打开相应页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录添加</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
        width: 160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</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 style="color: red;">添加用户</h1>
        
        <form action="CEServlet?method=Cadd" method="post" onsubmit="return check()">
            <div class="a">
                用户名<input type="text" id="user" name="user"/>
            </div>
            <div class="a">
                密码<input type="password" id="passward" name="passward" />
            </div>
            <div class="a">
                类型<input type="text" id="zhiwu" name="zhiwu"/>
            </div>
            
            
            <div class="a">
                <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>
            </div>
            <div class="a">
            <a href="index.jsp" >返回</a>
            </div>
        </form>
    </div>
    
</body>
</html>
package ceyan;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import xuexi.Bill;
import xuexi.DBUtil;


public class CEDao {
     public boolean add(users user1) {
            String sql = "insert into (user,passward,zhiwu) values ('" + user1.getUser() + "','" + user1.getPassward() + "','"+user1.getZhiwu()+"')";
            
            Connection conn = DBUtil.getConn();//调用方法连接数据库
            Statement state = null;
            boolean f = false;
            int a = 0 ;
            
            try {       //监视大括号内的代码
                state = conn.createStatement();
                a = state.executeUpdate(sql);
            } catch (Exception e) {     //捕获错误
                e.printStackTrace();
            } finally {
                //关闭z    连接
                DBUtil.close(state, conn);
            }
            
            if (a > 0) {
                f = true;
            }
            return f;
        }
}
//DBUtil.java
package ceyan;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class CEConnection {
    
    public static String db_url = "jdbc:mysql://localhost:3306 /roo1?useSSL=false&serverTimezone=GMT";
    public static String db_user = "root";//账号
    public static String db_pass = "123456";//密码
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return conn;
    }
    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();
            }
        }
    }
    
}

 

posted on 2019-12-09 17:21  芥末果酱  阅读(109)  评论(0编辑  收藏  举报