0

I am new to SQL and I am doing a school project that needs SQL. I have created the tables on the database named pizzeriaGennarino and I created a java program that should connect to the database, but I don't know how to connect to the MySql server.

UPDATE:
I have installed the driver but it generates a new exception when i run the java app. I am using SQL Workbench, you can see an image of it here:


here

UPDATED JAVA CODE:

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");

    }
}

UPDATED EXCEPTION:

 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
Andrea Dattero
  • 503
  • 4
  • 20
  • 1
    Have you added jar with driver to classpath of your application? Also you need to close connections/statements before exiting the program – Ivan Oct 18 '18 at 17:43
  • If you've created tables then you already know how to start the server. I think your question is how to connect properly. Like @Ivan and your error say, it seems like you might not have the MySQL JDBC driver that you need to connect to your MySQL server. – xtratic Oct 18 '18 at 17:48
  • If you don't have the MySQL JDBC driver, you can find it [here](https://dev.mysql.com/downloads/connector/j/8.0.html) – jhenrique Oct 18 '18 at 17:48
  • I have a new problem check the updated question please – Andrea Dattero Oct 21 '18 at 12:18

1 Answers1

0

Few things here,

First you need to add the MySQL driver to your project. Please see here for the latest MySQL JDBC connector from the MySQL project. Once the project has the correct driver you can begin to use it to connect and manipulate your database.

Secondly, the server itself (MySQL) usually runs as a service. If you can create the tables, it means your database server is up and running. The code you write is to manipulate the database itself. Providing the server is up and running and you have the correct driver installed, you're all set.

Now, if you are using Gradle, or Maven the driver installation can be quite easy:

Maven:

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

Gradle:

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

Personally I use gradle so I would add the above gradle snippet to the dependencies section of your build.gradle file. Maven would have a similar section.

Robert H
  • 10,659
  • 17
  • 63
  • 102
  • where should i add those lines? i am not working with gradle or maven(i know that Android Studio use them but i know nothing about them). – Andrea Dattero Oct 20 '18 at 15:11