943,148 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 938
  • JSP RSS
Mar 21st, 2010
0

Problem connecting to Mysql from jsp in tomcat

Expand Post »
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:

xml Syntax (Toggle Plain Text)
  1. <description>MySQL Test App</description>
  2. <resource-ref>
  3. <description>DB Connection</description>
  4. <res-ref-name>jdbc/TestDB</res-ref-name>
  5. <res-type>javax.sql.DataSource</res-type>
  6. <res-auth>Container</res-auth>
  7. </resource-ref>

server.xml I have added this:


xml Syntax (Toggle Plain Text)
  1. <Context path="/become3" docBase="become3"
  2. debug="5" reloadable="true" crossContext="true">
  3.  
  4. <Logger className="org.apache.catalina.logger.FileLogger"
  5. prefix="localhost_become3_log." suffix=".txt"
  6. timestamp="true"/>
  7.  
  8. <Resource name="jdbc/TestDB"
  9. auth="Container"
  10. type="javax.sql.DataSource"/>
  11.  
  12. <ResourceParams name="jdbc/TestDB">
  13. <parameter>
  14. <name>factory</name>
  15. <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
  16. </parameter>
  17.  
  18. <!-- Maximum number of dB connections in pool. Make sure you
  19. configure your mysqld max_connections large enough to handle
  20. all of your db connections. Set to -1 for no limit.
  21. -->
  22. <parameter>
  23. <name>maxActive</name>
  24. <value>100</value>
  25. </parameter>
  26.  
  27. <!-- Maximum number of idle dB connections to retain in pool.
  28. Set to 0 for no limit.
  29. -->
  30. <parameter>
  31. <name>maxIdle</name>
  32. <value>30</value>
  33. </parameter>
  34.  
  35. <!-- Maximum time to wait for a dB connection to become available
  36. in ms, in this example 10 seconds. An Exception is thrown if
  37. this timeout is exceeded. Set to -1 to wait indefinitely.
  38. -->
  39. <parameter>
  40. <name>maxWait</name>
  41. <value>10000</value>
  42. </parameter>
  43.  
  44. <!-- MySQL dB username and password for dB connections -->
  45. <parameter>
  46. <name>username</name>
  47. <value>root</value>
  48. </parameter>
  49. <parameter>
  50. <name>password</name>
  51. <value>root</value>
  52. </parameter>
  53.  
  54. <!-- Class name for mm.mysql JDBC driver -->
  55. <parameter>
  56. <name>driverClassName</name>
  57. <value>com.mysql.jdbc.Driver</value>
  58. </parameter>
  59.  
  60. <!-- The JDBC connection url for connecting to your MySQL dB.
  61. -->
  62. <parameter>
  63. <name>url</name>
  64. <value>jdbc:mysql://localhost:3306/first?autoReconnect=true</value>
  65. </parameter>
  66. </ResourceParams>
  67. </Context>

JSP Syntax (Toggle Plain Text)
  1. <%@ page import = "java.util.*" %>
  2. <%@ page import = "java.sql.*" %>
  3. <%@ page import = "javax.sql.*" %>
  4. <%@ page import = "java.io.*" %>
  5. <%@ page import = "javax.naming.*" %>
  6.  
  7. <html>
  8. <head>
  9. <title>DB Test</title>
  10. </head>
  11. <body>
  12.  
  13. <h2>Results</h2>
  14.  
  15. <%
  16.  
  17. try {
  18.  
  19. Connection con1 = null;
  20. String connectionURL = "jdbc:mysql://localhost:3306/first";
  21. String id = "";
  22.  
  23. Class.forName("com.mysql.jdbc.Driver").newInstance();
  24. String sql = "select id from deals";
  25. PreparedStatement pstmt2 = con1.prepareStatement(sql);
  26. con1 = DriverManager.getConnection(connectionURL, "*******", "******");
  27. out.println(con1);
  28.  
  29. ResultSet rs = pstmt2.executeQuery(sql);
  30.  
  31. while(rs.next()){
  32. id = rs.getString(1);
  33. }
  34.  
  35. out.println(id);
  36. }
  37. catch(Exception e){
  38. e.printStackTrace();
  39. }
  40. %>
  41. </body>
  42. </html>
Last edited by peter_budo; Mar 21st, 2010 at 11:10 am. Reason: Please do not use CODE SNIPPED for posting question. Use FORUM THREAD.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anuraggupta16 is offline Offline
5 posts
since Sep 2009
Mar 22nd, 2010
0
Re: Problem connecting to Mysql from jsp in tomcat
don't EVER try to put Java code in JSP.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: javamail
Next Thread in JSP Forum Timeline: Error configuring application listener of class listeners.ContextListener





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC