如何将 MySQL 连接到 Java 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3377151/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:42:37  来源:igfitidea点击:

How to connect MySQL to Java program

javamysqljdbcconnection

提问by

I ve installed MySQL (last update). I need to code, that ll create & establish a connection with SQL DB & manage the DB(using SELECT, INSERT, CREATE).

我已经安装了 MySQL(上次更新)。我需要编写代码,这将创建并建立与 SQL DB 的连接并管理 DB(使用 SELECT、INSERT、CREATE)。

I did everything but, I am not able to create connection. I've also installed the MySQL/J connector, I just extracted the .zippack in a folder & added the folder path in Variables).

我做了一切,但是,我无法创建连接。我还安装了 MySQL/J 连接器,我只是将.zip包解压缩到一个文件夹中并在Variables 中添加了文件夹路径)。

Can anyone tell me wat is meant by URL in the below line?

谁能告诉我 wat 是下面一行中的 URL 的意思吗?

Connection connection = DriverManager.getConnection(url, username, password);

I ve tried this:

我试过这个:

String url = "jdbc:odbc:sqlserver://localhost:3306/myfirstdb";
Connection con = DriverManager.getConnection(url, "root", "1234");

But it's not working. I am unable able to understand the term 'URL'. Can anyone explain, the meaning of 'url' and wat should be done to connect to a SQL server from Java.

但它不起作用。我无法理解“URL”这个词。任何人都可以解释一下,'url' 和 wat 的含义应该用来从 Java 连接到 SQL 服务器。



Update:

更新:

This is the Full code. It still cannot connect.

这是完整的代码。它仍然无法连接。

import java.sql.*;

public class TestDriver {

public static void main(String[] args) {
try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//This s wat actually i did for connection
    System.out.println("Driver Loaded Succesfully");
}
catch (Exception e){
    System.out.println("Unable to Load Driver!!!");
}

try {
    Class.forName(com.mysql.jdbc.Driver");  // initialise the driver
    String url ="jdbc:mysql://localhost:3306/myfirstdb";
    Connection con = DriverManager.getConnection(url, "root", "1234");
    System.out.println("connection Established");
    }
    catch(Exception e) {
                System.out.println("Couldnt get connection");
    }
    }
}

Can you tell me wat is the purpose of MySQL Connector/J?

你能告诉我 wat 是 MySQL Connector/J 的目的吗?

采纳答案by krock

In the question you seem to be using a MySQL jdbc driver with a SQL Server jdbc URL. This won't work.

在问题中,您似乎使用带有 SQL Server jdbc URL 的 MySQL jdbc 驱动程序。这行不通。

If you are using a MySQLdatabase:

如果您使用的是MySQL数据库:

Class.forName("com.mysql.jdbc.Driver");  // initialise the driver

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

If you are using a SQL Server database you are going to need a completely different jdbc driver. jTDSis open source and a good option. Include the jtds.jar file in your classpath and use something like:

如果您使用的是 SQL Server 数据库,您将需要一个完全不同的 jdbc 驱动程序。 jTDS是开源的,是一个不错的选择。在您的类路径中包含 jtds.jar 文件并使用以下内容:

Class.forName("net.sourceforge.jtds.jdbc.Driver");  // initialise the driver

String url = "jdbc:jtds:sqlserver://localhost:1433/myfirstdb";

回答by Zaki

If its MS SQL Server,

如果是 MS SQL Server,

String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(driver);
String url = "jdbc:microsoft:sqlserver://host:1433/database";
Connection conn = DriverManager.getConnection(url, "username", "password");

For more info, see thisto get started with Microsoft JDBC.

You can use any of the two JDBC drivers for MSSQL:

有关详细信息,请参阅此内容以开始使用 Microsoft JDBC。

您可以为 MSSQL 使用两个 JDBC 驱动程序中的任何一个:

For MS SQL Server driver 2.0, use

对于 MS SQL Server 驱动程序 2.0,请使用

URL: jdbc:sqlserver://server:port; DatabaseName=dbname
Class name: com.microsoft.sqlserver.jdbc.SQLServerDriver

For MySql & Java, see thison SO.

对于MySQL和Java的,看到对SO。

回答by BalusC

Here's an extract from your code:

这是您的代码的摘录:

} catch (Exception e) {
    System.out.println("Couldnt get connection");
}

You should neversuppress exceptions as long as you don't understandits cause. Replace it by at least:

只要您不了解其原因,就永远不应抑制异常。至少替换为:

} catch (Exception e) {
    System.out.println("Could not get connection");
    e.printStackTrace();
}

Or maybe

或者可能

} catch (Exception e) {
    throw new RuntimeException("Could not get connection", e);
}

Either way, you should see the exception type, message and trace. In your code snippet the possible exceptions are ClassNotFoundExceptionand SQLException. The first one would mean that the driver is not properly placed in the classpath. The second one would mean that connection cannot be obtained. The exception message and/or trace should tell in detail about the underlying root cause of the problem.

无论哪种方式,您都应该看到异常类型、消息和跟踪。在您的代码片段中,可能的例外是ClassNotFoundExceptionSQLException。第一个意味着驱动程序没有正确放置在类路径中。第二个意味着无法获得连接。异常消息和/或跟踪应详细说明问题的根本原因。

You should always observe exceptions. They tell something about the cause of the problem. You know, once a cause is understood, the solution is nothing more than obvious :)

您应该始终观察异常。他们讲述了问题的原因。您知道,一旦了解原因,解决方案就显而易见了:)

See also:

也可以看看:



Further,

更远,

Can anyone tell me wat is meant by URL in the below line?

谁能告诉我 wat 是下面一行中的 URL 的意思吗?

An URL is an Uniform Resource Locator. It's a common way to locate (identify) unique resources in computer systems and networks. The URL syntax for the MySQL database is explained in the documentation of the JDBC driver.

URL 是统一资源定位符。这是在计算机系统和网络中定位(识别)独特资源的常用方法。MySQL 数据库的 URL 语法在 JDBC 驱动程序文档中进行了解释。

Can you tell me wat is the purpose of MySQL Connector/J?

你能告诉我 wat 是 MySQL Connector/J 的目的吗?

It's the JDBC driver. The JDBC APIexist of almost only interfaces. The DB vendors should provide their own concrete JDBC API implementation, which is the JDBC driver. With a JDBC driver you'll be able to connect a specific database using JDBC API.

它是 JDBC 驱动程序。在JDBC API几乎只存在接口。DB 供应商应该提供他们自己的具体 JDBC API 实现,即 JDBC 驱动程序。使用 JDBC 驱动程序,您将能够使用 JDBC API 连接特定的数据库。

回答by George thant Zin

You forgot a " at Class.forName(com.mysql.jdbc.Driver");It should be

你忘记了“在Class.forName(com.mysql.jdbc.Driver");它应该是

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