-
Insert Data into MySQL database from HTML SubForms
about 9 years ago
-
about 9 years ago
Hi
Please send complete code you are using so that we can provide you correct solution.Post your code over here by clicking on (Answer it Button) only so that we will have a complete thread for this issue.
-
about 9 years ago
Here is the Login Servlet:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; public class Login extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException { HttpSession session = request.getSession(true); try { String name=request.getParameter("name"); String password=request.getParameter("pass"); String member=request.getParameter("mem"); session.setAttribute(name, "name"); response.setContentType("text/html"); PrintWriter out=response.getWriter(); Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection ("jdbc:mysql://localhost:3306/EATWO","root",""); PreparedStatement stmt=con.prepareStatement ("select * from singer where name=? and password=? and member=?"); stmt.setString(1,name); stmt.setString(2,password); stmt.setString(3,member); ResultSet rset=stmt.executeQuery(); if(rset.next()){ if(member.equals("Singers")) { System.out.println(rset.getString(1)); //out.println("Welcome," +name); //System.out.println(rset.getString(1)); //out.println("<!DOCTYPE html><head>"+"<title>Singers Page</title>"+"</head>"+"<body>"); //out.println("<script>alert('Create Ur Page Now')</script>"); //out.print("</body></html>"); //ServletContext context = getServletContext(); RequestDispatcher rd = request.getRequestDispatcher("Singers.html"); rd.include(request, response); } else{ RequestDispatcher rd = request.getRequestDispatcher("Producers.html"); rd.include(request, response); } } else { RequestDispatcher rd=request.getRequestDispatcher("login_err.html"); rd.include(request,response); con.close(); } } catch(Exception e) { System.out.println(e); } } }
Here is the code which is used to store the details of that user:
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class SingerDetails extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(true); String name = (String)request.getAttribute("name"); String Sdetails = request.getParameter("Sdetails"); String SName = request.getParameter("SName"); String Language = request.getParameter("Language"); String Website = request.getParameter("Email"); String Mobile = request.getParameter("Mobile"); try{ //loading drivers for mysql Class.forName("com.mysql.jdbc.Driver"); //creating connection with the database Connection con=DriverManager.getConnection ("jdbc:mysql://localhost:3306/EATWO","root",""); Statement statement = (Statement) con.createStatement(); ResultSet rs = statement.executeQuery("SELECT userID from singer where name ='" + name + "'"); String duplicate = null; while(rs.next()){ duplicate = rs.getString(1); } if(duplicate == null){ PreparedStatement ps=con.prepareStatement ("insert into singerdetails(singername,stagename,Language,Email,Mobile) values(?,?,?,?,?)"); ps.setString(1, Sdetails); ps.setString(2, SName); ps.setString(3, Language); ps.setString(4, Website); ps.setString(5, Mobile); int i=ps.executeUpdate(); if(i>0) { out.println("You are sucessfully registered"); //response.sendRedirect("./singerdetails1"); } } else{ //request.getRequestDispatcher("/register.html").forward(request, response); //request.setAttribute("error","Invalid Username/password"); request.getRequestDispatcher("/Singers.html").forward(request, response); } } catch(Exception se) { se.printStackTrace(); } } }
-
2 Answer(s)