public class signup extends HttpServlet {
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String u=request.getParameter("user"); String p=request.getParameter("password"); String c=request.getParameter("city"); String m=request.getParameter("mobile"); String s=request.getParameter("salary");
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn=DriverManager.getConnection("jdbc:odbc:student"); Statement st=cn.createStatement(); ResultSet i=st.executeQuery("select MAX(sn) from detail"); int x=i.getInt(1); x=x+1; st.executeUpdate("insert into detail values(3,'"+u+"','"+p+"','"+c+"','"+m+"','"+s+"')"); ResultSet rs=st.executeQuery("select * from detail"); //" & ' me space ni dena h while(rs.next()){ out.println("login success"+"<br>"); out.println("ur details are:<br>"); out.println("user name : "+rs.getString(2)+"<br>"); out.println("city : "+rs.getString(4)+"<br>"); out.println("mobile : "+rs.getString(5)+"<br>"); out.println("salary : "+rs.getString(6)+"<br>"); } cn.close(); } catch(ClassNotFoundException e){ out.print("class not found"); } catch(SQLException e){ out.print("sql error"); } }
}
i want to select max(sn) from table then insert into table as(max+1,user,password,city,mobile,salary).. help me pls
Try this:
public class signup extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String u=request.getParameter("user"); String p=request.getParameter("password"); String c=request.getParameter("city"); String m=request.getParameter("mobile"); String s=request.getParameter("salary"); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn=DriverManager.getConnection("jdbc:odbc:student"); Statement st=cn.createStatement(); ResultSet rs=st.executeQuery("select MAX(sn) from detail"); int x=0; while(rs.next()){ x=rs.getInt(1); } int y=x+1; st.executeUpdate("insert into detail values("+y+",'"+u+"','"+p+"','"+c+"','"+m+"','"+s+"')"); ResultSet rst=st.executeQuery("select * from detail"); if(rst.last()){ out.println("login success"+"<br>"); out.println("ur details are:<br>"); out.println("user name : "+rst.getString(2)+"<br>"); out.println("city : "+rst.getString(4)+"<br>"); out.println("mobile : "+rst.getString(5)+"<br>"); out.println("salary : "+rst.getString(6)+"<br>"); } cn.close(); } catch(ClassNotFoundException e){ out.print("class not found"); } catch(SQLException e){ out.print("sql error"); } } }
Try this:
public class signup extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String u=request.getParameter("user"); String p=request.getParameter("password"); String c=request.getParameter("city"); String m=request.getParameter("mobile"); String s=request.getParameter("salary"); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn=DriverManager.getConnection("jdbc:odbc:student"); Statement st=cn.createStatement(); ResultSet rs=st.executeQuery("select MAX(sn) from detail"); int x=0; while(rs.next()){ x=rs.getInt(1); } int y=x+1; st.executeUpdate("insert into detail values("+y+",'"+u+"','"+p+"','"+c+"','"+m+"','"+s+"')"); ResultSet rst=st.executeQuery("select * from detail"); if(rst.last()){ out.println("login success"+"<br>"); out.println("ur details are:<br>"); out.println("user name : "+rst.getString(2)+"<br>"); out.println("city : "+rst.getString(4)+"<br>"); out.println("mobile : "+rst.getString(5)+"<br>"); out.println("salary : "+rst.getString(6)+"<br>"); } cn.close(); } catch(ClassNotFoundException e){ out.print("class not found"); } catch(SQLException e){ out.print("sql error"); } } }