Connect to MySQL from Java [duplicate] - java

This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 7 years ago.
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?

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:
Connect Java to a MySQL database
SQLException: No suitable driver found

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]...
...

Related

Where exactly should JDBC URL value be changed?

I'm trying to run some application and get the following error:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for
JDBC]Can't start a cloned connection while in manual transaction mode.
I know that I should add the parameter ;SelectMethod=Cursor to your JDBC URL
But I'm having problem understanding where exactly should I change it? Should it be some conf file in JDBC driver folder somewhere? Or can I do it from sql management studio?
Also is there some easy way to determine if and what version of JDBC driver I have?
Help is very much appreciated!
You specify the URL when creating your JDBC connection, e.g.:
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]",
username,
password);
Of course you have to replace the stuff in the brackets with your values.
Quite the same is true for every other tool (e.g. IntelliJ, Eclipse) I know of that connects to a DB via JDBC. See e.g. attached screenshot. Here you also specify the connection parameters via the JDBC URL.

Connection from a Java program to a SQL Developer database

I would like to access to a Oracle database (SQL Developer) from a Java program. I never used JDBC before.
Here is what i wrote:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:host_name:port:database_name";
Connection con = DriverManager.getConnection(url, login, passwd);
I got an error:
[Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified
Host name, port, DB name and logins are good.
Is this driver OK to communicate with SQL Developer ?
I don't know what to do,
thanks for helping !
Try this
Class.forName ("oracle.jdbc.driver.OracleDriver");
for Oracle you can use ojdbc
Class.forName("oracle.jdbc.driver.OracleDriver");
for SQL Server u can use jtds
Class.forName("net.sourceforge.jtds.jdbc.Driver");
The JDBC driver sun.jdbc.odbc.JdbcOdbcDriver is bridge driver that wraps an ODBC driver as described here.
SQL Developer is an Oracle tool that acts as an IDE against the Oracle database.
To connect Java to an Oracle database you should obtain the Oracle JDBC driver and ensure the jar is on your classpath (as described in the documentation for java.sql.DriverManager, forcing the class to be loaded is no longer necessary).
The important bit is the connection string, which in its simplest form for Oracle should follow the structure:
jdbc:oracle:thin:#//host:port/service
Where:
host: the hostname of the machine running Oracle
port: the port that Oracle is listening for connections on
service: the database instance to connect to
The full docs are here.

How to connect ms sql-server using jdbc?

I have a Tomcat 7 and javascript application in here. Now i add jsp wiht connection to MS SQL-Server R2 data base.
In jsp file i have:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn= null;
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=dd_ugra";
conn = DriverManager.getConnection(url,"sa", "123456");
After i download jdbc for SQL-Server from Microsoft site and copy sqljdbc4.jar into tomcat\lib folder but when i start application i get error:
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
In another case i try to use:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
And get another error:
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ugra
How to correctly connect to SQL-Server?
I think you made mistake on connection string, it should be something like
String connectionUrl = "jdbc:sqlserver://serverA:1433;" +
"databaseName=AdventureWorks;integratedSecurity=true;" +
"failoverPartner=serverB";
in the 2nd case, you may take it as 'jdbc:microsoft:sqlserver://'
And the JDBC jar should be exist in Web content/WEB-INF/

Java ODBC and Microsoft.Jet.OLEDB.4.0

I want to access a database using a connection string that is given by a third party application. I have one example configuration that has a connection string like the following:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\path\to\theDatabase.mdb;Persist Security Info=False
Calling
DriverManager.getConnection("jdbc:odbc:" + connectionString);
gives me an SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
The third party application can access the database without problems.
The OS is Windows XP Service Pack 3 and up to date.
The msjet40.dll in system32 folder has version 4.0.9511.0 (up to date according to http://support.microsoft.com/kb/239114/en-us)
The file exists and I can access it using jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};C:\path\to\theDatabase.mdb
I just don't know what I'm doing wrong.
Problem is in your odbc connection
To connect access database try following
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:connSource");
goto ControlPanel->AdministrativeTools->DataSource(ODBC)->System DSN->ADD->MicrosoftAccess->
then in the name field give the Source Name as connSource.
you have to use this name instead of database name in your DriverManager.getConnection method.
Because getConnectionMethod take the source name not the database name. so your code is not working.
This might be a problem. I don't know of any JDBC drivers for OLE DB data sources. Here on SO this questions sits without answers from March: https://stackoverflow.com/questions/5184046/jdbc-oledb-bin .
Refer to the wesite below it contains connection strings of all variants for all the databases
http://www.connectionstrings.com/

Connecting to MS SQL DB from Java Netbeans

How do I connect to MS Sql database using Java.
I am using the jtds jdbc driver but keep getting the "No Suitable Driver" error. I have checked the jdbc URL and it conforms to the URL format specified in the documentation.
I am running on JRE6.
Have you actually loaded the driver class?
Example for MySQL:
Class.forName("com.mysql.jdbc.Driver");
before connecting:
Connection con = DriverManager.getConnection(url,"user", "pw");
I have actually found a solution. One can use the microsoft SQL server jdbc driver
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = " jdbc:microsoft:sqlserver://serverjag:1433";
Connection conn = DriverManager.getConnection(url,"sa","");

Categories

Resources