使用jsp连接到mySQL数据源的问题
我有一个简短的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
web应用程序的
/WEB-INF/lib
文件夹是否在其中?在这种特殊情况下,当您直接在 JSP 中加载驱动程序时,应该将其放置在那里。这仅适用于您使用 连接池数据源,由 servlet 容器管理。但是您手动加载驱动程序并使用
DriverManager#getConnection()
方法而不是DataSource#getConnection()
,因此context.xml
与您无关。但是,建议采用这种方法。连接池极大地提高了getConnection()
性能和 Java 代码 不属于 JSP 文件。另请参阅:
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.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 ofDataSource#getConnection()
, so thecontext.xml
is not relevant for you. However, it's recommend to go for this approach. A connection pool greatly improvesgetConnection()
performance and Java code doesn't belong in a JSP file.See also: