Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **package course_14024632;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- @WebServlet("/CourseAdd")
- public class CourseAdd extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public CourseAdd() {
- super();
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //this adds a new recording into the database
- //under the table called Music_Recordings
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- out.println("<header> <link rel = "stylesheet" href= "style1.css" /> </header>");
- //links to the stylesheet
- String r = request.getParameter("courseid");
- String a = request.getParameter("coursename");
- String t = request.getParameter("coursecredits");
- String c = request.getParameter("courseduration");
- String i = request.getParameter("coursetutor");
- //gets all the parameters
- String insertSQL = "insert into Course values('+r+','+a+','"+t+"','"+c+"','+i+')";
- //creates an SQL statement
- Connection conn =null; // Create connection object so it can connect to the database
- String database = "ahmadmoh"; // Name of the database
- String user = "ahmadmoh"; //name of the username
- String password = "rerRykil2"; //and password
- String url = "jdbc:mysql://mudfoot.doc.stu.mmu.ac.uk/" + database;
- try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch(Exception e) { System.out.println(e); }
- // connecting to database
- try{
- conn = DriverManager.getConnection(url, user, password);
- }
- catch(SQLException se) {
- System.err.println(se);
- }
- // Create select statement and execute it
- try{
- Statement stmt = conn.createStatement();
- int success = stmt.executeUpdate(insertSQL);
- if(success<1){
- System.out.println("update failed!");
- }
- //executes the statement created earlier and closes
- //conneciton to the database
- conn.close();
- }catch(SQLException se) {
- System.err.println(se);
- }
- doGet(request, response);
- }
- }**
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement