Java 使用jdbc连接mysql工作台

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

Connection with mysql workbench using jdbc

javamysqljdbc

提问by jigisha

i am using Mysql workbench with java.i have installed JDBC driver from ubantu software center.but connection can not be done using "jdbc:mysql://127.0.0.1:3306/test".

Is there any class path required to be set or anything else is going wrong?

我正在使用带有 java.i 的 Mysql 工作台。我已经从 ubantu 软件中心安装了 JDBC 驱动程序。但是使用"jdbc:mysql://127.0.0.1:3306/test".

是否需要设置任何类路径或其他任何问题?

回答by Java Man

use this :

用这个 :

Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password");

you forget to add Class.forname

你忘记添加 Class.forname

Thanks..

谢谢..

回答by Balram Maurya

You have to follow following steps in a sequence to make connection with MySql Database.

您必须按顺序执行以下步骤才能与 MySql 数据库建立连接。

1) installed MySQL on your system. 2) download MySQL JDBC driver jar to add in project library. 3) create database and tables using command line or using workbench. 4) now use in your code to make connection :

1) 在您的系统上安装 MySQL。2) 下载 MySQL JDBC 驱动 jar 添加到项目库中。3)使用命令行或使用工作台创建数据库和表。4)现在在您的代码中使用以建立连接:

  Class.forName("com.mysql.jdbc.Driver");
  // Setup the connection with the DB
  connect = DriverManager
      .getConnection("jdbc:mysql://localhost:3306/test", "username", "password"); 
  statement = connect.createStatement();

5) now perform your queries using Statement to update or fetch the data. 6) in last close the connection.

5) 现在使用 Statement 执行查询以更新或获取数据。6)最后关闭连接。

回答by Waleed Ghazi

Try to copy mysql-connector jar file to C:\Program Files(x86)\Java\jre\lib\ext then copy this pass to ClassPath in environment variables

尝试将 mysql-connector jar 文件复制到 C:\Program Files(x86)\Java\jre\lib\ext 然后将此传递复制到环境变量中的 ClassPath

回答by user207421

I am using MySQL Workbench with Java.

我正在将 MySQL Workbench 与 Java 一起使用。

No you aren't. You are using MySQL Workbench, and you are using Java, but you aren't using one 'with' the other in any sense.

不,你不是。您正在使用 MySQL Workbench,并且您正在使用 Java,但是您并没有在任何意义上“与”另一个一起使用。

  • MySQL Workbench is a client. JDBC is another client. A client cannot connect to a client.

  • MySQL Workbench is not a JDBC client. A non-JDBC client cannot use a JDBC connection string to connect to a MySQL database.

  • MySQL Workbench 是一个客户端。JDBC 是另一个客户端。客户端无法连接到客户端。

  • MySQL Workbench 不是 JDBC 客户端。非 JDBC 客户端无法使用 JDBC 连接字符串连接到 MySQL 数据库。

In short, your question doesn't make sense.

简而言之,你的问题没有意义。