Java 如何安装JDBC以及如何使用它连接到mysql?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3182282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to install JDBC and how to use it to connect to mysql?
提问by Mahmoud
i am trying to install JDBC but i dont know how, when you only have the jar file, i copied it to my java ext folder but it keep giving me an error, can anyone show me how to complete install the driver and use it?
我正在尝试安装 JDBC,但我不知道如何,当您只有 jar 文件时,我将它复制到我的 java ext 文件夹,但它一直给我一个错误,谁能告诉我如何完成安装驱动程序并使用它?
below is the codes that i used
下面是我使用的代码
import java.sql.*;
public class Test1
{
public static void main (String[] args)
{
String url = "jdbc:mysql://localhost:3306/sabayafr_sabmah";
String username = "root";
String password = "ma";
Connection connection = null;
try {
System.out.println("Connecting database...");
connection = DriverManager.getConnection(url, username, password);
System.out.println("Database connected!");
} catch (SQLException e) {
System.err.println("Cannot connect the database!");
e.printStackTrace();
} finally {
System.out.println("Closing the connection.");
if (connection != null) {
try {
connection.close();
} catch (SQLException ignore) {
}
}
}
}
}
And below is the Response that i get
下面是我得到的回应
Cannot connect to database server
Update # 3
更新 #3
C:\Users\AlAsad\Desktop>java -cp .;mysql-connector-java-5.0.8-bin.jar Test1
Connecting database...
Cannot connect the database!
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
sabayafr_sabmah
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Test1.main(Test1.java:12)
Closing the connection.
采纳答案by BalusC
You're trying to connect MySQLwith the URL of a jTDS JDBC driverwhich is designed specifically for Microsoft SQL Server. This ain't ever going to work. Even not when you fix the current problem by placing the JAR file in classpath.
您正在尝试将MySQL与专为Microsoft SQL Server设计的jTDS JDBC 驱动程序的 URL连接。这永远不会奏效。甚至当您通过将 JAR 文件放在类路径中来解决当前问题时也不行。
You really need the MySQL JDBC driver. Also see this answerfor a short but complete tutorial
您确实需要MySQL JDBC 驱动程序。另请参阅此答案以获取简短但完整的教程
回答by OscarRyz
Try putting your .jar file in the classpath.
尝试将您的 .jar 文件放在classpath 中。
回答by Andreas Dolk
The library that contains the Driver (net.sourceforge.jtds.jdbc.Driver
) needs to be on the classpath.
包含 Driver ( net.sourceforge.jtds.jdbc.Driver
)的库需要在类路径上。
Assuming you start your application with
假设你开始你的应用程序
java Test1
then simply do
然后简单地做
java -cp .;driver.jar Test1
where 'driver.jar' should be exchanged with the filename (relative or full path) of your database driver lib.
其中“driver.jar”应该与数据库驱动程序库的文件名(相对或完整路径)交换。
EDIT
编辑
A classpath tutorial will exceed the comments section below this question. Please take a cup of coffee and look at this page. It will most likely help you to continue.
类路径教程将超出此问题下方的评论部分。请喝杯咖啡看看这个页面。它很可能会帮助您继续。
回答by npinti
On the other hand, if you are using an IDE such as Netbeans or Eclipse you can add the jar file as a resource to the project.
另一方面,如果您使用诸如 Netbeans 或 Eclipse 之类的 IDE,您可以将 jar 文件作为资源添加到项目中。
回答by duffymo
You certainly have JDBC problems, but the exception isn't telling you that. Read it again:
您肯定有 JDBC 问题,但异常并没有告诉您这一点。再读一遍:
Exception in thread "main" java.lang.NoClassDefFoundError: Test1
Caused by: java.lang.ClassNotFoundException: Test1
It's yourTest1.class that it can't find, not the JDBC driver.
它找不到您的Test1.class,而不是 JDBC 驱动程序。
You should not be copying anything into the jre/lib/ext directory. That's for library extensions, not JDBC JARs. It wasn't meant as a crutch for people who don't understand how CLASSPATH works.
您不应将任何内容复制到 jre/lib/ext 目录中。这是用于库扩展,而不是 JDBC JAR。对于不了解 CLASSPATH 工作原理的人来说,它并不是一个拐杖。
I'd write it more like the following. Those close methods will come in handy.
我会更像下面这样写。那些关闭的方法会派上用场。
When I run it on my machine, adding the MySQL JDBC JAR to my CLASSPATH, I get the following result:
当我在我的机器上运行它时,将 MySQL JDBC JAR 添加到我的 CLASSPATH,我得到以下结果:
C:\java -classpath .\mysql-connector-java-5.1.6-bin.jar; persistence.utils.DatabaseUtils
product: MySQL
version: 5.1.24-rc-community
major : 5
minor : 1
Here is the source code:
这是源代码:
package persistence.utils;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseUtils
{
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/contacts";
public static final String USERNAME = "contacts";
public static final String PASSWORD = "contacts";
public static void main(String[] args)
{
Connection connection = null;
try
{
String driver = ((args.length > 0) ? args[0] : DRIVER);
String url = ((args.length > 1) ? args[1] : URL);
String username = ((args.length > 2) ? args[2] : USERNAME);
String password = ((args.length > 3) ? args[3] : PASSWORD);
connection = getConnection(driver, url, username, password);
DatabaseMetaData metaData = connection.getMetaData();
System.out.println("product: " + metaData.getDatabaseProductName());
System.out.println("version: " + metaData.getDatabaseProductVersion());
System.out.println("major : " + metaData.getDatabaseMajorVersion());
System.out.println("minor : " + metaData.getDatabaseMinorVersion());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
close(connection);
}
}
public static Connection getConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException
{
Connection connection = null;
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
return connection;
}
public static void close(Connection connection)
{
try
{
if (connection != null)
{
connection.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void close(Statement statement)
{
try
{
if (statement != null)
{
statement.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void close(ResultSet resultSet)
{
try
{
if (resultSet != null)
{
resultSet.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void rollback(Connection connection)
{
try
{
if (connection != null)
{
connection.rollback();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
回答by user207421
'I am trying to install JDBC'
“我正在尝试安装 JDBC”
You don't have to install JDBC. It is part of the JDK & JRE.
您不必安装 JDBC。它是 JDK 和 JRE 的一部分。