'Update database table through jsp to jsp

View.jsp

this jsp to fetching the data from the database.

<form method="post" action="updatedemo.jsp"> 
 <table width="50%" border="1" align="center">                
   <tr>
     <td width="41%" height="32"><em><strong>name</strong></em></td>
     <td width="59%">                     
        <input type="text" name="name" value="<%= resultset.getString(1) %>">                
    </td>
  </tr>
  <tr>
     <td height="34"><em><strong>lastname</strong></em></td>
     <td>
        <input type="text" name="lastname" value="<%= resultset.getString(2) %>">
     </td>                      
     <input type="submit" name="submit">                    
    </form>                      
    <% 
     } 
     %>
    </BODY>
</HTML>

Update.jsp

This is the update Query But This is not working and redirecting to the update.jsp but nothing displayed

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

<%

String name=request.getParameter("name");
//int no=Integer.parseInt(code);
String lastname=request.getParameter("lastname");
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root", "jack");
Statement st=null;
st=conn.createStatement();




String sqlEDIT="UPDATE INTO demoproject(name,lastname) VALUES(?,?)";
java.sql.PreparedStatement ps=conn.prepareStatement(sqlEDIT);
ps.setString(1,name);
ps.setString(2,lastname);

ps.executeUpdate();
   int i=ps.executeUpdate();
if(i>0)
        {
            out.print("project added into database");
            response.sendRedirect("demo1.jsp");
        }
}
catch(Exception e){
System.out.println(e);
    }
%>

When I pressed the Submit button, it redirected me to Update.jsp and nothing was changed in database .



Solution 1:[1]

  • You are using Update query instead of Insert query.
  • Writing Java code inside JSP not a good practice.

Solution 2:[2]

Your update syntax is wrong. It should look like:

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Annamalai Thangaraj
Solution 2 tmarois