使用jsp连接到mySQL数据源的问题

发布于 2024-11-15 17:59:03 字数 1583 浏览 2 评论 0原文

我有一个简短的 jsp 页面,尝试建立与名为“test”的 mySQL 数据库的连接。我尝试在 Tomcat 中正确部署它,但没有成功。所有其他 jsp 页面都运行良好。该页面执行时会出现错误。我还是一名学生&我正在尝试对此进行实验。我已经得到了 mysql-connector-java-5.1.16-bin.jar &尝试将其放在我用谷歌搜索这个问题时找到的答案中的位置。 jsp页面包括;

<%@ 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);
        }

错误是;

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
.....

我认为发生这种错误是因为我没有正确部署 Tomcat 中的所有内容。如果有人能够清楚地说明如何在 web.xml 文件中声明资源,修改 context.xml 文件,这将是一个巨大的帮助(此外,我不太明白每个人都指的是哪个 context.xml 文件,因为它是不同的在每个答案中)。这可能是一个不合适的问题,但请至少指出我正确的方式。非常感谢,提前。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ可否回来 2024-11-22 17:59:03

尝试将其放在我在谷歌上搜索此问题时找到的答案中的位置。

web应用程序的 /WEB-INF/lib 文件夹是否在其中?在这种特殊情况下,当您直接在 JSP 中加载驱动程序时,应该将其放置在那里。

此外,我不太明白每个人都指的是哪个 context.xml 文件,因为每个答案都不同

这仅适用于您使用 连接池数据源,由 servlet 容器管理。但是您手动加载驱动程序并使用 DriverManager#getConnection() 方法而不是 DataSource#getConnection(),因此 context.xml与您无关。但是,建议采用这种方法。连接池极大地提高了 getConnection() 性能和 Java 代码 属于 JSP 文件。

另请参阅:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文