首页 > 解决方案 > 如何启动 SQL Server?

问题描述

我是 SQL 新手,我正在做一个需要 SQL 的学校项目。我已经在名为的数据库上创建了表,pizzeriaGennarino并创建了一个应该连接到数据库的 java 程序,但我不知道如何连接到 MySql 服务器。

更新:
我已经安装了驱动程序,但是当我运行 java 应用程序时它会生成一个新的异常。我正在使用 SQL Workbench,你可以在这里看到它的图像:


这里

更新的 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

标签: javamysql

解决方案


这里有几件事,

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

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

现在,如果您使用 Gradle 或 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 文件的依赖项部分。Maven 会有类似的部分。


推荐阅读