java不联网怎么安装驱动,如何在Eclipse Web项目中安装JDBC驱动程序,而不会面临java.lang.ClassNotFoundexception...

There is a VERY similar question to mine but in my case I don't have any duplicate jars in my build path, so the solution does not work for me. I've searched google for a couple of hours now, but none of the solutions I've found there actually resolve my issue. I'm creating a web site with some database connectivity for a homework. I'm using a MySQL database, developing in Eclipse and running on Windows.

I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver with the following code:

import java.sql.*;

//...

public void someMethodInMyServlet(PrintWriter out)

{

Connection connection = null;

PreparedStatement query = null;

try {

out.println("Create the driver instance.
");

Class.forName("com.mysql.jdbc.Driver").newInstance();

out.println("Get the connection.
");

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");

query = connection.prepareStatement( "SELECT * FROM customers");

//...

} catch (Exception e)

{

out.println(e.toString()+"
");

}

}

//...

When I run the above code I get the following output:

Create the driver instance.

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

It doesn't get past the Class.forName... line and I can't figure out why! Here is what I did:

Download mysql-connector.

Put it in my MySQL folder C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar.

Opened the project properties in Eclipse.

Add External Jar to my Build Path and I selected mysql-connector-java-5.1.12-bin.jar.

Every time I attempt to use the servlet I get the same error regardless if I have the jar in there or if I don't. Could you help me figure this out?

解决方案

As for every "3rd-party" library in flavor of a JAR file which is to be used by the webapp, just copy/drop the physical JAR file in webapp's /WEB-INF/lib. It will then be available in webapp's default classpath. Also, Eclipse is smart enough to notice that. No need to hassle with buildpath. However, make sure to remove all unnecessary references you added before, else it might collide.

An alternative is to install it in the server itself by dropping the physical JAR file in server's own /lib folder. This is required when you're using server-provided JDBC connection pool data source which in turn needs the MySQL JDBC driver.

See also:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值