0

Today I started to work with connecting databases to java and without much knowledge I started to work with a few tools I found in the internet. My programms are eclipse and MYSQL. I already got the MYSQL-App and created a new java-programm which builds the SQL-Plugin. When I try to use some databases in Java, there is an error which says

"No suitable driver found for jdbc:mysql://localhost:3306/world".

World ist a "demo-database" from MYSQL. When I open the MYSQL-Workbench it already says hat there is a Server running on my PC. As you can see my "url" is "jdbc:mysql://localhost:3306/world". I already tried a few different URLs and other things. I think there could be a problem with my Code (wrong URL...), I messed up starting a server or I built the external jar wrong.

To conclude my question is: How should I choose my URL to connect the Database stored on MYSQL-localserver? Or are there any other possibilities to use my database in java?

This is my actual code:

package pack1;

import java.sql.*;

public class Main {

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/world";
        String user = "root";
        String password = "";

        
        try (Connection conn = DriverManager.getConnection(url, user, password)) { 
            System.out.println("Success");
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
        }
    }

}
andrewJames
  • 19,570
  • 8
  • 19
  • 51
Jan S.
  • 1
  • 1

1 Answers1

0

When it comes to my webapps I use Spring framework. There is a lot of tools that handle connecting to DB for me.

I usually use templates so it speeds up job of mine. At https://start.spring.io I create a sample and add dependecies I want to work with. Also - I have to remember about setting all data in application.properties (sample of application.properties for H2 and MySql DB below)

# DATABASE CONFIGURATION
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:h2:mem:mycoolapp
spring.main.banner-mode=off

MySql

spring.datasource.url=jdbc:mysql://localhost:3306/new_project? 
serverTimezone=Europe/London&useSSL=False&DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
Fishy_Code
  • 49
  • 8
  • Thank you for the fast answer, but I cant really understand what the Spring framework does for me in Java when I want to use my Database. Shouldnt be there an easier way to connect the database I created to my Java project and use it for whatever purpose? – Jan S. Dec 26 '20 at 15:28
  • It depends on that what you need. F.E. Spring helps me a lot to handle retrieve all data from DB, find data by ID, add new data, modify data and delete it (best example where it is usefull is classical REST App - like web app for library to verify do they have the book you need or update books for library and etc. If you need anything - PM me - I will do my best to share my knowledge). – Fishy_Code Dec 26 '20 at 15:57
  • If you want to check for more data - check this link in here. It might containt answears you are looking for https://spring.io/guides/gs/accessing-data-mysql/ - here is accessing mySql DB :) – Fishy_Code Dec 26 '20 at 16:01
  • How to PM on Stackoverflow? – Jan S. Dec 26 '20 at 16:10