Java 在intellij idea中使用JDBC/Mysql Connector

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

Use JDBC/Mysql Connector in intellij idea

javamysqljdbcintellij-idea

提问by Nimp

I'm new in Java, and I need to establish a connection to a MySQL server (local), I have add the libraries in Intellij idea but it seems not work, the IDE can't find the class i think... I become crazy I'm searching since two hours... I come from visual studio/c# dev environment and i think that i should miss something...

我是 Java 新手,我需要建立到 MySQL 服务器(本地)的连接,我在 Intellij 想法中添加了库,但似乎不起作用,IDE 找不到我认为的类...我变得疯狂,我从两个小时开始搜索......我来自visual studio/c# dev环境,我认为我应该错过一些东西......

Here you can have a pic from my IDE and the simple code that I wanted use. You can also deduce that I have import the jar in my project (mysql-jdbc).

在这里,您可以从我的 IDE 中获得一张图片以及我想要使用的简单代码。您还可以推断出我在我的项目 (mysql-jdbc) 中导入了 jar。

IDE pic

IDE图片

Edit : here is the code, the comment show where the error appear :

编辑:这是代码,注释显示错误出现的位置:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import org.*;
import com.mysql.jdbc.Driver;


public class JdbcLogin {
    public String Login;
    public String MotDePasse;
    private boolean Logged = false;

    public void StartBdd(){
        String driverName = "com.mysql.jdbc.Driver";
        Class.forName(driverName); // here is the ClassNotFoundException

        String serverName = "localhost";
        String mydatabase = "suptodo";
        String url = "jdbc:mysql://" + serverName + "/" + mydatabase;

        String username = "root";
        String password = "azerty";
        Connection connection = DriverManager.getConnection(url, username, password);
    }
}

采纳答案by Mohammed Mukhtar

You have to add 'mysql:mysql-connector-java:5.1.40' from maven or add it as java library as shown:

您必须从 maven 添加 'mysql:mysql-connector-java:5.1.40' 或将其添加为 java 库,如下所示:

enter image description here

在此处输入图片说明

回答by Li Rao

Solution#1:Drop the mysql-connector-java-version-bin.jar file to your project, right click on it, select "Add as library".

解决方案#1:将 mysql-connector-java-version-bin.jar 文件拖放到您的项目中,右键单击它,选择“添加为库”。

Solution#2:Build and run with command line, for example(Windows)

解决方案#2:使用命令行构建并运行,例如(Windows)

  • javac yourclassname
  • java -cp".;mysql-connector-java-version-bin.jar" yourclassname
  • javac你的类名
  • java -cp".;mysql-connector-java-version-bin.jar" yourclassname

回答by Pronab Roy

It's easy to configure first just open the InteliJ IDE and follow this simple step

首先配置很容易,只需打开 InteliJ IDE 并按照这个简单的步骤操作

File->Project Structure->Libraries

Then Click on the plus(+) sign and select From Meven....enter image description here

然后单击加号(+)并选择 From Meven....在此处输入图片说明

After you'll get a search box there you should put

在你得到一个搜索框之后,你应该把

mysql:mysql-connector-java:5.1.40

mysql:mysql-connector-java:5.1.40

enter image description here

在此处输入图片说明

This will solve the issue

这将解决问题

回答by Mubarak Tahir

If it says time zone unrecognized you can add this piece of code to the URL:

如果它说时区无法识别,您可以将这段代码添加到 URL:

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost?useTimezone=true&serverTimezone=UTC","USER-NAME","PASSWORD");

Don't forget to wrap it with a try-catch.

不要忘记用 try-catch 包裹它。