This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
                                
                                    (13个回答)
                                
                        
                                2年前关闭。
            
                    
我刚接触SQL,正在做一个需要SQL的学校项目。
我已经在名为pizzeriaGennarino的数据库上创建了表,并且创建了一个应连接到数据库的Java程序,但是我不知道如何连接到MySql服务器。

更新:

我已经安装了驱动程序,但是在运行Java应用程序时会生成新的异常。
我正在使用SQL Workbench,您可以在此处查看其图像:



java - 如何启动SQL Server?-LMLPHP

更新的JAVA代码:

import java.sql.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
          Connection connect = DriverManager
              .getConnection("jdbc:mysql://localhost:3306/pizzeriaGennarino","theusername","thepassword");

    }
}


更新例外:

 Exception in thread "main" java.sql.SQLException: The server time zone value 'CEST' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:832)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at Main.main(Main.java:7)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'CEST' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
    at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
    at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2236)
    at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2260)
    at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1314)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:963)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:822)
    ... 6 more

最佳答案

这里很少

首先,您需要将MySQL驱动程序添加到您的项目中。有关MySQL项目中最新的MySQL JDBC连接器,请参见here。一旦项目有了正确的驱动程序,您就可以开始使用它来连接和操作数据库。

其次,服务器本身(MySQL)通常作为服务运行。如果可以创建表,则意味着数据库服务器已启动并正在运行。您编写的代码是操纵数据库本身。只要服务器已启动并正在运行,并且您已安装了正确的驱动程序,便一切就绪。

现在,如果您使用的是Gradle或Maven,则驱动程序的安装非常简单:

Maven:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.12</version>
</dependency>


摇篮:

// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.12'


我个人使用gradle,因此将上述gradle片段添加到build.gradle文件的dependencies部分。 Maven会有类似的部分。

08-17 13:35
查看更多