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();
}
}
%>
I'm unable to insert data into my workbench SQL database. My database name is jsp_multiuser_login_db and there's a table inside it called tbl_user with 6 columns: id, firstname,lastname,email,password and role. //////////////
When executing try just
pstmt.execute(); //execute query
I run your code and everything worked fine: Mysql Database
There are two possible issues may be preventing your code from submitting to database:
con.isClosed()
to check if you have open connection.Invalid library. please validate your imports as java.sql as follows:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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.