2014-09-20 148 views
-3

我必須做出如何解決它們? 請注意,代碼仍然完整,我輸入了一些打印語句進行檢查。 我的數據庫 數據庫表:sharefile 片劑:會員爲什麼我的代碼不會在mysql中將數據插入數據庫?

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"; 
} 

回答

0

我一直在編寫Java代碼15年,這可能是我看到有人試圖做到這一點的最糟糕的方式。這種方法當時是一個可悲的想法,今天仍然是一個 - 也許更是如此。

停止。現在。

花一些時間,瞭解在Java中使用數據庫的選項(mybatis,hibernate,JPA,除了jsp中的直接jdbc)。

進一步瞭解Java的Web開發和圍繞它的選項(struts,jsf,stripes,除了一堆糟糕的代碼都扔在一個jsp文件中)。

之後,回來再問一次。

+0

謝謝。但我有2個月製作項目Web Share File,就像mediafire一樣,但它比簡單! – 2014-09-20 15:54:59

0

Class.forName(「com.mysql.jdbc.Driver」)。newInstance();

在上面的代碼中...沒有必要創建一個新的實例。 我們所做的基本上是將驅動程序類加載到內存中,而不是創建新的實例。

所以使用像

的Class.forName( 「com.mysql.jdbc.Driver」);

相關問題