hey buddy's, i'm creating the program of employee details and i will inserted the fields are ADD,UPDATE,DELETE,EXIT,. I hope the all thing to find the exact solutions but, particular one thing only, that was UPDATE and these are the following some tips and code:
1.the input is not going store in database and,
2.the file is not updated. advance congrats for finding the solutions...

<html>
<head>
<title>
UPDATING DETAILS ABOUT EMPLOYEE
</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h3>ENTER THE EMPLOYEE DETAILS</h3><br>

<form action="editEmployee.jsp" name="UpdateForm" method="post">
<table cellspacing="1" cellpadding="1" border="1" WIDTH=90%>
<tr>
<td align="center">YOUR EMPLOYEE ID:</td><td align="center"><B><%= request.getParameter("emp_id")%></B></td>
</tr>
<tr>
<td align="center">EMPLOYEE NAME:</td>
<td align="center"><input type="text" name="EmployeeName"></td>
</tr>
<tr>
<td align="center"> EMPLOYEE NATIONLITY:</td>
<td align="center"><input type="text" name="Nationality"></td>
</tr>
</table><br><br>

<input type="submit" value="UPDATE" name="method" class="button" >
<input type="reset" value="CLEAR">
<input type="button" name="method" value="BACK TO LIST" class="button" onclick="location.href='Employeelist.jsp'"/>
</form>


<%@ page import="java.sql.*" %>
<%@ page import="java.sql.DriverManager.*" %>
<%@ page import="java.sql.Statement.*" %>
<%@ page import="java.io.*" %>

<%
String EmployeeName=request.getParameter("EmployeeName");
int Employee_id=Integer.parseInt(request.getParameter("emp_id"));
String Nationality=request.getParameter("Nationality");

    PreparedStatement ps;
    Connection con=null;

String driverName="com.mysql.jdbc.Driver";
String connectionUrl="jdbc:mysql://localhost:3306/";
String user="root";
String password="sarakrish";
String dbName="employee";

try{
    Class.forName(driverName);
    con=DriverManager.getConnection(connectionUrl+dbName,user,password);
    ps=con.prepareStatement("update employees set EmployeeName=?, Nationlity=? where Employee_id=?");
    ps.setString(1, EmployeeName);
    ps.setInt(2, Employee_id);
    ps.setString(3, Nationality);
    ps.executeUpdate();
    }
catch(ClassNotFoundException e)
    {
    out.print("ClassNotFoundException:"+e.getMessage());
    e.printStackTrace();
    }
%>

</body>
</html>          

Recommended Answers

All 21 Replies

They're not in the right order:

ps=con.prepareStatement("update employees set EmployeeName=?, Nationality=? where Employee_id=?");
ps.setString(1, EmployeeName);
ps.setString(2, Nationality);
ps.setInt(3, Employee_id);

no buddy, nothing can be changed for the o/p.
i will do the mysql table like as EmployeeName,Employee_id,Nationality
this order in my database.

I meant the order of the code snippet. Check my lines with yours, see the difference. The parameters need to be lined up in the same order as in the query.

i ill change that order but its containing same error only occured. the line was,
An exception occurred processing JSP page /WebContent/EDList/WebContent/editEmployee.jsp at line 57

        ps.setString(2, Nationality);
56:     ps.setInt(3, Employee_id);
57:     ps.executeUpdate();
58:     }
59: catch(ClassNotFoundException e)
60:     {

this the error line buddy...

sorry, Nationality only buddy,

An exception occurred processing JSP page /WebContent/EDList/WebContent/editEmployee.jsp at line 38

now the error was:

35: 
36: <%
37: String EmployeeName=request.getParameter("EmployeeName");
38: int Employee_id=Integer.parseInt(request.getParameter("Employee_id"));
39: String Nationality=request.getParameter("Nationality");
40: 
41:     PreparedStatement ps;

this is the code buddy:

<%
String EmployeeName=request.getParameter("EmployeeName");
int Employee_id=Integer.parseInt(request.getParameter("Employee_id"));
String Nationality=request.getParameter("Nationality");

    PreparedStatement ps;
    Connection con=null;

    String driverName="com.mysql.jdbc.Driver";
    String connectionUrl="jdbc:mysql://localhost:3306/";
    String user="root";
    String password="sarakrish";
    String dbName="employee";
try{
    Class.forName(driverName);
    con=DriverManager.getConnection(connectionUrl+dbName,user,password);
    ps=con.prepareStatement("update employees set EmployeeName=?, Nationality=? where Employee_id=?");
    ps.setString(1, EmployeeName);
    ps.setInt(2, Employee_id);
    ps.setString(3, Nationality);

    ps.executeUpdate();
    }
catch(ClassNotFoundException e)
    {
    out.print("ClassNotFoundException:"+e.getMessage());
    e.printStackTrace();
    }
%>

ps.setInt(2, Employee_id);
ps.setString(3, Nationality);

Wrong order again.

I suggest you add:

catch(Exception e)
{
    out.print(e.getMessage());
    e.printStackTrace();
}

to get more information.

commented: patience is a virtue indeed. +13

buddy now im getting error line of:40

37: 
38: <%
39: String EmployeeName=request.getParameter("EmployeeName");
40: int Employee_id=Integer.parseInt(request.getParameter("Employee_id"));
41: String Nationality=request.getParameter("Nationality");
42: 
43:     PreparedStatement ps;

It MIGHT help to know WHAT error you are getting (you will probably have to look at the logs). But what will REALLY help is to STOP WITH THE SCRIPTLETS. Program PROPERLY.

1.buddy i will do some changes then i got that page but its not update the values. i do the updations is default setting in emp_id, that emp_id was constant for that particular updations only.

2.but, now i got this error:Parameter index out of range (3 > number of parameters, which is 2).

3.as the code following as:

<%@ page import="java.sql.*" %>
<%@ page import="java.sql.DriverManager.*" %>
<%@ page import="java.sql.Statement.*" %>
<%@ page import="java.io.*" %>

<html>
<head>
<title>
UPDATING DETAILS ABOUT EMPLOYEE
</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h3>ENTER THE EMPLOYEE DETAILS</h3><br>

<form action="editEmployee.jsp" name="UpdateForm" method="post">
<table cellspacing="1" cellpadding="1" border="1" WIDTH=90%>
<tr>
<td align="center">YOUR EMPLOYEE ID:</td><td align="center"><B><%= request.getParameter("emp_id")%></B></td>
</tr>
<tr>
<tr>
<td align="center">EMPLOYEE NAME:</td>
<td align="center"><input type="text" name="EmployeeName"></td>
</tr>
<tr>
<td align="center"> EMPLOYEE NATIONLITY:</td>
<td align="center"><input type="text" name="Nationality"></td>
</tr>

<%
String EmployeeName=request.getParameter("EmployeeName");

String Nationality=request.getParameter("Nationality");

    PreparedStatement ps;
    Connection con=null;

    String driverName="com.mysql.jdbc.Driver";
    String connectionUrl="jdbc:mysql://localhost:3306/";
    String user="root";
    String password="sarakrish";
    String dbName="employee";
try{
    Class.forName(driverName);
    con=DriverManager.getConnection(connectionUrl+dbName,user,password);
    ps=con.prepareStatement("update employees set EmployeeName=?,Nationality=?");
    ps.setString(1, EmployeeName);
    ps.setString(3, Nationality);
    ps.executeUpdate();
    }
catch(Exception e)
    {
    out.print(e.getMessage());
    e.printStackTrace();
    }
%>


</table><br><br>
<input type="submit" value="UPDATE" name="method" class="button" >
<input type="reset" value="CLEAR">
<input type="button" name="method" value="BACK TO LIST" class="button" onclick="location.href='Employeelist.jsp'"/>
</form>

</body>
</html>          

i ill mark that code in this symbols...

this that page

ps=con.prepareStatement("update employees set EmployeeName=?,Nationality=?");
ps.setString(1, EmployeeName);
ps.setString(3, Nationality);

Uhm, where did the "where clause" portion of the statement go? And how many question marks do you count in this statement? And is the index you used larger than this?

yeah i know that, but i ill doing in the database 1.EmployeeName 2.Employee_id 3.Nationality in this order.
i ill change this order, i got this error:>

Unknown column 'Nationality' in 'field list'

con=DriverManager.getConnection(connectionUrl+dbName,user,password);
        ps=con.prepareStatement("update employees set EmployeeName=?,Nationality=?");
        ps.setString(1, EmployeeName);
        ps.setString(2, Nationality);
        ps.executeUpdate();
        }

Then LOOK at your DB. Is the column REALLY named like that, EXACTLY like that.

And, it doesn't matter in what order the columns were created and/or exist in the db, these index numbers are PARAMETER indexes and are ONLY concerned with the number and position of the parameters (i.e. question marks) in the QUERY.

you would 've done well to first learn the basics of persistence (either jdbc, hibernate, jpa, ... ) before trying to do an entire application.
just the simple insert - edit - delete statements on their own.

I would also have suggested to learn to use servlets before trying to use a database in your application.
keep writing scriplets in your jsp files, and it'll become a lot harder than it needs to be to debug/maintain.

keep writing scriplets in your jsp files, and it'll become a lot harder than it needs to be to debug/maintain.

I have told this guy not to do this multiple times in multiple threads.

k buddy thanks for all comments and replys, i ill do some changes and finally i got the o/p. little much know in scriplets so, i must learn the scriplets. And the code is following as:

<%@ page import="java.sql.*" %>


    <%@ page import="java.sql.DriverManager.*" %>
    <%@ page import="java.sql.Statement.*" %>
    <%@ page import="java.io.*" %>

    <%

        PreparedStatement ps;
        Connection con=null;

        ResultSet rs=null;
        Statement st=null;


        String driverName="com.mysql.jdbc.Driver";
        String connectionUrl="jdbc:mysql://localhost:3306/";
        String user="root";
        String password="sarakrish";
        String dbName="employee";

        String emp_id = "";
        String emp_name = "";
        String nationality = "";
        try{
            Class.forName(driverName);
            con=DriverManager.getConnection(connectionUrl+dbName,user,password);

            if(request.getParameter("edit")!=null && request.getParameter("edit").toString().equals("yes")){
                String EmployeeId = request.getParameter("emp_id");
                String EmployeeName=request.getParameter("EmployeeName");
                String Nationality=request.getParameter("Nationality");

                ps=con.prepareStatement("update employees set EmployeeName=?,Nationlity=? where Employee_id=?");
                ps.setString(1, EmployeeName);
                ps.setString(2, Nationality);
                ps.setString(3, EmployeeId);
                ps.executeUpdate();
                ps.close();
            }
            st = con.createStatement();
            String sql="SELECT * FROM employees where employee_id='"+request.getParameter("emp_id")+"'";
            rs=st.executeQuery(sql);
            while (rs.next()) {
                emp_id = rs.getString(2);
                emp_name = rs.getString(1);
                nationality = rs.getString(3);
                break;
            }       
            rs.close();
            st.close();

            con.close();        
        }catch(Exception e){
            out.print(e.getMessage());
            e.printStackTrace();
        }

    %>

    <html>
    <head>
    <title>
    UPDATING DETAILS ABOUT EMPLOYEE
    </title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <h3>ENTER THE EMPLOYEE DETAILS</h3><br>
    <%=request.getParameter("edit")%>
    <form action="editEmployee.jsp" name="UpdateForm" method="post">
    <input type="hidden" name="edit" id="edit" value="yes">
    <input type="hidden" name="emp_id" id="emp_id" value="<%=emp_id %>">
    <table cellspacing="1" cellpadding="1" border="1" WIDTH=90%>
    <tr>
    <td align="center">YOUR EMPLOYEE ID:</td><td align="center"><B><%= request.getParameter("emp_id")%></B></td>
    </tr>
    <tr>
    <td align="center">EMPLOYEE NAME:</td>
    <td align="center"><input type="text" name="EmployeeName" value="<%=emp_name %>"></td>
    </tr>
    <tr>
    <td align="center"> EMPLOYEE NATIONLITY:</td>
    <td align="center"><input type="text" name="Nationality" value="<%=nationality %>"></td>
    </tr>



    </table><br><br>
    <input type="submit" value="UPDATE" name="method" class="button" >
    <input type="reset" value="CLEAR">
    <input type="button" name="method" value="BACK TO LIST" class="button" onclick="location.href='Employeelist.jsp'"/>
    </form>

    </body>
    </html>          

you mustn't learn the scriplets, you should remove them and work with servlets.

BTW THIS

String sql="SELECT * FROM employees where employee_id='"+request.getParameter("emp_id")+"'";

Is just BEGGING for SQL syntax errors and/or SQL injection attacks.

hey stultuske, hmm yeah buddy, today only ill start, thanks for the comment.

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.