2016-09-13 26 views
0

私はJavaを使用してWebページを開発しようとしています。データがMySQLデータベースに挿入されていません

この私のindex.html

<!DOCTYPE html> 
 
<html > 
 
    <head> 
 
    <meta charset="UTF-8"> 
 
    <title>Simple Dark Form</title> 
 
      <link rel="stylesheet" href="css/style.css"> 
 
    </head> 
 

 
    <body> 
 
<form action="../PatientRegistrationServlet" method="post"> 
 
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'> 
 
<link href='http://fonts.googleapis.com/css?family=Sofia' rel='stylesheet' type='text/css'> 
 
<div class='login'> 
 
    <h2>Register</h2> 
 
    <input name='First Name' placeholder='First Name' type='text'> 
 
    <input name='Last Name' placeholder='Last Name' type='text'> 
 
    <input name='ad1' placeholder='Address Line 1' type='text'> 
 
     <input name='Birthdate' placeholder='Birt date' type='text'> 
 
     <input name='ad2' placeholder='Address Line 2' type='text'> 
 
     <input name='Country' placeholder='Country' type='text'> 
 
    <input name='State' placeholder='State' type='text'> 
 
    <input name='city' placeholder='city' type='text'> 
 
    <input name='pcode' placeholder='Pincode' type='text'> 
 
    <input name='Blood group' placeholder='Blood group' type='text'> 
 
    <input name='Email' placeholder='E-Mail' type='text'> 
 
    <input name='Gender' placeholder='Gender' type='text' > 
 
     <input name='mobilno' placeholder='Mobile no' type='text'> 
 
     <input id='pw' name='pw' placeholder='Password' type='password'> 
 

 
<div class='agree'> 
 
    <input id='agree' name='agree' type='checkbox'> 
 
    <label for='agree'></label>Accept rules and conditions 
 
    </div> 
 
    <input class='animated' type='submit' value='Register' "> 
 
    <a class='forgot' href="../cpaneliclinix/login.jsp">Already have an account?</a> 
 
</div> 
 
    </form> 
 
      </body> 
 
</html>

これは私のJavaサーブレットページのコードです:私はで与えられる登録ページの内容を保存しようとしている

package com.iclinix.controller; 

import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 


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


    public PatientRegistrationServlet() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     String fname = request.getParameter("First Name"); 
     String lname = request.getParameter("Last Name"); 
     String addr1 = request.getParameter("ad1"); 
     String bd = request.getParameter("Birthdate"); 
     String addr2 = request.getParameter("ad2"); 
     String cntry = request.getParameter("Country"); 
     String st1 = request.getParameter("State"); 
     String ct = request.getParameter("city"); 
     String pc = request.getParameter("pcode"); 
     String bg = request.getParameter("Blood group"); 
     String mail= request.getParameter("Email"); 
     String gen = request.getParameter("Gender"); 
     String mno = request.getParameter("mobilno"); 
     String pass = request.getParameter("pw");  

      try { 
       Class.forName("com.mysql.jdbc.Driver"); 
       Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/iclinix","root","root"); 
       Statement st = con.createStatement(); 
       st.executeUpdate("INSERT INTO tbl_patient_registration (first_name, last_name, add1, birth_date, add2, country, state, city, pincode, bloodgroup, email, gender, mobileno, password)"+ "values('"+fname+"','"+lname+"','"+addr1+"','"+bd+"','"+addr2+"','"+cntry+"','"+st1+"','"+ct+"','"+pc+"','"+bg+"','"+mail+"','"+gen+"','"+mno+"','"+pass+"')"); 


      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 

} 

MySQLデータベースの形式でユーザー。 MySQLデータベースが接続されています。

Here is screenshot

画像リンク Everthingは私が姓、名、パスワードだけの3つのフィールドで行くことにしようとすると正常に動作しますが、私はより多くのフィールドを追加し、それを実行しようとすると、何もして更新されなかっますデータベース。 サーブレットにエラーはありません。

名前と姓だけで動作します。誰でも助けてくれますか?

+0

例外がスローされないと仮定しているので、コミットしたトランザクションが必要ですか? –

+0

私は基本的に何を意味するのかわかりません。私はこの分野の新しい人です。 –

+0

このページを見るhttps://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html –

答えて

0

あなたは次のことを試してみてください:

String insertStatement = "INSERT INTO tbl_patient_registration (first_name, last_name, add1, birth_date, add2, country, state, city, pincode, bloodgroup, email, gender, mobileno, password) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
PreparedStatement pst = con.prepareStatement(insertStatement); 
pst.setString(1, fname); 
pst.setString(2, lname); 
pst.setString(3, addr1); 
pst.setDate(4, bd); // here I am guessing that the type of the column is date; if I am wrong use setString instead 

... // set the rest of the parameters here 

pst.setString(14, pass); 

pst.executeUpdate(); // execute the statement after setting the params 

それは

  • SQLインジェクションと、
  • が読み取り可能である(私の好み)となりやすい少ない誤差で処理するので、これは文字列の連結よりも優れています。

PreparedStatementの詳細については、JDBC tutorialをご覧ください。

+0

それdoes not work.no..is servelts全体が間違っているか、他のshitsに何か問題があります。 –

+0

また、別のログインサーブレットで私のデータベースを使っているので、私のデータベースも動いています。 –

+0

あなたはいくつかのことを確認できます:1.なぜ 'action =" ../ PatientRegistrationServlet "のドット(' ..')? 2.サーブレットコードをデバッグするか、すべてのパラメータを 'System.out.println()'だけで使用します。3.例外がある場合は、ログファイルまたはコンソールを調べます。 4. HTML入力コントロール名( 'First Name'など)に空白を入れることはできますが、削除します。代わりに 'firstName'のような名前を使用してください。まだ動作しない場合は、私にコメントを残して、私は今日それを夜に試してみます。 – ujulu

関連する問題