开发者

problem with connecting to a mySQL data source using jsp

I have a short jsp page that tries to establish the connection to the mySQL database named 'test'. I tried开发者_如何转开发 to deploy it properly in Tomcat but with no success. All other jsp pages work well. This page gives an error when executed. I'm still a student & I'm trying experimenting on this. I already got the mysql-connector-java-5.1.16-bin.jar & tried putting it to the places in the answers that I found when i googled this question. The jsp page includes;

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>

Connection con = null;

try
        {
            String connectionURL = "jdbc:mysql://localhost:3306/test";

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection(connectionURL);

        }
        catch(SQLException ex)
        {
            throw new ServletException("Servlet could not display records.",ex);
        }
        catch(ClassNotFoundException ex)
        {
            throw new ServletException("JDBC Driver not found",ex);
        }

The errors are;

org.apache.jasper.JasperException: An exception occurred processing JSP page /access_exp_two.jsp at line 72

69:         {
70:             String connectionURL = "jdbc:mysql://localhost:3306/test";
71:             
72:             Class.forName("com.mysql.jdbc.Driver").newInstance();
73:             con = DriverManager.getConnection(connectionURL);
74:             
75:         }

.....

root cause
javax.servlet.ServletException: JDBC Driver not found
.....

root cause
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
.....

I think this sort of error occurs because I haven't deployed everything in Tomcat correctly. It would be a huge help if someone could clearly state how to state a resource in the web.xml file, modify the context.xml file (further more I didn't quite understand which context.xml file everyone refers to, because it's different in each answer). This might be a unsuitable question, but please at least point me in the right way. Thanks a lot, in advance.


tried putting it to the places in the answers that I found when i googled this question.

Is the webapp's /WEB-INF/lib folder among them? It should be placed there in this particular case where you're loading the driver straight inside a JSP.

further more I didn't quite understand which context.xml file everyone refers to, because it's different in each answer

That is only for when you're using a connection pooled datasource which is managed by the servletcontainer. But you're manually loading the driver and using DriverManager#getConnection() approach instead of DataSource#getConnection(), so the context.xml is not relevant for you. However, it's recommend to go for this approach. A connection pool greatly improves getConnection() performance and Java code doesn't belong in a JSP file.

See also:

  • How to configure Tomcat to connect with MySQL
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜