user14707212
user14707212

Reputation:

Identifying the right URL to access a database in Java

I'm wondering which would be the right path/url to access the "aliens" database I have in my MySQL Workbench.

enter image description here

enter image description here

As you can see, the connection name is "new connection_1", the user is "root", and the port is "3306". Then, I think the database name is aliens.

Until now I've tried this in my eclipse IDE for java:

    Connection conn = 
        DriverManager.getConnection("jdbc:mysql://localhost:3306/aliens","root", "myDatabase");


System.out.println(conn);

The url is now "jdbc:mysql://localhost:3306/aliens","root", "myDatabase", but i receive the following exception:


Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/aliens
   at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
   at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
   at EdoardoDatabase.main(MainClass.java:9)


Moreover, I would like to print the "connection", nothing else.

Upvotes: 0

Views: 308

Answers (2)

Abhi
Abhi

Reputation: 94

YOu'd need a MySql Connector Jar in your classpath and and a Driver set before initiating the connection.

Class.forName("com.mysql.jdbc.Driver");

You can use maven or download the Driver from Mysql page https://www.mysql.com/products/connector/

Upvotes: 2

Bernd Farka
Bernd Farka

Reputation: 512

your Connection String doesn't look bad at all,

your problem is that Java doesn't know to which driver it should map the mysql driver...

if you are sure you have the Mysql jdbc driver on the ClassPath try

 Class.forName("com.mysql.jdbc.Driver")

before you do you getConnection()

this calls the static{} block in the Driver registers the mysql driver for JDBC Connection lookups via the Factory

Upvotes: 1

Related Questions