简体   繁体   English

通过jsp更新数据库表到jsp

[英]Update database table through jsp to jsp

View.jsp视图.jsp

this jsp to fetching the data from the database.这个jsp从数据库中获取数据。

<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更新.jsp

This is the update Query But This is not working and redirecting to the update.jsp but nothing displayed这是更新查询,但这不起作用并重定向到 update.jsp,但未显示任何内容

<%@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 .当我按下 Submit 按钮时,它会将我重定向到 Update.jsp 并且 database 中没有任何更改。

  • You are using Update query instead of Insert query.您正在使用更新查询而不是插入查询。
  • Writing Java code inside JSP not a good practice.在 JSP 中编写 Java 代码不是一个好习惯。

Your update syntax is wrong.您的更新语法错误。 It should look like:它应该看起来像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM