<%-- 
    Document   : response
    Created on : Jan 20, 2010, 11:34:37 AM
    Author     : srishi
--%>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>employee data entry</title>

    </head>
    <body>
        <form action="response.jsp" method="get">
        <center>Employee Data Entry Page</center>
        <table border ="0">
            <tr>
                <td>
                    <br>
                    <h5>Employee Id(India)</h5>
                </td>
                <td><input type="text"  name ="emp_idin"size="15" id="tb">
                </td>
            <br>

            <tr>
                <td>
                    <h5>Employee Id(USA)</h5>
                </td>
                <td><input type="text"  name ="emp_idus"size="15" id="textbox1">
                </td>
            </tr>
            <br>

            <tr>
                <td>
                    <h5>Employee Name</h5>
                </td>
                <td><input type="text" name ="emp_name" size ="40" id="textbox2">
                    <br>
                </td>
            </tr>
            <tr>
                <td>
                    <h5>Designation</h5>
                </td><td><input type="text" name ="emp_des" size="40" id="textbox3">
                </td></tr>
            <tr>
                <td>
                    <h5>Department</h5></td><td><input type="text" name ="emp_dept" size="40" id="textbox4">
                </td></tr>
        </table>
       <input type="submit" value="submit" name="submit"/>
       <%
   String emp_idin = request.getParameter("emp_idin");
   String emp_idus = request.getParameter("emp_idus");
   String emp_name = request.getParameter("emp_name");
   String emp_des = request.getParameter("emp_name");
   String emp_dept = request.getParameter("emp_dept");


   /* Create string of connection url within specified
   format with machine name,
    port number and database name. Here machine name id
    localhost and database name is student. */
    String connectionURL = "jdbc:odbc:empdsn";

    // declare a connection by using Connection interface
    Connection connection = null;

    // declare object of Statement interface that uses for executing sql statements.
    
    PreparedStatement pstatement = null;

    // Load JBBC driver "com.mysql.jdbc.Driver"
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

    int updateQuery = 0;


	 // check if the text box is empty
	 if(emp_idin!=null && emp_idus!=null && emp_name!=null && emp_des!=null && emp_dept!=null){

		 // check if the text box having only blank spaces
	     if(emp_idin!="" && emp_idus!="" && emp_name!="" && emp_des!="" && emp_dept!="") {

            try {
              /* Create a connection by using getConnection()
              method that takes parameters of string type
              connection url, user name and password to connect
		to database. */
              connection = DriverManager.getConnection(connectionURL, "", "");

              // sql query to insert values in the secified table.
              String queryString = "INSERT INTO emp_mast(emp_idin,emp_idus,emp_name,emp_des,emp_dept) VALUES (?, ?, ?)";

	      /* createStatement() is used for create statement
              object that is used for
		sending sql statements to the specified database. */
              pstatement = connection.prepareStatement(queryString);
              pstatement.setString(1,emp_idin);
              pstatement.setString(2,emp_idus);
              pstatement.setString(3,emp_name);
              pstatement.setString(4,emp_des);
              pstatement.setString(5,emp_dept);
              updateQuery = pstatement.executeUpdate();

              if (updateQuery != 0) { %>
	           <br>
	           <table border="0">
		      <tr><th>Data is inserted successfully
                    in database.</th></tr>
		   </table>
              <%
              }
            }
            catch (Exception ex) {
            out.println("Unable to connect to Database.");


            }
            finally {
                // close all the connections.
                pstatement.close();
                connection.close();
            }
	  }
	}
%>

       </form>
    </body>
</html>

the problem with this code is this whn i run the project its executing very good but on entereing the data ... its goin directly on the exception and printing unable tyo connect database

Recommended Answers

All 3 Replies

and i am using netbeans ide 6.8 for the development ... and i tried it with mysql and ms access both

print the stack trace. How do you propose to find out what is going wrong when you ignore the cause.

In any case, get all of this scriptlet BS out of this JSP's. Create Beans and other helper classes and use those, don't use scriptlets, especially not ones of this size.

And use the web containers connection pool. Opening a new connection with every request is the pinnacle of inefficience.

<%-- 
    Document   : response
    Created on : Jan 20, 2010, 11:34:37 AM
    Author     : srishi
--%>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>employee data entry</title>

    </head>
    <body>
        <form action="response.jsp" method="get">
        <center>Employee Data Entry Page</center>
        <table border ="0">
            <tr>
                <td>
                    <br>
                    <h5>Employee Id(India)</h5>
                </td>
                <td><input type="text"  name ="emp_idin"size="15" id="tb">
                </td>
            <br>

            <tr>
                <td>
                    <h5>Employee Id(USA)</h5>
                </td>
                <td><input type="text"  name ="emp_idus"size="15" id="textbox1">
                </td>
            </tr>
            <br>

            <tr>
                <td>
                    <h5>Employee Name</h5>
                </td>
                <td><input type="text" name ="emp_name" size ="40" id="textbox2">
                    <br>
                </td>
            </tr>
            <tr>
                <td>
                    <h5>Designation</h5>
                </td><td><input type="text" name ="emp_des" size="40" id="textbox3">
                </td></tr>
            <tr>
                <td>
                    <h5>Department</h5></td><td><input type="text" name ="emp_dept" size="40" id="textbox4">
                </td></tr>
        </table>
       <input type="submit" value="submit" name="submit"/>
       <%
   String emp_idin = request.getParameter("emp_idin");
   String emp_idus = request.getParameter("emp_idus");
   String emp_name = request.getParameter("emp_name");
   String emp_des = request.getParameter("emp_name");
   String emp_dept = request.getParameter("emp_dept");


   /* Create string of connection url within specified
   format with machine name,
    port number and database name. Here machine name id
    localhost and database name is student. */
    String connectionURL = "jdbc:odbc:empdsn";

    // declare a connection by using Connection interface
    Connection connection = null;

    // declare object of Statement interface that uses for executing sql statements.
    
    PreparedStatement pstatement = null;

    // Load JBBC driver "com.mysql.jdbc.Driver"
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

    int updateQuery = 0;


	 // check if the text box is empty
	 if(emp_idin!=null && emp_idus!=null && emp_name!=null && emp_des!=null && emp_dept!=null){

		 // check if the text box having only blank spaces
	     if(emp_idin!="" && emp_idus!="" && emp_name!="" && emp_des!="" && emp_dept!="") {

            try {
              /* Create a connection by using getConnection()
              method that takes parameters of string type
              connection url, user name and password to connect
		to database. */
              connection = DriverManager.getConnection(connectionURL, "", "");

              // sql query to insert values in the secified table.
              String queryString = "INSERT INTO emp_mast(emp_idin,emp_idus,emp_name,emp_des,emp_dept) VALUES (?, ?, ?)";

	      /* createStatement() is used for create statement
              object that is used for
		sending sql statements to the specified database. */
              pstatement = connection.prepareStatement(queryString);
              pstatement.setString(1,emp_idin);
              pstatement.setString(2,emp_idus);
              pstatement.setString(3,emp_name);
              pstatement.setString(4,emp_des);
              pstatement.setString(5,emp_dept);
              updateQuery = pstatement.executeUpdate();

              if (updateQuery != 0) { %>
	           <br>
	           <table border="0">
		      <tr><th>Data is inserted successfully
                    in database.</th></tr>
		   </table>
              <%
              }
            }
            catch (Exception ex) {
            out.println("Unable to connect to Database.");


            }
            finally {
                // close all the connections.
                pstatement.close();
                connection.close();
            }
	  }
	}
%>

       </form>
    </body>
</html>

the problem with this code is in response.jsp what coding must be given.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.