2016-09-13 2 views
0

Java를 사용하여 웹 페이지를 개발하려고했습니다.데이터가 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>

이 내 자바 서블릿 페이지 코드 : 나는 주어진다 등록 페이지의 정보를 저장하기 위해 노력 해왔다

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

이미지 링크 모든게 내가 단지 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

을위한 Mysql 워크 벤치 전체 servelts가 잘못되었거나 다른 똥과 관련된 문제가있다. –

+0

또한 다른 데이터베이스 서블릿에서 데이터베이스를 사용하면서 데이터베이스가 작동합니다. –

+0

몇 가지를 확인할 수 있습니다 : 1. 왜'action = "../ PatientRegistrationServlet"의 점 ('..')이? 2. 서블릿 코드를 디버깅하거나'System.out.println()'을 사용하십시오. 3. 예외가 있으면 로그 파일이나 콘솔을보십시오. 4. HTML 입력 제어 이름 (예 :'First Name ')에는 공백이 허용되지만 제거 할 수 있습니다. 대신에 'firstName'과 같은 이름을 사용하십시오. 그래도 여전히 작동하지 않는다면 나에게 의견을 남기고 오늘 저녁에 직접 시험해 보겠습니다. – ujulu

관련 문제