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 8378335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:54:28+00:00 2026-06-09T15:54:28+00:00

I have the following Java class: package web; import javax.servlet.*; import javax.servlet.http.*; import java.io.*;

  • 0

I have the following Java class:

package web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;


public class HelloWorld extends HttpServlet {
 public void doPost(HttpServletRequest request,HttpServletResponse response)throws   IOException, ServletException {
   response.setContentType("text/html");
   PrintWriter out = response.getWriter();
   out.println("Student Name<br>");


 try{  
   Connection conn=null;
       String url = "jdbc:mysql://localhost/test";
       PreparedStatement pre=null;

       Class.forName("com.mysql.jdbc.Driver").newInstance();
       conn=DriverManager.getConnection(url,"root","3324911");
       out.println("Database connected");
       pre = conn.prepareStatement("Insert INTO countries(COUNTRY,COUNTRY_ISO_CODE,REGION) VALUES (?,?,?)");
       pre.setString(1,"USA");
       pre.setString(2,"US");
       pre.setString(3,"North America");
       pre.executeUpdate();

   }catch (Exception e){//Catch exception if any
       System.err.println("Error: " + e.getMessage());
  }
 }
}

For whatever reason, after I deploy this class in Tomcat, the program could only reach the line

  Class.forName("com.mysql.jdbc.Driver").newInstance();

It will fail after it gets to line:

  conn=DriverManager.getConnection(url,"root","3324911");

and this try/catch block is working as a separate method, I took it from a main method from another class, and it is working properly.

I am not sure where I messed up when I plug this try/catch block here in Tomcat.

Any help is appreciated, thanks!

  • 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-09T15:54:30+00:00Added an answer on June 9, 2026 at 3:54 pm

    Is the JDBC driver configured in the path or copied into the lib directory (i.e. $CATALINA_HOME/lib or web-inf/lib)?

    Also, it would be better if you use a connection pool – tomcat comes with one which is a better approach than spawning your own DB connections.

    Also, it might be better if you clean up your DB related resources (unless you just left it out from the code above for simplicity).

    Hope this helps.

    Yes, I agree with the other responders – getting more detailed error logging might be the preferred route.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following action class: package com.pendulum.web; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest;
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have the following servlet coded in /src/main/java/examples/web/SimpleServlet.java : package examples.web; import java.io.IOException; import
I have the following Stripes ActionBean: package myapp; import net.sourceforge.stripes.action.*; public class WelcomeActionBean extends
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
I have the following Java class defined in src/java package org.davisworld.trip; public class AirportHbm
I have the following class: package lt.vic.valdos.domain.valda; public class Valda implements java.io.Serializable { private
I have the following java class: package domain; //imports @Entity public class User {
Say suppose I have the following Java code. public class Example { public static
I'm looking at some Java classes that have the following form: public abstract class

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.