设置类路径以便在mysql和jdbc之间进行连接 [英] set the classpath in order to connect between mysql to jdbc
问题描述
我使用的程序:eclipse和mysql。
我在mysql中有一个数据库,我想在eclipse和mysql之间建立连接。
我的项目中有一些我在日食中创建的文件夹。
我有主文件夹:testest ..和我告诉你我在相关文件夹中的内容:
testest
..src
.... testest
...... testestservlet.java
..app engine sdk
..JRE系统库
..referenced library
..war
..lib
.... mysql-connector-java-5.1.22
我更新了我的testestservlet.java:
package testest;
import java.io.IOException;
import javax.servlet.http。*;
import java.sql。*;
@SuppressWarnings(serial)
公共类TestestServlet扩展HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse resp)
抛出IOException {
resp.setContentType( text / plain的);
resp.getWriter()。println(嘿,世界);
resp.getWriter()。println(MySQL Connect Example。);
连接conn = null;
String url =jdbc:mysql:// localhost:3306 /;
String dbName =database_alon;
String driver =com.mysql.jdbc.Driver;
字符串userName =root;
String password =ADMINALON;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName,userName,password);
resp.getWriter()。println(连接到数据库);
conn.close();
resp.getWriter()。println(与数据库断开连接);
} catch(例外e){
e.printStackTrace();
}
}
}
现在,我运行testest运行as-> web application。
输入url:localhost:8888
并且只能看到'你好'世界'。
我读到了这一点,看到我需要写命令:
设置CLASSPATH =%CLASSPATH%; JAVA_HOME \lib;
但我的CLASSPATH和JAVA_HOME的url-s应该是多少?我有以下文件夹:c:/ program_fils / java
和:c:/ Jconnector(类似的东西)。
Jconnector的文件夹包含一个文件夹:mysql-connector-java-5.1.22.jar
我试图通过eclipse将JAR添加到库中(右键单击testest-> properties-> Libraries - >添加外部jar-> mysql-connector-java-5.1.22-bin.jar:
ps,我的操作系统是windows 32bit。
没有必要这样做
设置CLASSPATH =%CLASSPATH %; JAVA_HOME \ lib;
只需添加 mysql-connector-java- 5.1.22-bin.jar:
到 / WEB-INF / lib
目录。
< Eclipse>智能足以识别在
/ WEB-INF / lib
下添加的.jar文件,并且应该包含在CLASSPATH中。 使用添加库添加库外部Jar
也应该有效但不是最佳做法。
在 / WEB-INF中删除.jar使用
.war
在外部部署Web应用程序时,/ lib 也可以正常工作
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:
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
这篇关于设置类路径以便在mysql和jdbc之间进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!