记第一个javaweb网页

  记第一个javaweb网页_第1张图片
记第一个javaweb网页_第2张图片
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here

<%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
%>
         
<%} %>

        

当前位置:添加学生信息

登录账号
登录密码
姓名
学号
电子邮件
所在学院
所在系
所在班级
入学年份
生源地
备注
package Db;

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

public class Db {
    public static String db_url = "jdbc:mysql://localhost:3306/system?serverTimezone=UTC";
    public static String db_user = "root";
    public static String db_pass = "vayne";
    
    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();
            }
        }
    }
}
package Fa;

public class Fa {
    String zhanghao;
    String password;
    String name;
    String sex;
    String stunum;
    String xueyuan;
    String mailbox;
    String xi;
    String classroom;
    String nian;
    String hometown;
    String beizhu;
    public Fa(String zhanghao) {
        this.zhanghao = zhanghao;
    }

    public Fa(String zhanghao, String password, String name, String sex,String stunum,String xueyuan,String mailbox, String xi, String classroom, String nian, String hometown, String beizhu) {

        this.zhanghao = zhanghao;
        this.password = password;
        this.name = name;
        this.sex = sex;
        this.stunum = stunum;
        this.xueyuan = xueyuan;
        this.mailbox = mailbox;
        this.xi = xi;
        this.classroom = classroom;
        this.nian = nian;
        this.hometown = hometown;
        this.beizhu = beizhu;
    }

    public String getzhanghao() {
        return zhanghao;
    }
    public void setzhanghao(String zhanghao) {
        this.zhanghao = zhanghao;
    }
    public String getpassword() {
        return password;
    }
    public void setpassword(String password) {
        this.password = password;
    }
    public String getname() {
        return name;
    }
    public void setage(String name) {
        this.name = name;
    }
    public String getsex() {
        return sex;
    }
    public void setsex(String sex) {
        this.sex = sex;
    }
    public String getstunum() {
        return stunum;
    }
    public void setstunum(String stunum) {
        this.stunum = stunum;
    }
    public String getxueyuan() {
        return xueyuan;
    }
    public void setxueyuan(String xueyuan) {
        this.xueyuan = xueyuan;
    }
    public String getxi() {
        return xi;
    }
    public void setxi(String xi) {
        this.xi = xi;
    }
    public String getclassroom() {
        return classroom;
    }
    public void setclassroom(String classroom) {
        this.classroom = classroom;
    }
    public String getnian() {
        return nian;
    }
    public void setnian(String nian) {
        this.nian = nian;
    }
    public String gethometown() {
        return hometown;
    }
    public void sethometown(String hometown) {
        this.hometown = hometown;
    }
    public String getbeizhu() {
        return beizhu;
    }
    public void setbeizhu(String beizhu) {
        this.beizhu = beizhu;
    }
    public String getmailbox() {
        return sex;
    }
    public void setmailbox(String mailbox) {
        this.mailbox = mailbox;
    }
}
package servelt;

import java.io.IOException;

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 Fa.Fa;
import dao.dao;

@WebServlet("/servlet")
    public class servelt extends HttpServlet {
        private static final long serialVersionUID = 1L;
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            add(request, response);
        
        }
        
        
        private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            String zhanghao=request.getParameter("zhanghao");
            String password=request.getParameter("password");
            String name=request.getParameter("name");
            String sex=request.getParameter("sex");
            String stunum=request.getParameter("stunum");
            String mailbox=request.getParameter("mailbox");
            String xueyuan=request.getParameter("xueyuan");
            String xi=request.getParameter("xi");
            String classroom=request.getParameter("classroom");
            String nian=request.getParameter("nian");
            String hometown=request.getParameter("hometown");
            String beizhu=request.getParameter("beizhu");
            
            
            
            
            Fa f=new Fa( zhanghao,password,name,sex,stunum,xueyuan,mailbox,xi,classroom,nian,hometown,beizhu);
            if(dao.add(f))
            {
                request.setAttribute("message", "添加成功");
                request.getRequestDispatcher("NewFile.jsp").forward(request,response);
            }
            else
            {
                request.setAttribute("message", "添加失败");
                request.getRequestDispatcher("NewFile.jsp").forward(request,response);
            }
        }
    }
package dao;

import java.sql.Connection;
import java.sql.Statement;

import Db.Db;
import Fa.Fa;

public class dao {
    public static boolean add(Fa x) {
        String sql = "insert into biao2(zhanghao,password,name,sex,stunum,xueyuan,mailbox,xi,classroom,nian,hometown,beizhu) values('" + x.getzhanghao() + "','" + x.getpassword() +"','" + x.getname() +"','" + x.getsex() +"','"+ x.getstunum() +"','"+ x.getmailbox() +"','"+ x.getxueyuan() +"','"+ x.getxi() +"','"+ x.getclassroom()+"','"+ x.getnian()+"','"+ x.gethometown()+"','"+ x.getbeizhu()+"')";
        Connection conn = Db.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;
        try {
            state = conn.createStatement();
            a = state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Db.close(state, conn);
        }
        
        if (a > 0) {
            f = true;
        }
        return f;
    }
}

 

你可能感兴趣的:(记第一个javaweb网页)