插入之前检查数据库中已存在的数据

时间:2018-03-08 05:12:21

标签: mysql servlets

Register.java

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

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

    @SuppressWarnings("unused")
    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("select * from headwy where unmae=? " );
ResultSet rs = pstmt.executeQuery(); 
if(rs.next()==true)
{
   out.print("username already exist");
}


else {

    String insertquery = "insert into headwwy (phone,uname,pass) values(?,?,?)";
    pstmt.executeQuery("insertquery"); 

pstmt.setString(1,phone);
 pstmt.setString(2,username);
 pstmt.setString(3,password); 


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 and insert the record.In the above try to execute it shows the blank page.
在插入命令工作之前

但是我们添加了select命令它不起作用。

使用select命令检查数据库,如果数据库中存在任何数据,则显示该记录已存在,请选择其他名称。

如果没有找到重复数据,insert命令执行操作并将新记录插入数据库,它会成功地将新记录显示到数据库中。

但是我的代码没有正常工作。

谢谢。

3 个答案:

答案 0 :(得分:0)

您的代码中有错误:

cat

检查您的专栏名称&#34; uname&#34;不是&#34; unmae&#34;。

答案 1 :(得分:0)

您的代码中存在多个问题。首先是一个错字。如果这是失败的原因。

select * from headwy where unmae=? 
insert into headwwy (phone,uname,pass) values(?,?,?)

另外

pstmt.executeQuery("insertquery"); 
设置参数

后应执行

pstmt.setString(1,phone);
pstmt.setString(2,username);
pstmt.setString(3,password); 

我建议您浏览JDBC教程并再次运行。

答案 2 :(得分:0)

在执行preparedstatement之前,您应该在preparedstatement中设置参数。

PreparedStatement pstmt=con.prepareStatement("select * from headwy where unmae=? " );
pstmt.setString(1,username);

ResultSet rs = pstmt.executeQuery(); 

if(rs.next())
{

  System.out.print("username already exist");

}