magoo123

Untitled

Oct 17th, 2011
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. package onlinecontactbackup;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11.  
  12. import javax.servlet.ServletException;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16.  
  17. public class EnrollmentServlet extends HttpServlet {
  18.  
  19. static final String DataBaseURL = "jdbc:mysql://localhost:3306/phonebookbackup?"
  20. + "user=root&password=null";
  21.  
  22. public void doSend(HttpServletRequest request,HttpServletResponse response)
  23. throws IOException,ServletException{
  24.  
  25. Connection conn = null;
  26.  
  27. response.setContentType("text/plain");
  28.  
  29. PrintWriter outResponse = response.getWriter();
  30.  
  31. try{
  32. Class.forName("com.mysql.jdbc.Driver");
  33.  
  34. } catch(ClassNotFoundException e){
  35. throw new ServletException("Unable to load JDBC Driver");
  36.  
  37.  
  38. }
  39.  
  40. try{
  41.  
  42. String userInputName =
  43. request.getParameter("FullNames");
  44.  
  45. String userInputUserName =
  46. request.getParameter("UserName");
  47.  
  48. String userInputEmail =
  49. request.getParameter("Email");
  50.  
  51. String userInputPassword =
  52. request.getParameter("Password");
  53.  
  54. String userInputVerifyPassword =
  55. request.getParameter("VerifyPassword");
  56.  
  57. conn = DriverManager.getConnection(DataBaseURL);
  58.  
  59. PreparedStatement psmt =
  60. conn.prepareStatement("INSERT INTO RegistrationT VALUES(?,?,?,?,?)");
  61.  
  62. psmt.setString(2, userInputName);
  63. psmt.setString(3, userInputUserName);
  64. psmt.setString(4, userInputEmail);
  65. psmt.setString(5, userInputEmail);
  66. psmt.setString(6, userInputPassword);
  67. psmt.setString(7, userInputVerifyPassword);
  68.  
  69. psmt.execute();
  70.  
  71. conn.close();
  72.  
  73.  
  74. } catch(SQLException sqe){
  75. throw new ServletException("SQL call failed");
  76.  
  77. } catch(Exception e){
  78. throw new ServletException(e.getMessage());
  79.  
  80. } finally{
  81. if(conn!=null){
  82.  
  83. try{
  84. conn.close();
  85.  
  86. } catch(SQLException e){
  87. throw new ServletException("Connection close failed");
  88.  
  89. }
  90. }
  91. }
  92.  
  93. }
  94.  
  95.  
  96.  
  97. public void doGet(HttpServletRequest request,HttpServletResponse response)
  98. throws IOException,ServletException{
  99.  
  100. Connection conn = null;
  101.  
  102. response.setContentType("text/plain");
  103. PrintWriter outResponse = response.getWriter();
  104.  
  105. try{
  106. Class.forName("com.mysql.jdbc.Driver");
  107.  
  108. } catch(ClassNotFoundException e){
  109. throw new ServletException("Unable to load JDBC driver");
  110.  
  111. }
  112.  
  113. try{
  114. String UserName = request.getParameter("UserName");
  115. System.out.print("UserName=" +UserName);
  116. conn = DriverManager.getConnection(DataBaseURL);
  117.  
  118. Statement stmt = conn.createStatement();
  119.  
  120. String SQlQuery = "SELECT* "
  121. + "FROM LoginT" + "WHERE UserName='"
  122. + UserName + "'";
  123.  
  124. ResultSet rs = stmt.executeQuery(SQlQuery);
  125.  
  126. if(rs.next()){
  127. outResponse.println(rs.getString(2));
  128. outResponse.println(rs.getString(3));
  129.  
  130. } else {
  131.  
  132. outResponse.println("Record Not Found!");
  133. }
  134. conn.close();
  135.  
  136. } catch(SQLException e){
  137. throw new ServletException("SQL call failed!");
  138.  
  139. } catch(Exception e){
  140. throw new ServletException(e.getMessage());
  141.  
  142. } finally{
  143. if(conn!=null){
  144. try{
  145. conn.close();
  146.  
  147. } catch(SQLException e){
  148. throw new ServletException("Connection close failed!");
  149. }
  150. }
  151. }
  152.  
  153. }
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment