简体   繁体   中英

i am trying to check the before inserting the data check the existing data in database

registration.java
    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

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

/**
 *
 * @author srini
 */
@WebServlet(urlPatterns = {"/RegisterServlet"})
public class RegisterServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    public void doGet(HttpServletRequest request,HttpServletResponse response) 
    throws IOException,ServletException{
        processRequset (request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequset (request,response);
    }

    public void processRequset(HttpServletRequest request,HttpServletResponse response) 
            throws IOException,ServletException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String phone =request.getParameter("phone");
        String username =request.getParameter("uname");
        String password =request.getParameter("pass");

       try{  
Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/register","root","toor");  

PreparedStatement pstmt=con.prepareStatement(  
"insert into headwy(phone,uname,pass) values(?,?,?)"); 
  pstmt.setString(1,phone);
 pstmt.setString(2,username);
 pstmt.setString(3,password); 

 int i=pstmt.executeUpdate();  
 if(i>0) 
out.print("You are successfully registered...");  

pstmt.close();
con.close();

}
       catch (Exception e2) {System.out.println(e2);
       }  

out.close();  
}  
}

I am trying to check the existing data in database before inserting the data.

But I don't know how to execute the two SQL statement in servlet program.

Because I am first using sql and servlet.

Please provide the solution or give me the idea for the how to check the aleardy existing data in database.

=> Make store procedure like below sample and then call using java code and get your result in output parameter.

CREATE PROCEDURE InsertName
(
   @username varchar(25), 
   @userpassword varchar(25)
) 
AS
  IF EXISTS(SELECT 'True' FROM MyTable WHERE username = @username)
  BEGIN
   --This means it exists, return it to ASP and tell us
     SELECT 'This record already exists!'
  END
  ELSE
  BEGIN
   --This means the record isn't in there already, let's go ahead and add it
     SELECT 'Record Added'
     INSERT into MyTable(username, userpassword) 
     VALUES(@username,@userpassword)
  END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM