不能插入数据到mysql表中

问题描述 投票:1回答:2
    if(request.getParameter("btn_register")!=null)  //check condition button register click event not null "btn_register"
    {
        String firstname,lastname,email,password,role; //create blank bariable

        firstname=request.getParameter("txt_firstname");    //textbox name "txt_fristname"
        lastname=request.getParameter("txt_lastname");  //textbox name "txt_lastname"
        email=request.getParameter("txt_email");    //textbox name "txt_email"
        password=request.getParameter("txt_password");  //textbox name "txt_password"
        role=request.getParameter("txt_role"); //select option name "txt_role"

        String dburl="jdbc:mysql://localhost:3306/jsp_multiuser_login_db"; //database url string the "jsp_multiuser_login_db" is database name
        String dbuname="root"; //database username   
        String dbpwd ="root"; //database password

        try
        {
            Class.forName("com.mysql.jdbc.Driver"); //load driver
            Connection con=DriverManager.getConnection(dburl,dbuname,dbpwd); //create connection

            PreparedStatement pstmt=null; //create statement

            pstmt=con.prepareStatement("SELECT * FROM tbl_user WHERE email=? "); //sql select query
            pstmt.setString(1,email); 
            ResultSet rs=pstmt.executeQuery(); //execute query and set in ResultSet object "rs".

            if(rs.next())
            {
                String checkEmail=rs.getString("email");

                if(email.equals(checkEmail))    //check condition email already exist from user email
                {
                    request.setAttribute("errorMsg", "sorry email already exist"); //error message for email already exist
                }
            }
            else
            {
                pstmt=con.prepareStatement("INSERT INTO tbl_user(firstname,lastname,email,password,role) VALUES(?,?,?,?,?)"); //sql insert query
                pstmt.setString(1,firstname);
                pstmt.setString(2,lastname);
                pstmt.setString(3,email);           //set all variables
                pstmt.setString(4,password);
                pstmt.setString(5,role);
                pstmt.executeUpdate(); //execute query

                request.setAttribute("successMsg", "register successfully please login account"); //register successfully message
            }

            pstmt.close();  //close statement 
            con.close(); //close connection
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
%>

我的工作台SQL数据库中无法插入数据。我的数据库名称是jsp_multiuser_login_db,里面有一个叫tbl_user的表,有6个列:id、firstname、lastname、email、password和role。///////

java sql jsp jdbc netbeans
2个回答
0
投票

当执行时,只需尝试

pstmt.execute(); //execute query

0
投票

我运行了你的代码,一切正常。Mysql数据库

有两个可能的问题会阻止您的代码提交到数据库。

  1. 无效凭证或无法连接,请检查。con.isClosed() 检查你是否有开放的连接,请验证你的导入库为java.sql,如下:
  2. Invalid library.请验证你导入的java.sql如下。

    • import java.sql.Connection;
    • import java.sql.DriverManager;
    • import java.sql.PreparedStatement;
    • import java.sql.ResultSet;
© www.soinside.com 2019 - 2024. All rights reserved.