什么是 MySQL JDBC 驱动程序连接字符串?

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

What is the MySQL JDBC driver connection string?

mysqljdbcconnection-string

提问by

I am new to JDBC and I am trying to make a connection to a MySQL database. I am using Connector/J driver, but I cant find the JDBC connection string for my Class.forName()method.

我是 JDBC 的新手,我正在尝试连接到 MySQL 数据库。我正在使用 Connector/J 驱动程序,但我找不到我的Class.forName()方法的 JDBC 连接字符串。

回答by Langali

Assuming your driver is in path,

假设您的驱动程序在路径中,

String url = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection conn = DriverManager.getConnection (url, "username", "password");

回答by Tim Sylvester

Have you read the documentation?

你读过文档吗?

https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

A basic connection string looks like:

一个基本的连接字符串如下所示:

jdbc:mysql://localhost:3306/dbname

The class.forName string is "com.mysql.jdbc.Driver", which you can find (edit: now on the same page).

class.forName 字符串是“com.mysql.jdbc.Driver”,您可以找到它(编辑:现在在同一页面上)。

回答by Fintan Kearney

"jdbc:mysql://localhost"

From the oracle docs..

从 oracle 文档..

jdbc:mysql://[host][,failoverhost...]
[:port]/[database]
[?propertyName1][=propertyValue1]
[&propertyName2][=propertyValue2]

host:portis the host name and port number of the computer hosting your database. If not specified, the default values of host and port are 127.0.0.1 and 3306, respectively.

host:port是托管数据库的计算机的主机名和端口号。如果不指定,host 和 port 的默认值分别为 127.0.0.1 和 3306。

databaseis the name of the database to connect to. If not specified, a connection is made with no default database.

database是要连接的数据库的名称。如果未指定,则建立一个没有默认数据库的连接。

failoveris the name of a standby database (MySQL Connector/J supports failover).

故障转移是备用数据库的名称(MySQL Connector/J 支持故障转移)。

propertyName=propertyValuerepresents an optional, ampersand-separated list of properties. These attributes enable you to instruct MySQL Connector/J to perform various tasks.

propertyName=propertyValue表示可选的、以与号分隔的属性列表。这些属性使您能够指示 MySQL Connector/J 执行各种任务。

回答by Krishnan Devarajan

It is very simple :

这很简单:

  1. Go to MySQL workbench and lookup for Database > Manage Connections
  2. you will see a list of connections. Click on the connection you wish to connect to.
  3. You will see a tabs around connection, remote management, system profile. Click on connection tab.
  4. your url is jdbc:mysql://<hostname>:<port>/<dbname>?prop1etc. where <hostname>and <port>are given in the connection tab.It will mostly be localhost : 3306. <dbname>will be found under System Profile tab in Windows Service Name. Default will mostly be MySQL5<x>where x is the version number eg. 56 for MySQL5.6 and 55 for MySQL5.5 etc.You can specify ur own Windows Service name to connect too.
  5. Construct the url accordingly and set the url to connect.
  1. 转到 MySQL 工作台并查找数据库 > 管理连接
  2. 您将看到一个连接列表。单击要连接的连接。
  3. 您将看到围绕连接、远程管理、系统配置文件的选项卡。单击连接选项卡。
  4. 您的网址是jdbc:mysql://<hostname>:<port>/<dbname>?prop1等在那里<hostname>,并<port>在连接tab.It给出将主要是本地主机:3306<dbname>将系统配置文件选项卡下的Windows服务名称被发现。默认值主要是 MySQL5 <x>,其中 x 是版本号,例如。MySQL5.6 为 56,MySQL5.5 为 55 等。您也可以指定您自己的 Windows 服务名称进行连接。
  5. 相应地构造 url 并设置要连接的 url。

回答by Arun Kumar N

For Mysql, the jdbc Driver connection string is com.mysql.jdbc.Driver. Use the following code to get connected:-

对于 Mysql,jdbc 驱动程序连接字符串是com.mysql.jdbc.Driver。使用以下代码进行连接:-

class DBConnection {
   private static Connection con = null;
   private static String USERNAME = "your_mysql_username";
   private static String PASSWORD = "your_mysql_password";
   private static String DRIVER = "com.mysql.jdbc.Driver";
   private static String URL = "jdbc:mysql://localhost:3306/database_name";

   public static Connection getDatabaseConnection(){
       Class.forName(DRIVER);
       return con = DriverManager.getConnection(URL,USERNAME,PASSWORD);
   }
}

回答by Nem?t Abdullayev

update for mySQL 8 :

mySQL 8 的更新:

String jdbcUrl="jdbc:mysql://localhost:3306/youdatabase?useSSL=false&serverTimezone=UTC";

String jdbcUrl="jdbc:mysql://localhost:3306/youdatabase?useSSL=false&serverTimezone=UTC";

回答by Rathore

As the answer seems already been answered, there is not much to add but I would like to add one thing to the existing answers. This was the way of loading class for JDBC driver for mysql

由于答案似乎已经得到解答,因此没有什么可添加的,但我想在现有答案中添加一件事。这是为 mysql 的 JDBC 驱动程序加载类的方式

com.mysql.jdbc.Driver

But this is deprecated now. The new driver class is now

但这现在已被弃用。新的驱动程序类现在是

com.mysql.cj.jdbc.Driver

Also the driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

此外,驱动程序通过 SPI 自动注册,通常不需要手动加载驱动程序类。

回答by ARiyou Jahan

it's depends on what service you're using.

这取决于您使用的服务。

if you use MySQL Workbench it wold be some thing like this :

如果您使用 MySQL Workbench,它将是这样的:

jdbc:mysql://"host":"port number"/

jdbc:mysql://"host":"端口号"/

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

And of course it will be different if you using SSL/SSH.

当然,如果您使用 SSL/SSH,情况会有所不同。

For more information follow the official link of Jetbriens (intelliJ idea) :

有关更多信息,请访问 Jetbriens (intelliJ idea) 的官方链接:

Connecting to a database #

连接到数据库#

https://www.jetbrains.com/help/idea/connecting-to-a-database.html

https://www.jetbrains.com/help/idea/connecting-to-a-database.html



Configuring database connections #

配置数据库连接#

https://www.jetbrains.com/help/idea/configuring-database-connections.html

https://www.jetbrains.com/help/idea/configuring-database-connections.html

回答by NSC

protocol//[hosts][/database][?properties]

协议//[主机][/数据库][?属性]

If you don't have any properties ignore it then it will be like

如果你没有任何属性忽略它那么它会像

jdbc:mysql://127.0.0.1:3306/test

jdbc:mysql://127.0.0.1:3306/test

jdbc:mysql is the protocol 127.0.0.1: is the host and 3306 is the port number test is the database

jdbc:mysql 是协议 127.0.0.1: 是主机 3306 是端口号 test 是数据库

回答by Optimizer

String url = "jdbc:mysql://localhost:3306/dbname";
String user = "user";
String pass = "pass";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection conn = DriverManager.getConnection (url, user, pass);

3306is the default port for mysql.

3306是mysql的默认端口。

If you are using Java 7 then there is no need to even add the Class.forName("com.mysql.jdbc.Driver").newInstance ();statement.Automatic Resource Management (ARM) is added in JDBC 4.1 which comes by default in Java 7.

如果您使用的是 Java 7,则甚至不需要添加该Class.forName("com.mysql.jdbc.Driver").newInstance ();语句。自动资源管理 (ARM) 已在 Java 7 中默认提供的 JDBC 4.1 中添加。

The general format for a JDBC URL for connecting to a MySQL server is as follows, with items in square brackets ([ ]) being optional:

用于连接 MySQL 服务器的 JDBC URL 的一般格式如下,方括号 ([]) 中的项目是可选的:

jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] ?
[?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]