0

我通过 WAMP 服务器在 Tomcat 和 MySQL 上运行 JSP。我无法将 JSP 连接到 MySQL。这是代码

<% 
try {
/* Create string of connection url within specified format with machine name, 
port number and database name. Here machine name id localhost and 
database name is usermaster. */ 
String connectionURL = "jdbc:mysql://localhost:3306/database"; 

// declare a connection by using Connection interface 
Connection connection = null; 

// Load JBBC driver "com.mysql.jdbc.Driver" 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 

/* Create a connection by using getConnection() method that takes parameters of 
string type connection url, user name and password to connect to database. */ 
connection = DriverManager.getConnection(connectionURL, "root", "");

// check weather connection is established or not by isClosed() method 
if(!connection.isClosed())
%>
<% 
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
<%
out.println("Unable to connect to database."+ex.toString());
}
%>

我什至放在mysql-connector-java-5.0.8.jar文件/lib夹中。谁能帮我这个?

4

2 回答 2

1

您必须将文件复制到webapp的mysql-connector-java-5.0.8.jar文件夹中。WEB-INF/libcontext

Java EE Web 应用的文件夹结构必须是:

/webapp   <--- This is known as `context` folder
|
|-------/WEB-INF
|       |
|       |-----------/classes
|       | 
|       |-----------/lib
|                    mysql-connector-java-5.0.8.jar
| sample.jsp

要请求 sample.jsp,

http://localhost:here_is_port_number/webapp/sample.jsp

我建议您在 tomcat自动部署文件夹中创建您的网络应用程序 - C:\apache-tomcat-7.0.29\webapps\

于 2012-08-17T03:50:05.327 回答
0

编辑:通过讨论,确定没有找到该类,因为需要重新启动服务器。

如果您能告诉我们异常实际上是什么,那将非常有帮助。

根据您提供的信息,可能有以下几点:

  • 您的 jdbc 资源未在 server.xml 中定义(或任何其他可以定义的位置)
  • 您的连接恢复为空,但您正在调用它的方法。

我也从来没有以这种方式访问​​过资源。通常,我将 JNDI 查找与来自 InitialContext 的数据源一起使用。

于 2012-08-17T03:45:54.077 回答