Advertisement
Guest User

sandip

a guest
Oct 26th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package uk.ac.le.cs.CO3098;
  2.  
  3. import java.io.*;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import javax.servlet.*;
  10. import javax.servlet.http.*;
  11.  
  12. public class GetStudentInfo extends HttpServlet {
  13. private static Connection connect = null;
  14. private static String host = "mysql.mcscw3.le.ac.uk";
  15. private static String database = "ss906";
  16. private static String username = "ss906";
  17. private static String password = "teewearl";
  18.  
  19. public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  20.  
  21. PrintWriter out = res.getWriter();
  22. res.setContentType("text/html");
  23.  
  24. String id = req.getParameter("id");
  25. try {
  26. if (id != null && id.length() != 0) {
  27. String sql = "SELECT * from Student WHERE ID=?";
  28. Class.forName("com.mysql.jdbc.Driver");
  29. String conn_string = "jdbc:mysql://" + host + "/" + database;
  30. try (Connection connect = DriverManager.getConnection(conn_string, username, password);
  31. PreparedStatement pstmt = connect.prepareStatement(sql);) {
  32. pstmt.setInt(1, Integer.parseInt(id));
  33. try (ResultSet rs = pstmt.executeQuery();) {
  34. while (rs.next()) {
  35. out.println(rs.getString("Name"));
  36. }
  37. }
  38. } catch (SQLException ex) {
  39. out.println("Error");
  40. ex.printStackTrace();
  41. }
  42. } else {
  43. out.println("Student id not provided");
  44. }
  45. } catch (Exception ex) {
  46. out.println("MySQL driver not found");
  47. }
  48. out.close();
  49. }
  50.  
  51. public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  52. doGet(req, res);
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement