2016-08-21 22 views
3

登録ページをデザインするためにHTMLファイルを作成しました。 Submitボタンをクリックすると、フォーム値がJSPページに送信されます。しかし、私はこのプロジェクトを実行すると、フォームを記入した後、空のJSPページが表示され、 "データの入力に失敗しました"というメッセージも表示されません。値はデータベースに入力されません。私はNetbeans IDEを使用しています。 ここに両方の​​ファイルのコードがあります。JSPフォームのデータがmysqlデータベースに入力されていません

addlibform.html:

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Add Librarian</title> 
    <link rel="stylesheet"  href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <link rel="stylesheet" type="text/css" href="background.css"> 
    <link rel="stylesheet" type="text/css" href="main.css"> 
    <style> 
     .label{ 
      font-size: 15px; 
      color: black; 
      position: absolute; 
      margin-left: 500px; 
      margin-top: 100px; 
     } 
     .input{ 
      position: absolute; 
      margin-left: 650px; 
      margin-top: 100px; 
     } 
    </style> 
</head> 
<body> 

    <h1 style="position: absolute; margin-left: 550px;">Add Librarian</h1><br> 
    <form method="post" action="addlib.jsp"> 
     <label for="username" class="label">Username:</label> 

     <input type="text" name="userid" id="userid" required class="input"><br><br><br> 

    <label for="password" class="label">Password:</label> 

    <input type="password" name="password" id="password" class="input"><br><br><br> 

     <label for="address" class="label">Address:</label> 

     <textarea rows="2" cols="22" name="add" class="input"></textarea><br><br><br> 

     <label for="city" class="label">City:</label> 

     <input type="text" name="city" class="input"><br><br><br> 

     <label for="contact" class="label">Contact No.:</label> 

     <input type="text" name="contact" class="input"><br><br><br> 

     <input type="submit" class="btn btn-default" value="Add Librarian" style="position: relative; margin-left: 600px; margin-top: 100px;"> 
     <input class="btn btn-default" value="Back" style="position: relative; margin-left: 600px; margin-top: 20px;">   
    </form> 

</body> 
</html> 

addlib.jspファイル:

<%@ page import ="java.sql.*" %> 
<%@ page import ="java.lang.*" %> 
<% 
String username = request.getParameter("userid"); 
String pwd = request.getParameter("password"); 
String add = request.getParameter("address"); 
String city = request.getParameter("city"); 
String contact = request.getParameter("contact"); 
try{ 
Class.forName("com.mysql.jdbc.Driver"); 
Connection c= DriverManager.getConnection("jdbc:mysql://localhost:3306/librarymgmt", "root", "root"); 
Statement st = c.createStatement(); 

int i=st.executeUpdate("INSERT INTO librarians VALUES('" + username + "','" + pwd + "','" + add + "','" + city + "','" + contact + ")"); 
if(i > 0) 
    System.out.println("Librarian added successfully"); 
else 
    System.out.println("Failed to insert data"); 
} 
catch(Exception e) 
{ 
    System.out.println(e); 
} 
%> 

助けてください。事前に感謝:)

答えて

0

"','" + contact + ")"); 

は括弧を閉じる前に単一引用符を閉じることができません。

これはあなたのエラーです。

しかし、それは無関係です。ここでの問題は、あなたが作った特別なエラーではなく、エラーを見ることができずに、盲目でコーディングしているという事実です。ブラインドでコーディングを続けると、さらにエラーが発生し、何が問題なのかまだ分かりません。私たちはスタックオーバーフローでここに助けてくれるが、ある時点の後、あなたと私たちの両方にとって退屈になり始める。

したがって、例外の例外がスローされた場合(前述のようににスローされた場合)、例外のtoString()が標準出力に出力されます。あなたはコンソールでそれを見ますか?あなたが標準出力に無条件で "hello"を出力すると(例えば、try{,の直前のように)、それは見えますか?そうでない場合は、それが問題です。エラーは効果的に抑制されるため、何がうまくいかないかを知る方法がありません。

どの程度ませは、サーバーがあなたのために例外をキャッチし、サーバーログでと実際のWebページ上の両方の意味のあるエラーメッセージを提供するように、全く例外をキャッチしようとしていますか?

関連する問題