0

i ill created the program of create, insert, update, delete, exit using in jsp and mysql only. i got the solutions for insert, delete, exit, and create but, update section only i got error., in my DB name is employee and table name is employees, the fields are in the order like 1. employee name, 2. employee_id, 3. nationality,. In update part i'm using the Employee_id is displayed in default, it's also displayed but i got error in 3 field of Nationality. I send my code if you any clear my error,. thanks in advance. the coding 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>
<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(2, 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>         
jmail
  • 5,944
  • 3
  • 21
  • 35
  • Do not use scriptlets `<% %>` in JSP. Write the Servlet for the Java code. See [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1031945) – Aniket Kulkarni Jan 28 '14 at 05:25

2 Answers2

0

hey buddy's thanks for little conversations. I ill do some changes in my program and got the output., As 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>         
jmail
  • 5,944
  • 3
  • 21
  • 35
-1

Assuming this as string , since your Table details are unclear . Pass the arguments as String

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

Also avoid using scriptlets , read through the link Aniket suggested.

Hope it helps !!

Santhosh
  • 8,181
  • 4
  • 29
  • 56