我正在编写此代码以使用可调用接口从我的jsp在mysql数据库上调用存储过程来注册客户,这是存储过程代码

CREATE DEFINER=`root`@`localhost` PROCEDURE `register_customer`(in firstname varchar(45),
in lastname varchar(45),in contactnumber varchar(45),in username varchar(100),
in pass varchar(45),in emailid varchar(45),in gender varchar(45) )
BEGIN
insert into customers (First_Name,Last_Name,Contact_Number,User_Name,Password,Email_Id,Gender)
 values(firstname,lastname,contactnumber,username,pass,emailid,gender);
END


这是用于调用存储过程的Java代码

 <body>
    <h1>Hello Account holder</h1>
    <%!
    public class SignUp{
    String firstname;
    String secondname;
    String Gender;
    String emailid;
    String password;
    String rpassword;
    //connection variables
    PreparedStatement insert=null;
    CallableStatement procstate=null;
    Connection connection=null;
    String URL=null;
    String server_user=null;
    String server_password=null;

    int result=0;
    //constructor
    public SignUp()
    {
        try{
            URL="jdbc:mysql://localhost:3306/onlinestore";
            server_user="root";
            server_password="Bitp0cketer";
            connection=DriverManager.getConnection(URL,server_user,server_password);
           // insert=connection.prepareStatement("Insert into customes(First_Name,Last_Name,User_Name,Password,Email_Id,Gender values('?','?','?','?','?','?'))");
            procstate=connection.prepareCall("{call register_customer(?,?,?,?,?,?)}");

        }
        catch(SQLException e) {
            e.printStackTrace();
        }
    }

    public void  registeruser(String fn,String ln,String un,String pas,String emailid,String gender)
    {
       try{
           //insert=connection.prepareStatement("Insert into customers(First_Name,Last_Name,User_Name,Password,Email_Id,Gender) values('"+fn+"','"+ln+"','"+un+"','"+pas+"','"+emailid+"','"+gender+"')");

           //result=insert.executeUpdate();
           /* storedprocedure calling*/

           procstate.setString(1, fn);
           procstate.setString(2,ln);
           procstate.setString(3,un);
           procstate.setString(4,pas);
           procstate.setString(5,emailid);
           procstate.setString(6,gender);

           result =procstate.executeUpdate();
        }
    catch(SQLException e){
        e.printStackTrace();
        }

    }

    }

    %>
    <%
    SignUp sp=new SignUp();
    String fn;
    String ln;
    String un;
     String pas;
     String emailid;
     String gender;
     fn=request.getParameter("first");
     ln=request.getParameter("last");
     un=request.getParameter("username");
     emailid=request.getParameter("emailid");
     pas=request.getParameter("pas");
     gender=request.getParameter("Gender");
    String cpass=request.getParameter("cpass");
    sp.registeruser(fn, ln, un, pas, emailid, gender);
    %>


没有引发异常,没有错误生成,但是它没有在我的数据库中插入记录,我不知道为什么,请调查一下并帮助我解决问题。

最佳答案

存储过程必须像

procstate = con.prepareCall("{call register_customer(?,?,?,?,?,?)}");
procstate.setString(1, fn);
procstate.setString(2,ln);
procstate.setString(3,un);
procstate.setString(4,pas);
procstate.setString(5,emailid);
procstate.setString(6,gender);
procstate.execute();


更新:在您的存储过程中,您有7个参数。但是您在Java代码中传递了6个参数。 contactnumber丢失

关于mysql - 从jsp在mysql上调用存储过程不会更新我的数据库表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39487427/

10-15 19:49