I am trying to connect to Mysql from Jsp in Tomcat and I am getting Java.lang.NullPointer Exception. Please provide any help as soon as possible.

I have set the web.xml and the server.xml properly.

My web.xml contains:

<description>MySQL Test App</description>
		  <description>DB Connection</description>

server.xml I have added this:

<Context path="/become3" docBase="become3"
			debug="5" reloadable="true" crossContext="true">

	  <Logger className="org.apache.catalina.logger.FileLogger"
				 prefix="localhost_become3_log." suffix=".txt"

	  <Resource name="jdbc/TestDB"

	  <ResourceParams name="jdbc/TestDB">

		<!-- Maximum number of dB connections in pool. Make sure you
			 configure your mysqld max_connections large enough to handle
			 all of your db connections. Set to -1 for no limit.

		<!-- Maximum number of idle dB connections to retain in pool.
			 Set to 0 for no limit.

		<!-- Maximum time to wait for a dB connection to become available
			 in ms, in this example 10 seconds. An Exception is thrown if
			 this timeout is exceeded.  Set to -1 to wait indefinitely.

		<!-- MySQL dB username and password for dB connections  -->

		<!-- Class name for mm.mysql JDBC driver -->

		<!-- The JDBC connection url for connecting to your MySQL dB.
<%@ page import = "java.util.*" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "javax.sql.*" %>
<%@ page import = "java.io.*" %>
<%@ page import = "javax.naming.*" %>

    <title>DB Test</title>

	   try {
		   Connection con1 = null;
		   String connectionURL = "jdbc:mysql://localhost:3306/first";
		   String id = "";

		   String sql = "select id from deals";
		   PreparedStatement pstmt2 = con1.prepareStatement(sql);
		   con1 = DriverManager.getConnection(connectionURL, "*******", "******");

			ResultSet rs = pstmt2.executeQuery(sql);
				id = rs.getString(1);

	   catch(Exception e){

don't EVER try to put Java code in JSP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.