Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. **package course_14024632;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.annotation.WebServlet;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. @WebServlet("/CourseAdd")
  14. public class CourseAdd extends HttpServlet {
  15. private static final long serialVersionUID = 1L;
  16.  
  17. public CourseAdd() {
  18. super();
  19.  
  20. }
  21. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  22.  
  23. //this adds a new recording into the database
  24. //under the table called Music_Recordings
  25.  
  26. response.setContentType("text/html;charset=UTF-8");
  27. PrintWriter out = response.getWriter();
  28.  
  29. out.println("<header> <link rel = "stylesheet" href= "style1.css" /> </header>");
  30. //links to the stylesheet
  31.  
  32. String r = request.getParameter("courseid");
  33. String a = request.getParameter("coursename");
  34. String t = request.getParameter("coursecredits");
  35. String c = request.getParameter("courseduration");
  36. String i = request.getParameter("coursetutor");
  37.  
  38. //gets all the parameters
  39.  
  40. String insertSQL = "insert into Course values('+r+','+a+','"+t+"','"+c+"','+i+')";
  41.  
  42. //creates an SQL statement
  43.  
  44.  
  45. Connection conn =null; // Create connection object so it can connect to the database
  46. String database = "ahmadmoh"; // Name of the database
  47. String user = "ahmadmoh"; //name of the username
  48. String password = "rerRykil2"; //and password
  49. String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk/" + database;
  50.  
  51. try{
  52. Class.forName("com.mysql.jdbc.Driver").newInstance();
  53. } catch(Exception e) { System.out.println(e); }
  54.  
  55.  
  56. // connecting to database
  57. try{
  58. conn = DriverManager.getConnection(url, user, password);
  59.  
  60. }
  61. catch(SQLException se) {
  62. System.err.println(se);
  63. }
  64. // Create select statement and execute it
  65.  
  66. try{
  67.  
  68. Statement stmt = conn.createStatement();
  69. int success = stmt.executeUpdate(insertSQL);
  70. if(success<1){
  71. System.out.println("update failed!");
  72. }
  73.  
  74. //executes the statement created earlier and closes
  75. //conneciton to the database
  76.  
  77. conn.close();
  78. }catch(SQLException se) {
  79. System.err.println(se);
  80. }
  81.  
  82.  
  83. doGet(request, response);
  84. }
  85. }**
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement