• java连接MySQL增加信息


    package com.dao;//导入信息

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

    import com.entry.user;
    import com.util.DBUtil;

    public class userDao {
    public boolean add(user user) {
    String sql = "insert into user2(id,password,sex,name,no,mail,xy,x,bj,nf,syd,bz) "
    + "values('" + user.getId() + "','" + user.getPassword() + "','" + user.getSex() + "','" + user.getName() + "','" + user.getNo() + "','" + user.getMail() + "','" + user.getXy() + "','" + user.getX() + "','" + user.getBj() + "','" + user.getNf() + "','" + user.getSyd() + "','" + user.getBz() + "')";
    Connection conn = DBUtil.getConn();
    Statement state = null;
    boolean f = false;
    int a = 0;

    try {
    state = conn.createStatement();
    state.executeUpdate(sql);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    DBUtil.close(state, conn);
    }

    if (a > 0) {
    f = true;
    }
    return f;
    }
    public boolean id(String id) {
    boolean f = false;
    String sql = "select id from user2 where name = '" + id + "'";
    Connection conn = DBUtil.getConn();
    Statement state = null;
    ResultSet rs = null;
    try {
    state = conn.createStatement();
    rs = state.executeQuery(sql);
    while (rs.next()) {
    f = true;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    DBUtil.close(rs, state, conn);
    }
    return f;
    }
    }

    输入要的信息变量

    package com.entry;

    public class user {
    private String id;
    private String name;
    private String password;
    private String no;
    private String mail;
    private String xy;
    private String sex;
    private String x;
    private String bj;
    private String nf;
    private String syd;
    private String bz;


    public String getId() {
    return id;
    }
    public String getX() {
    return x;
    }
    public void setX(String x) {
    this.x = x;
    }
    public String getBj() {
    return bj;
    }
    public void setBj(String bj) {
    this.bj = bj;
    }
    public String getNf() {
    return nf;
    }
    public void setNf(String nf) {
    this.nf = nf;
    }
    public String getSyd() {
    return syd;
    }
    public void setSyd(String syd) {
    this.syd = syd;
    }
    public String getBz() {
    return bz;
    }
    public void setBz(String bz) {
    this.bz = bz;
    }
    public void setId(String id) {
    this.id = id;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getSex() {
    return sex;
    }
    public void setSex(String sex) {
    this.sex = sex;
    }
    public String getNo() {
    return no;
    }
    public void setNo(String no) {
    this.no = no;
    }
    public String getMail() {
    return mail;
    }
    public void setMail(String mail) {
    this.mail = mail;
    }
    public user(){}
    public user(String name, String password) {
    this.name = name;
    this.password = password;
    };

    public user(String id, String name, String password, String no, String mail, String xy, String sex, String x, String bj,
    String nf, String syd, String bz) {
    super();
    this.id = id;
    this.name = name;
    this.password = password;
    this.no = no;
    this.mail = mail;
    this.xy = xy;
    this.sex = sex;
    this.x = x;
    this.bj = bj;
    this.nf = nf;
    this.syd = syd;
    this.bz = bz;
    }
    public String getXy() {
    return xy;
    }
    public void setXy(String xy) {
    this.xy = xy;
    }
    }

    检测是否导入

    package com.service;

    import com.dao.userDao;
    import com.entry.user;

    public class userservice {
    userDao cDao = new userDao();


    public boolean add(user user) {
    boolean f = false;
    if(!cDao.id(user.getId())) {
    cDao.add(user);
    f = true;
    }
    return f;
    }
    }

    package com.servlet;

    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 com.entry.user;
    import com.service.userservice;


    @WebServlet("/AddServlet")
    public class AddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    userservice service = new userservice();
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    String method = req.getParameter("method");
    if("add".equals(method))
    doGet(req,resp);

    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    String id = request.getParameter("id");
    String password = request.getParameter("password");
    String sex = request.getParameter("sex");
    String name = request.getParameter("name");
    String no = request.getParameter("no");
    String mail = request.getParameter("mail");
    String xy = request.getParameter("xy");
    String x = request.getParameter("x");
    String bj = request.getParameter("bj");
    String nf = request.getParameter("nf");
    String syd = request.getParameter("syd");
    String bz = request.getParameter("bz");
    user user= new user(id, name, password, no, mail, xy, sex, x, bj,
    nf, syd, bz);
    System.out.println(id);
    if(service.add(user)) {
    request.setAttribute("message");
    request.getRequestDispatcher("NewFile.jsp").forward(request,response);
    } else {
    request.setAttribute("message");
    request.getRequestDispatcher("NewFile.jsp").forward(request,response);
    }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    doGet(request, response);
    }

    }

    连接数据库

    package com.util;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    public class DBUtil {

    public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
    public static String db_user = "root";
    public static String db_pass = "root";

    public static Connection getConn () {
    Connection conn = null;

    try {
    Class.forName("com.mysql.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();
    }
    }
    }

    public static void main(String[] args) throws SQLException {
    Connection conn = getConn();
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String sql ="select * from user";
    pstmt = conn.prepareStatement(sql);
    rs = pstmt.executeQuery();
    if(rs.next()){
    System.out.println("添加成功");
    }else{
    System.out.println("添加失败");
    }
    }
    }

    做注册界面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <title>Insert title here</title>
    <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">
    <h6 style="color: black;">注册</h6>

    <form name = "form1" action="AddServlet?method=add" method="post" onsubmit="return check_submit()">
    <table>
    <tr>
    <td>登录账号</td>
    <td colspan="2"> <input type="text" id="id" name="id" maxlength="12" onblur="blur_id()" onfocus="focus_id()"/></td>
    <td width="300"><div id="result"></td>
    </tr>
    <tr>
    <td>登录密码</td>
    <td colspan="2"> <input type="password" id="password" name="password" onblur="blur_pass()" onfocus="focus_pass()" /></td>
    <td width="300"><div id="result1"></td>
    </tr>
    <tr>
    <td>性别:</td>
    <td colspan="2">
    <select name="sex"id="sex" >
    <option value="男" >男</option>
    <option value="女" >女</option>
    </select></td>
    </tr>
    <tr>
    <td>姓名</td>
    <td colspan="2"><input type="text" id="name" name="name" /></td>
    </tr>
    <tr>
    <td>学号</td>
    <td colspan="2"><input style="ime-mode:Disabled" maxlength="8" type="text" id="no" name="no" onblur="blur_phone()" onfocus="focus_phone()"/></td>
    <td width="300"><div id="result_name"></td>
    </tr><tr>
    <td>电子邮件</td>
    <td colspan="2"><input type="email" id="mail" name="mail" ></td>
    </tr>
    <tr>
    <td>所在学院</td>
    <td colspan="2"><input type="text" id="xy" name="xy" ></td>
    </tr>
    <tr>
    <td>所在系</td>
    <td colspan="2"><input type="text" id="x" name="x" ></td>
    </tr>
    <tr>
    <td>所在班级</td>
    <td colspan="2"><input type="text" id="bj" name="bj" ></td>
    </tr>
    <tr>
    <td>入学年份(届):</td>
    <td colspan="2">
    <select name="nf"id="nf" >
    <option value="2017" >2017</option>
    <option value="2018" >2018</option>
    </select></td>
    </tr>
    <tr>
    <td>生源地:</td>
    <td colspan="2"><input type="text" id="syd" name="syd" ></td>
    </tr>
    <tr>
    <td>备注:</td>
    <td colspan="2"><input type="text" id="bz" name="bz" ></td>
    </tr>

    <tr>
    <td colspan="3"> <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <button type="reset" class="b">重&nbsp;&nbsp;&nbsp;置</button></td>

    </tr>
    </table>
    </form>
    </div>
    <script type="text/javascript">
    /*
    表单验证
    */
    var flag = false; // flag 如果为true(即用户名合法)就允许表单提交, 如果为false(即用户名不合法)阻止提交
    function focus_pass()
    {
    var nameObj = document.getElementById("result1");
    nameObj.innerHTML = "由八位以上字符和数字组成";
    nameObj.style.color="#999";
    }
    function blur_pass()
    {
    var nameObj = document.getElementById("result1");
    // 判断用户名是否合法
    var str2 = check_user_pass(document.form1.password.value);
    nameObj.style.color="red";
    if ("密码合法" == str2)
    {
    flag = true;
    nameObj.innerHTML = str2;
    }
    else
    {
    nameObj.innerHTML = str2;
    }
    }

    function check_user_pass(str)
    { var str2 = "密码合法";
    if ("" == str)
    {
    str2 = "密码为空";
    return str2;
    }
    else if (str.length<8)
    {
    str2 = "密码应为8位组成";
    return str2;
    }
    else if (!check_word(str))
    {
    str2 = "未含有英文字符";
    return str2;
    }

    return str2;
    }

    下载GDBC复制到lib项目下

  • 相关阅读:
    03点云文件常用格式转换(pcd,txt,ply,obj,stl)
    04点云数据结构格式
    vs2015 +ZXing/Zbar的环境配置
    01PCL 点云+vs2015在win10下的环境配置
    07点云的滤波与分割
    gitlabCI/CD部署一个java项目
    k8s 为什么需要数据卷
    gitlab Runner 安装与部署
    gitlab ci/cd介绍
    k8s emptyDir临时数据卷
  • 原文地址:https://www.cnblogs.com/yyl141/p/11716739.html
Copyright © 2020-2023  润新知