如何从jsp页面获取id到servlet?

时间:2014-02-19 09:42:16

标签: java mysql spring jsp servlets

我有一个在网站上注册的用户列表,当管理员点击批准时,它必须获取id,然后将该行移动到另一个名为login的表...现在问题来自jsp如何获取id在servlet中,它可以传递给服务和DAO层的方法。

以下是我的jsp:

<tr bgcolor="white">
<td><b>id</b></td>
<td><b>FirstName</b></td>
<td><b>LastName</b></td>
<td><b>Gender</b></td>
<td><b>Category</b></td>
<td><b>DateOfBirth</b></td>
<td><b>Age</b></td>
<td><b>Address</b></td>
<td><b>Country</b></td>
<td><b>State</b></td>
<td><b>City</b></td>
<td><b>PinCode</b></td>
<td><b>EmailId</b></td>
<td><b>ContactNo</b></td>
<td><b>MobileNo</b></td>
</tr>


 <c:forEach items="${clients}" var="client">
 <tr>
            <td><c:out value="${client.id}"/></td>
            <td><c:out value="${client.firstName}"/></td>
            <td><c:out value="${client.lastName}"/></td>
            <td><c:out value="${client.gender}"/></td>
            <td><c:out value="${client.category}"/></td>
            <td><c:out value="${client.date}"/></td>
            <td><c:out value="${client.age}"/></td>
            <td><c:out value="${client.address}"/></td>
            <td><c:out value="${client.country}"/></td>
            <td><c:out value="${client.state}"/></td>
            <td><c:out value="${client.city}"/></td>
            <td><c:out value="${client.pinCode}"/></td>
            <td><c:out value="${client.emailId}"/></td>
            <td><c:out value="${client.contactNo}"/></td>
            <td><c:out value="${client.mobileNo}"/></td>
            <td><a href="ClientApproveServlet?id=${client.id}">APPROVE</a></td> // I have fetched the id here
            <td><a href="Reject.jsp">REJECT</a></td>
            <td><a href="OnHold.jsp">ONHOLD</a></td>

            </tr>
      </c:forEach>

Stack TRace:

java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
    at com.affiliate.DAO.ClientApproveDAO.insertClient(ClientApproveDAO.java:28)
    at com.affiliate.service.ClientApproveService.clientApprove(ClientApproveService.java:12)
    at com.affiliate.servlet.ClientApproveServlet.doGet(ClientApproveServlet.java:35)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2812)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
    at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
    at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
    at com.affiliate.DAO.ClientApproveDAO.insertClient(ClientApproveDAO.java:23)
    ... 23 more

7 个答案:

答案 0 :(得分:1)

嘿,我决定改变我的DAO:

public class ClientApproveDAO {

        private DataSource dataSource;
        public void setDataSource(DataSource dataSource) {
       this.dataSource = dataSource;
    }
    Connection conn=null;
    PreparedStatement statement=null;
    ResultSet rs=null;


    public void insertClient(int id) {    
        try{
            conn=dataSource.createConnection();
            PreparedStatement ps=conn.prepareStatement("insert into login(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

            ps.executeUpdate("insert into login(id,FirstName,LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo)select * from register where id="+id+"");
            ps.setInt(1, id);

        }
        catch (SQLException e) {
            throw new RuntimeException(e);

    } finally {
            if (rs != null) try { rs.close(); } catch (SQLException ignore) {}
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
        if (conn != null) try { conn.close(); } catch (SQLException ignore) {}
    }


    }

}

感谢所有人的帮助,这是我的最终代码,它的工作非常精细,并且符合要求。

答案 1 :(得分:0)

删除${client.id}

之间的空格
<a href="ClientApproveServlet?id= $ {client.id}">
                                   ↑  

应该是

<a href="ClientApproveServlet?id=${client.id}">

更新

这意味着我们克服了NumberFormat。现在,错误在insertClient()方法中。

我可以从异常中说插入语句的语法错误。

只需添加条件where id=?,因为select * from register会获取所有记录。

PreparedStatement ps=conn.prepareStatement("insert into login(id,FirstName,
     LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,
     PinCode,EmailId,ContactNo,MobileNo)select * from register where id=?");
ps.setInt(1, id);

答案 2 :(得分:0)

只需检查您的SQL查询。我的观点是你的列数不匹配。尽量给予

select id,firstname... etc 

而不是

select * from tablename 

答案 3 :(得分:0)

我认为你在$ {client.id}中有一个空格,只需删除它就可以了。

 <a href="ClientApproveServlet?id= ${client.id}">

我认为你查询的格式也不太好试试:

 String query ="insert into login(id,FirstName,LastName,Gender,Category,Dateofbirth,Age,"
 " Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo)"+
 " select id,..,..,.. FROM register; ";

答案 4 :(得分:0)

您必须在以下位置更改插入代码:

insert into login (FirstName,LastName,Gender,Category,

                     Dateofbirth,Age,Address,Country,State,city,PinCode,

                     EmailId,ContactNo,MobileNo)values(?,?,?,?,?,?,?,?,?,?);

您应该像以下一样检查此用户模型:

private int userid;
public int getUserid()
{
return userid;  
}
public void setUserid(int userid)
{
this.userid=userid; 
}

您应该为插入类型更改此类型:

public void addUser(User user)
    {
        try
        {
            PreparedStatement preparedStatement=connection.prepareStatement("insert into users (firstname,lastname,dob,email)values(?,?,?,?)");

            preparedStatement.setString(1, user.getFirstName());
            preparedStatement.setString(2, user.getLastName());
            preparedStatement.setDate(3, new java.sql.Date(user.getDob().getTime()));
            preparedStatement.setString(4, user.getEmail());
            preparedStatement.executeUpdate();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    } 

答案 5 :(得分:0)

好的,你可以试试这个

执行此行之前

public void insertClient(int id) {    
        try{
            conn=dataSource.createConnection();
            PreparedStatement ps=conn.prepareStatement("insert into login(id,FirstName,LastName,Gender,Category,Dateofbirth,Age,Address,Country,State,city,PinCode,EmailId,ContactNo,MobileNo)select * from register");
            ps.setInt(1, id);
            ps.executeUpdate();
        }
        catch (SQLException e) {
            throw new RuntimeException(e);

将此添加到其中

 String x=null;
 Resultset rs=conn.prepareStatement("select id from old_table where FirstName='request.getParameter('firstname')'and LastName='request.getParameter('lastname')'.......and so on.
while(rs.next){
x=rs.getString('id');
}

然后使用此x并将其传递给insert语句..

答案 6 :(得分:0)

最简单的方法是将它们放入会话或cookie中,如果只有一个或两个值。

session.setAttribute("id",id);
cookie.setAttribute("name",name);