为什么我的代码不在mysql中向数据库插入数据?

时间:2014-09-20 15:37:48

标签: java mysql

我必须如何修复它们? 请注意,代码仍然完整,我已输入一些打印语句进行检查。 我在数据库中的表 数据库:sharefile 平板电脑:会员

id,mem_first_name,mem_last_name,mem_email,mem_password,mem_birthday

singup.jsp

    <%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="jsp.*,java.util.*,java.sql.*"%>
    <%!
    public static String validateEmail(String email){
        String error = "";
        if(email.isEmpty()){
            error ="This field is required.";
        }else if(email.length() > 32)
        {

            error ="This field cannot exceed the length of 32.";
        }else if(email.matches("\\w+(\\.\\w+)*@\\w+\\.\\w(2,4)")){
            error = "Invalid email.";
        }
        return error;
    }
    public static String validatePassword(String password){
        String error = "";
        if(password.isEmpty()){
            error ="This field is required.";
        }else if(password.length() > 24){

            error ="This field cannot exceed the length of 24.";
        }
        return error;
    }
    public static String validateBirthday(String birthday){
        String error = "";
        if(birthday.isEmpty()){
            error ="This field is required.";
        }else if(!birthday.matches("\\d{2}\\/\\d{2}/\\d{4}")) {
            error = "Invalid date format. MM/dd/yyyy";


        }else{
            try{
                java.util.Date d = new SimpleDateFormat("MM/dd/yyyy").parse(birthday);
                if(d.after(new java.util.Date())){
                    error = "Invalid date.";
                }
            }
            catch(Exception e){
                error = "Invalid date.";
            }

        }
        return error;
    }

        public static long insertUser(Connection c, String fname, String lname, String email, String password, String birthday){

            long id = -1;

            try{
            PreparedStatement ps = c.prepareStatement("INSERT INTO members (mem_first_name,mem_last_name,mem_email,mem_password,mem_birthday) VALUES(?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
                ps.setString(1,fname);
                ps.setString(2,lname);
                ps.setString(3,email);
                ps.setString(4,password);
                java.sql.Date d = new java.sql.Date(new SimpleDateFormat("MM/dd/yyyy").parse(birthday).getTime());
                ps.setDate(5,d);
                ps.execute();
                ResultSet rs = ps.getGeneratedKeys();
                if(rs.next()){
                    id = rs.getLong(1);
                }
                rs.close();
                ps.close();
            }catch(Exception e){

            }
            return id;
        }

    %>

    <%
String email = request.getParameter("email");
String fname = request.getParameter("first_name");
String lname = request.getParameter("last_name");
    String password = request.getParameter("password");
    String birthday = request.getParameter("birthday");
    String email_error = "",password_error = "",birthday_error = "";
    if(email==null)
    {
        email = "";
        password = "";
        birthday = "";
        fname = "";
        lname = "";

    }else{
        email_error = validateEmail(email);
        password_error = validatePassword(password);
        birthday_error = validateBirthday(birthday);
        if(email_error.isEmpty() && password_error.isEmpty() || birthday_error.isEmpty())
        {
            Connection c = MySQL.connect();
            if(c!=null){
                long id = insertUser(c,email,password,fname,lname,birthday);   
                if(id!=1){
                    session.setAttribute(Template.USER_ID, id);
                }else{
                    email_error = "That email is already in use.";
                }
                }
        }

    }

    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<link type="text/css" rel="stylesheet" href="css/jquery-ui.css">
<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="css/bootstrap-theme.min.css">
<link type="text/css" rel="stylesheet" href="css/style.css">
<script type="text/javascript">
$(function() {
    $("#birthday").datepicker();
});

</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

</head>
<body>
<div class="container">
        <div class="row centered-form">
        <div class="col-xs-12 col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4">
            <div class="panel panel-default">
                <div class="panel-heading">
                        <h3 class="panel-title">Please sign up <small>It's free!</small></h3>
                        </div>
                        <div class="panel-body">
                        <form role="form">
                            <div class="row">
                                <div class="col-xs-6 col-sm-6 col-md-6">
                                    <div class="form-group">
                            <input type="text" name="first_name" id="first_name" class="form-control input-sm" placeholder="First Name">
                                    </div>
                                </div>
                                <div class="col-xs-6 col-sm-6 col-md-6">
                                    <div class="form-group">
                                        <input type="text" name="last_name" id="last_name" class="form-control input-sm" placeholder="Last Name">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <input type="email" name="email" id="email" class="form-control input-sm" placeholder="Email Address" value="<%=email%>"><%=email_error %>
                            </div>

                            <div class="row">
                                <div class="col-xs-6 col-sm-6 col-md-6">
                                    <div class="form-group">
                                        <input type="password" name="password" id="password" class="form-control input-sm" placeholder="Password"><%=password_error %>
                                    </div>
                                </div>

                            </div>
                            <div class="form-group">
                                <input type="text" name="birthday" id="birthday" class="form-control input-sm" placeholder="Birthday" value="<%=birthday%>"><%=birthday_error %>
                            </div>
                            <input type="submit" value="Register" class="btn btn-info btn-block">

                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

MySQL.java

    package jsp;

import java.sql.*;

public class MySQL {
    public static Connection connect() {
    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        return DriverManager.getConnection("jdbc:mysql://localhost/sharefile","root","123456");
    }catch(Exception e){
        return null;

    }

    }
    public static boolean close(Connection c){
        try{
            c.close();
            return true;
        }catch(Exception e){
            return false;
        }
    }
}

Template.java

    package jsp;

public class Template {

    public static final String USER_ID = "id";
}

2 个答案:

答案 0 :(得分:0)

我已经编写了15年的Java代码,这可能是我看到有人试图实现这一目标的最糟糕的方式。这种方法是一个悲惨的想法,今天仍然是一个 - 可能更多。

停止。现在

花点时间了解在Java中使用数据库的选项(mybatis,hibernate,JPA,jsp中的直接jdbc除外)。

了解更多信息并了解Java中的Web开发及其周围的选项(struts,jsf,stripe,除了在jsp文件中转储的一堆蹩脚代码)。

之后,回来再问一遍。

答案 1 :(得分:0)

的Class.forName(&#34; com.mysql.jdbc.Driver&#34)。的newInstance();

在上面的代码中......不需要创建新实例。 我们所做的基本上是将驱动程序类加载到内存中而不是创建新实例。

所以请使用

的Class.forName(&#34; com.mysql.jdbc.Driver&#34);