Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:22:40+00:00 2026-06-12T08:22:40+00:00

I’m creating a java servlet and trying to create a new ‘lab’. However, it

  • 0

I’m creating a java servlet and trying to create a new ‘lab’. However, it isn’t submitting to the database. Looking at the console when the page loads it prints JDBC Driver Loaded and then when submit is clicked it prints out 'got connection' in the console. When I check the database the information hasn’t submitted

The table I’m try to submit to is:

lab - id, capacity, day, time, room, subject_id, user_id

Here is the code for my servlet:

import java.io.IOException;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
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;

    /**
     * Servlet implementation class CreateLab
     */
    @WebServlet("/CreateLab")
    public class CreateLab extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
         * @see HttpServlet#HttpServlet()
         */
        public CreateLab() {
            super();
            // TODO Auto-generated constructor stub
        }

        private int capacity; 
        private String day = ""; 
        private String time = ""; 
        private String room = ""; 
        private int subject_id;
        private int user_id;

        public void init() {
          try {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con =
                DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
            System.out.println("JDBC driver loaded"); 
          } 
          catch (ClassNotFoundException e) {
            System.out.println(e.toString()); 
          } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        } 

        /**Process the HTTP Get request*/ 
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
      ServletException,IOException {  
          sendPageHeader(response); 
          sendRegistrationForm(request, response, false); 
          sendPageFooter(response); 
        } 

        /**Process the HTTP Post request*/ 
        public void doPost(HttpServletRequest request, 
          HttpServletResponse response) 
          throws ServletException, IOException {
          sendPageHeader(response); 


          day = request.getParameter("day"); 
          time = request.getParameter("time"); 
          room = request.getParameter("room"); 



          boolean error = false; 
          String message = null; 
          try {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = 
                DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
            System.out.println("got connection"); 
            System.out.println(capacity);
            System.out.println(day);
            System.out.println(time);
            System.out.println(room);
            System.out.println(subject_id);
            System.out.println(user_id);
            Statement s = con.createStatement(); 

            String sql = "SELECT id FROM user" + 
                    " WHERE id='" + user_id + "'";  
            ResultSet rs = s.executeQuery(sql); 
            if (rs.next()) {
              rs.close(); 
               sql = "INSERT INTO lab" + 
                      " (capacity, day, time, room, subject_id, user_id)" + 
                      " VALUES" + 
                      " ('" +  capacity + "'," + 
                         " '"  +  day + "'," + 
                         " '"  +  time + "'," + 
                         " '"  + room + "','" + subject_id +
                         "'," + user_id + "')"; 


              int i = s.executeUpdate(sql); 
              if (i==1) {
                message = "Successfully a new lab class."; 
              } 
            } 
              s.close(); 
              con.close(); 
            } 
            catch (SQLException e) {
              message = "Error." + e.toString(); 
              error = true; 
            } 
            catch (Exception e) {
              message = "Error." + e.toString(); 
              error = true; 
            } 
            if (message!=null) {
              PrintWriter out = response.getWriter(); 
              out.println("<B>" + message + "</B><BR>"); 
              out.println("<HR><BR>"); 
            } 
            if (error==true) 
              sendRegistrationForm(request, response, true); 
            else 
              sendRegistrationForm(request, response, false); 
            sendPageFooter(response); 
          } 

          /** 
           * Send the HTML page header, including the title 
           * and the <BODY> tag 
           */ 
          private void sendPageHeader(HttpServletResponse response) 
            throws ServletException, IOException {
            response.setContentType("text/html"); 
            PrintWriter out = response.getWriter(); 
            out.println("<HTML>"); 
            out.println("<HEAD>"); 
            out.println("<TITLE>Create Lab Page</TITLE>"); 
            out.println("</HEAD>"); 
            out.println("<BODY>"); 
            out.println("<CENTER>"); 
          } 

          /** 
           * Send the HTML page footer, i.e. the </BODY> 
           * and the </HTML> 
           */ 
          private void sendPageFooter(HttpServletResponse response) 
            throws ServletException, IOException {
            PrintWriter out = response.getWriter(); 
            out.println("</CENTER>"); 
            out.println("</BODY>"); 
            out.println("</HTML>"); 
          }   
          /**Send the form where the user can type in 
           * the details for a new user 
           */ 
          private void sendRegistrationForm(HttpServletRequest request, 
            HttpServletResponse response, boolean displayPreviousValues) 
            throws ServletException, IOException {

            PrintWriter out = response.getWriter(); 
            out.println("<BR><H2>Create A Lab</H2>"); 
            out.println("<BR>Please enter the lab details."); 
            out.println("<BR>"); 
            out.println("<BR><FORM METHOD=POST>"); 
            out.println("<TABLE>"); 
            out.println("<TR>"); 
            out.println("<TD>Class Capacity</TD>"); 
            out.print("<TD><INPUT TYPE=TEXT Name=capacity"); 

            if (displayPreviousValues) 
              out.print(" VALUE=\"" + capacity + "\""); 

            out.println("></TD>"); 
            out.println("</TR>"); 
            out.println("<TR>"); 
            out.println("<TD>Day</TD>"); 
            out.print("<TD><INPUT TYPE=TEXT Name=day"); 

            if (displayPreviousValues) 
              out.print(" VALUE=\"" + day + "\""); 

            out.println("></TD>"); 
            out.println("</TR>"); 
            out.println("<TR>"); 
            out.println("<TD>Time</TD>"); 
            out.print("<TD><INPUT TYPE=TEXT Name=time"); 

            if (displayPreviousValues) 
              out.print(" VALUE=\"" + time + "\""); 

            out.println("></TD>"); 
            out.println("</TR>"); 
            out.println("<TR>"); 
            out.println("<TD>Room</TD>"); 
            out.print("<TD><INPUT TYPE=TEXT Name=room");
            if (displayPreviousValues) 
                out.print(" VALUE=\"" + room + "\"");
            out.println("></TD>");
            out.println("</TR>");
            out.println("<TR>");
            out.println("<TD>subject_id</TD>");
            out.print("<TD><INPUT TYPE=TEXT Name=subject_id");
            if (displayPreviousValues) 
                out.print(" VALUE=\"" + subject_id + "\"");
            out.println("></TD>");
            out.println("</TR>");
            out.println("<TR>");
            out.println("<TD>user_id</TD>");
            out.print("<TD><INPUT TYPE=TEXT Name=user_id");
            out.println("></TD>");
            out.println("</TR>");

            if (displayPreviousValues) 
            out.print(" VALUE=\"" + user_id + "\""); 
            out.println("</TD>"); 
            out.println("</TR>");

            out.println("<TR>"); 
            out.println("<TD><INPUT TYPE=RESET></TD>"); 
            out.println("<TD><INPUT TYPE=SUBMIT></TD>"); 
            out.println("</TR>"); 
            out.println("</TABLE>"); 
            out.println("</FORM>"); 
            out.println("<BR>"); 
            out.println("<BR>"); 
          }
      }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:22:42+00:00Added an answer on June 12, 2026 at 8:22 am
    import java.io.IOException;
    
    
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    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;
    
    /**
     * Servlet implementation class CreateLab
     */
    @WebServlet("/CreateLab")
    public class CreateLab extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        /**
         * @see HttpServlet#HttpServlet()
         */
        public CreateLab() {
            super();
            // TODO Auto-generated constructor stub
        }
    
         int capacity; 
         String day = ""; 
         String time = ""; 
         String room = ""; 
         int subject_id;
         int user_id;
    
        public void init() {
          try {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con =
                DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
            System.out.println("JDBC driver loaded"); 
          } 
          catch (ClassNotFoundException e) {
            System.out.println(e.toString()); 
          } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        } 
    
        /**Process the HTTP Get request*/ 
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
      ServletException,IOException {  
          sendPageHeader(response); 
          sendRegistrationForm(request, response, false); 
          sendPageFooter(response); 
        } 
    
        /**Process the HTTP Post request*/ 
        public void doPost(HttpServletRequest request, 
          HttpServletResponse response) 
          throws ServletException, IOException {
          sendPageHeader(response); 
    
          capacity = Integer.parseInt(request.getParameter("capacity")); 
          day = request.getParameter("day"); 
          time = request.getParameter("time"); 
          room = request.getParameter("room"); 
          user_id = Integer.parseInt(request.getParameter("user_id")); 
          subject_id = Integer.parseInt(request.getParameter("subject_id")); 
    
    
          boolean error = false; 
          String message = null; 
          try {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = 
                DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
            System.out.println("got connection"); 
            System.out.println(capacity);
            System.out.println(day);
            System.out.println(time);
            System.out.println(room);
            System.out.println(subject_id);
            System.out.println(user_id);
            Statement s = con.createStatement(); 
    
            String sql = "SELECT id FROM user" + 
                    " WHERE id='" + user_id + "'";  
            ResultSet rs = s.executeQuery(sql); 
            if (rs.next()) {
              rs.close(); 
               sql = "INSERT INTO lab" + 
                      " (capacity, day, time, room, subject_id, user_id)" + 
                      " VALUES" + 
                      " ('" +  capacity + "'," + 
                         " '"  +  day + "'," + 
                         " '"  +  time + "'," + 
                         " '"  + room + "','" + subject_id + "','" + user_id + "')"; 
    
              System.out.println(sql);
              int i = s.executeUpdate(sql); 
              if (i==1) {
                message = "Successfully a new lab class."; 
              } 
            } 
              s.close(); 
              con.close(); 
            } 
            catch (SQLException e) {
              message = "Error." + e.toString(); 
              error = true; 
            } 
            catch (Exception e) {
              message = "Error." + e.toString(); 
              error = true; 
            } 
            if (message!=null) {
              PrintWriter out = response.getWriter(); 
              out.println("<B>" + message + "</B><BR>"); 
              out.println("<HR><BR>"); 
            } 
            if (error==true) 
              sendRegistrationForm(request, response, true); 
            else 
              sendRegistrationForm(request, response, false); 
            sendPageFooter(response); 
          } 
    
          /** 
           * Send the HTML page header, including the title 
           * and the <BODY> tag 
           */ 
          private void sendPageHeader(HttpServletResponse response) 
            throws ServletException, IOException {
            response.setContentType("text/html"); 
            PrintWriter out = response.getWriter(); 
            out.println("<HTML>"); 
            out.println("<HEAD>"); 
            out.println("<TITLE>Create Lab Page</TITLE>"); 
            out.println("</HEAD>"); 
            out.println("<BODY>"); 
            out.println("<CENTER>"); 
          } 
    
          /** 
           * Send the HTML page footer, i.e. the </BODY> 
           * and the </HTML> 
           */ 
          private void sendPageFooter(HttpServletResponse response) 
            throws ServletException, IOException {
            PrintWriter out = response.getWriter(); 
            out.println("</CENTER>"); 
            out.println("</BODY>"); 
            out.println("</HTML>"); 
          }   
          /**Send the form where the user can type in 
           * the details for a new user 
           */ 
          private void sendRegistrationForm(HttpServletRequest request, 
            HttpServletResponse response, boolean displayPreviousValues) 
            throws ServletException, IOException {
    
            PrintWriter out = response.getWriter(); 
            out.println("<BR><H2>Create A Lab</H2>"); 
            out.println("<BR>Please enter the lab details."); 
            out.println("<BR>"); 
            out.println("<BR><FORM METHOD=POST>"); 
            out.println("<TABLE>"); 
            out.println("<TR>"); 
            out.println("<TD>Class Capacity</TD>"); 
            out.print("<TD><INPUT TYPE=TEXT Name=capacity"); 
    
            if (displayPreviousValues) 
              out.print(" VALUE=\"" + capacity + "\""); 
    
            out.println("></TD>"); 
            out.println("</TR>"); 
            out.println("<TR>"); 
            out.println("<TD>Day</TD>"); 
            out.print("<TD><INPUT TYPE=TEXT Name=day"); 
    
            if (displayPreviousValues) 
              out.print(" VALUE=\"" + day + "\""); 
    
            out.println("></TD>"); 
            out.println("</TR>"); 
            out.println("<TR>"); 
            out.println("<TD>Time</TD>"); 
            out.print("<TD><INPUT TYPE=TEXT Name=time"); 
    
            if (displayPreviousValues) 
              out.print(" VALUE=\"" + time + "\""); 
    
            out.println("></TD>"); 
            out.println("</TR>"); 
            out.println("<TR>"); 
            out.println("<TD>Room</TD>"); 
            out.print("<TD><INPUT TYPE=TEXT Name=room");
            if (displayPreviousValues) 
                out.print(" VALUE=\"" + room + "\"");
            out.println("></TD>");
            out.println("</TR>");
            out.println("<TR>");
            out.println("<TD>subject_id</TD>");
            out.print("<TD><INPUT TYPE=TEXT Name=subject_id");
            if (displayPreviousValues) 
                out.print(" VALUE=\"" + subject_id + "\"");
            out.println("></TD>");
            out.println("</TR>");
            out.println("<TR>");
            out.println("<TD>user_id</TD>");
            out.print("<TD><INPUT TYPE=TEXT Name=user_id");
            out.println("></TD>");
            out.println("</TR>");
    
            if (displayPreviousValues) 
            out.print(" VALUE=\"" + user_id + "\""); 
            out.println("</TD>"); 
            out.println("</TR>");
    
            out.println("<TR>"); 
            out.println("<TD><INPUT TYPE=RESET></TD>"); 
            out.println("<TD><INPUT TYPE=SUBMIT></TD>"); 
            out.println("</TR>"); 
            out.println("</TABLE>"); 
            out.println("</FORM>"); 
            out.println("<BR>"); 
            out.println("<BR>"); 
          }
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.