Home  >  Forum  >  JSP, Servlet
Post New Query

Unable to insert data in database using JSP and Servlet and no exception thrown


joined on
September 16,2014
Asked on September 20,2015
I am creating a registration form in JSP, I have coded the code and when deploy it on server then No error , no output and data is not inserted in database. Can any one solve the my problem.
What is uses Server: Tomcat 8, IDE: Eclipse Luna, Database: Oracle 12c.

index.jsp

    <html>
     <head>
     <title>Registration Form</title>
    </head>
    <body>
<form action="process.jsp" method="post">
<input type="text" name="uname" value="Name..." onclick="this.value" /><br>
<input type="text" name="uemail" value="Email..." onclick="this.value" /><br>
<input type="password" name="upass" value="Password..."
onclick="this.value" /><br> <input type="submit"
value="Register">

</form>

    </body>
    </html>

process.jsp

    <%@ page import="registration.RegisterDao"%>
    <jsp:useBean id="obj" class="registration.User"></jsp:useBean>
    <jsp:setProperty property="*" name="obj" />
    <html>
    <head>
    </head>
    <body>

<%
int status = RegisterDao.register(obj);
if (status > 0)
out.print("You are successfully registered");
%>

    </body>
    </html> 

user.java

    package registration;

    public class User {

private String uname;
private String upass;
private String uemail;

public String getUname() {
return uname;
}

public void setUname(String uname) {
this.uname = uname;
}

public String getUpass() {
return upass;
}

public void setUpass(String upass) {
this.upass = upass;
}

public String getUemail() {
return uemail;
}

public void setUemail(String uemail) {
this.uemail = uemail;
}

}

provider.java

     package registration;

    public interface Provider {

String DRIVER = "oracle.jdbc.driver.OracleDriver";

String CONNECTION_URL = "jdbc:oracle:thin:@localhost:1521:oracle12c";

String USERNAME = "system";
String PASSWORD = "system";

}


connectionProvider.java

     package registration;

    import java.sql.Connection;
    import java.sql.DriverManager;

    import static registration.Provider.*;

    public class ConnectionProvider {

private static Connection con = null;

static {

try {

Class.forName(DRIVER);
con = DriverManager.getConnection(CONNECTION_URL, USERNAME,
PASSWORD);

} catch (Exception ex) {
ex.printStackTrace();
}
}

public static Connection getCon() {
return con;
}

}


Registerdao.java

     package registration;

     import java.sql.Connection;
     import java.sql.PreparedStatement;

    public class RegisterDao {

public static int register(User u) {
int status = 0;

try {

Connection con = ConnectionProvider.getCon();

PreparedStatement ps = con
.prepareStatement("insert into detail values(?,?,?)");

ps.setString(1, u.getUname());
ps.setString(2, u.getUemail());
ps.setString(3, u.getUpass());

} catch (Exception ex) {
ex.printStackTrace();
}
return status;
}

}

Other details are: my table name is DETAIL and column name is NAME, EMAIL, PASS.
joined on
October 12,2013
Replied on September 22,2015

You are missing executeUpdate() statement in your Registerdao  class.
executeUpdate() This method Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

 try {

    Connection con = ConnectionProvider.getCon();

    PreparedStatement ps = con
            .prepareStatement("insert into detail values(?,?,?)");

    ps.setString(1, u.getUname());
    ps.setString(2, u.getUemail());
    ps.setString(3, u.getUpass());

   status = ps.executeUpdate();

  } catch (Exception ex) {
    ex.printStackTrace();
 }
   return status;
}

Write Answer








Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us