-1

I have a website, and I have a database in MySQL at that website as well. I can handle my database from phpMyAdmin online and it has name "dbPersons";

However I want to connect to that database from Java. I have downloaded JDBC, but I can't connect because of my connection string. I use the following connection string

String mySQLCredecials = "jdbc:mysql:thin:" + user + "/" + pass + "@jdbc:mysql:www.websiteName.net:2222/dbPersons";

Do you know what I am doing wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Anonymous
  • 183
  • 1
  • 2
  • 9
  • And what are you doing with your string? – Werner Kvalem Vesterås Dec 30 '12 at 21:52
  • What error message do you see? – tobiasbayer Dec 30 '12 at 21:52
  • java.sql.SQLException: No suitable driver found for jdbc:mysql:thin:sampleUser/sampleUserPassword@jdbc:mysql:www.websiteName.net:2222/dbPersons at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at SQLcmds.DBConnector.connect(DBConnector.java:75) at SQLcmds.DBConnector.(DBConnector.java:38) at MainClass.main(MainClass.java:11) – Anonymous Dec 30 '12 at 21:57

2 Answers2

8

Just look in the MySQL JDBC driver documentation for the proper syntax.

21.3.5.1. Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J

...

JDBC URL Format

The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:

jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306, the default port number for MySQL servers.

jdbc:mysql://[host:port],[host:port].../[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

Here is a sample connection URL:

jdbc:mysql://localhost:3306/sakila?profileSQL=true

Yours is thus clearly wrong. The presence of :thin suggests that you were incorrectly reading the Oracle JDBC driver documentation instead of the MySQL one. Use the following JDBC URL:

String url = "jdbc:mysql://www.websiteName.net:2222/dbPersons";

And obtain the connection as follows:

Connection connection = DriverManager.getConnection(url, user, pass);

The SQLException: No suitable driver simply means that the given JDBC connection URL is not recognized by any of the so far loaded drivers. So apart from the wrong connection URL, another possible cause is that the JDBC driver supporting the URL isn't been loaded at all.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

I think you need to use a correct JDBC URL, yours is not correct.

Read here: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html

JDBC URL Format

The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:

jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

...

burna
  • 2,932
  • 18
  • 27