mysql jdbc路径_设置类路径以便在mysql和jdbc之间进行连接

bd96500e110b49cbb3cd949968f18be7.png

I use the programs: eclipse and mysql.

I have a database in mysql and I want to connect between the eclipse to the mysql.

I have some folders in my project that I created in the eclipse.

I have the main folder: testest.. and I show you what I have in the relevant folders:

testest

..src

....testest

......testestservlet.java

..app engine sdk

..JRE system library

..referenced library

..war

..lib

....mysql-connector-java-5.1.22

I updated my testestservlet.java:

package testest;

import java.io.IOException;

import javax.servlet.http.*;

import java.sql.*;

@SuppressWarnings("serial")

public class TestestServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp)

throws IOException {

resp.setContentType("text/plain");

resp.getWriter().println("Hey, world");

resp.getWriter().println("MySQL Connect Example.");

Connection conn = null;

String url = "jdbc:mysql://localhost:3306/";

String dbName = "database_alon";

String driver = "com.mysql.jdbc.Driver";

String userName = "root";

String password = "ADMINALON";

try {

Class.forName(driver).newInstance();

conn = DriverManager.getConnection(url+dbName,userName,password);

resp.getWriter().println("Connected to the database");

conn.close();

resp.getWriter().println("Disconnected from database");

} catch (Exception e) {

e.printStackTrace();

}

}

}

now, I run testest by run as->web application.

enter the url: localhost:8888

and only see the 'hello world'.

I read about that and see that I need to write the command:

set CLASSPATH=%CLASSPATH%;JAVA_HOME\lib;

but what are the url-s of my CLASSPATH and JAVA_HOME should be? I have folders of: c:/program_fils/java

and: c:/Jconnector (something like that).

the folder of Jconnector contains a file of: mysql-connector-java-5.1.22.jar

I tried to add the JAR into the libraries via the eclipse (right click on testest->properties->Libraries->add external jar->mysql-connector-java-5.1.22-bin.jar:

6p3jt.jpg

p.s, my operating system is windows 32bit.

解决方案

There is no need of doing

set CLASSPATH=%CLASSPATH%;JAVA_HOME\lib;

Just add mysql-connector-java-5.1.22-bin.jar: to /WEB-INF/lib directory.

Eclipse is smart enough to recognize .jar files added under /WEB-INF/lib and should include in the CLASSPATH.

Adding a library using "Add External Jar" should also work but its not best practice.

Dropping ".jar" inside /WEB-INF/lib will also work when you externally deploy your web apllication using .war

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值