JDBC connection not happening [duplicate] - java

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 7 years ago.
Below is the code I used for jdbc connection
String dbUrl="jdbc:mysql://localhost:3306/mysql";
String user= "kumar";
String pwd="ratiol";
try (Connection connection = DriverManager.getConnection(dbUrl, user, pwd)) {
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
but I got error as below-
Exception in thread "main" java.lang.IllegalStateException: Cannot connect the database!
at jdbcConnection.Jdbcdemo.main(Jdbcdemo.java:22)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at jdbcConnection.Jdbcdemo.main(Jdbcdemo.java:19)
Can you please tell me how can I get jdbc url?
I am using eclipse(mars) in ubuntu 14.04

if you are using netbeans, right click project -->properties -->libraries-->add library and select MySQL JDBC Driver

just add the com.mysql.jdbc.Driver to the lib folder in you program files.

This is what you wrote
try (Connection connection = DriverManager.getConnection(dbUrl, user, pwd)) {
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
it would be like this:
Connection con = null;
try {
//registering the jdbc driver here, your string to use
//here depends on what driver you are using.
Class.forName("something.jdbc.driver.YourFubarDriver");
Connection connection = DriverManager.getConnection(dbUrl, user, pwd)
} catch (SQLException e) {
throw new RuntimeException(e);
}
Please check Class.forName not more needed when using JDBC v.4
Let's take a quick look at how we can use this new feature to load a
JDBC driver manager. The following listing shows the sample code that
we typically use to load the JDBC driver. Let's assume that we need to
connect to an Apache Derby database, since we will be using this in
the sample application explained later in the article:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn =
DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
But in JDBC 4.0, we don't need the Class.forName() line. We can
simply call getConnection() to get the database connection.
Note that this is for getting a database connection in stand-alone
mode. If you are using some type of database connection pool to manage
connections, then the code would be different.
There are plenty of reason for the exception No suitable driver found for jdbc:mysql like
Your JDBC URL can be wrong.
ClassPath/BuildPath/Lib folder missing the connector jar.
Driver Information is Wrong.

Just load the driver first:
Class.forName("com.mysql.jdbc.driver");

Related

How to resolve java.sql.SQLException: No suitable driver found for SQLServer 2008 R2?

I have a sample code to connect to SQLServer is given below :
Connection conn=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn= DriverManager.getConnection(" jdbc:sqlserver://localhost:1433;instance=SQLEXPRESS;databaseName=Test" );
System.out.println("connected");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
When I execute this code, I'm getting an exception given below :
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:1433;databaseName=Test
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at JDBCSample.main(JDBCSample.java:14)
I think the jar for SQLServer is not supporting or typo in the connection string.
Download SQLServer 2008 R2 compatible jar from here :
https://www.microsoft.com/en-in/download/details.aspx?id=11774
Steps :
Click on Download :
Select sqljdbc_6.0.8112.200_enu.tar.gz or sqljdbc_6.0.8112.200_enu.zip if shown.
Click on Next to start downloading.
After downloading, extract the content. Now, go into sqljdbc_6.0/enu/jre8 or sqljdbc_6.0/enu/jre7and copy the jar based on the jdk you are using.
Add the jar in the classpath of the project.
Fix this line
Connection conn = DriverManager.getConnection(" jdbc:sqlserver://localhost:1433;databaseName=Test" );
to this by removing space.
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Test");
Seems like you need an JDBC driver supporting your type and version of your SQL Database. Maybe your version of the driver is the wrong one?
You should check this out: https://javarevisited.blogspot.com/2016/09/javasqlsqlexception-no-suitable-driver-mysql-jdbc-localhost.html
What is the DriverManager doing at lines 689 and 270?

Android sqlite jdbc can't connect because of Read-only file system

I'm trying to open an sqlite jdbc connection but I keep getting an exception
java.sql.SQLException: opening db:
'\data\data\com.lppapp.ioi.lpp\databases\lppDB.db': Read-only file
system
Before trying to get the jdbc connection I copy the database into the internal storage and try to access it from there. I'm using this code to connect:
public Connection connect() {
String url = "jdbc:sqlite:\\data\\data\\com.lppapp.ioi.lpp\\databases\\lppDB.db";
Connection conn = null;
try {
Properties config = new Properties();
config.setProperty("open_mode", "1");
//TODO: can't open db (read-only file system) even though directory has a+rw permissions
conn = DriverManager.getConnection(url, config);
System.out.println("Connection successful.");
} catch (SQLException e) {
System.out.println("Connection failed.");
System.out.println(e.getMessage());
}
return conn;
}
I'm using an Android emulator and I've checked the permissions of the databases directory.
I've tried every solution found on the web but with no help.
I've decided to use Android's native SQLite API as mentioned by CommonsWare.
Thanks!

Why does my SQL connection get stuck when I use DriverManager.getConnection, without any error?

I need to connect to my database from a standalone Java application.
I try with this code, but it gets stuck on the DriverManager.getConnection line.
try{
String url = "jdbc:mysql://192.168.2.11:1121/TEST";
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Before");
Connection conn = DriverManager.getConnection(url,"test","test");
System.out.println("After");
} catch (SQLException e ) {
System.err.println("Sql exception! ");
System.err.println(e.getMessage());
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
The output I get is the following:
Before
and nothing else.
This is the db configuration page in MySQL(obviously it works via MySQL)
I'd appreciate any help
Same old question's but without answer
It seems that you are trying to connect to an Oracle database with MySQL driver. According to your configuration, you should change the connection string from jdbc:mysql://192.168.2.11:1121/TEST to jdbc:oracle:thin:#192.168.2.11:1121:1521:xe and use the Oracle driver oracle.jdbc.driver.OracleDriver instead of com.mysql.jdbc.Driver. You can download it here.
As suggested from Nigel Ren, the error was that is it an Oracle db.
I solved editing the code as follow
String url = "jdbc:oracle:thin:#192.168.2.11:1511:XE";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url,"test","test");

Receiving SQLException "Login failed for user" connecting to SQL Server 2008

I am trying to connect to SQL Server 2008 via Java.
I've added sqljdbc4.jar to my project's library.
No username and password is set for database accessing the database (Windows Authentication).
The 1433 port is Listening, but I still receive this exception:
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. ClientConnectionId:085d5df3-ad69-49e1-ba32-b2b990c16a69
Relevant code:
public class DataBases
{
private Connection link;
private java.sql.Statement stmt;
public ResultSet rs;
public DataBases()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB;";
Connection con = DriverManager.getConnection(connectionUrl);
}
catch (SQLException e)
{
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE)
{
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}
}
If you want windows authentication you need to add the option integratedSecurity=true to your JDBC URL:
jdbc:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true
You also need sqljdbc_auth.dll (beware of 32/64 bit) in your Windows system path or in a directory defined through java.library.path
For details see the driver's manual: http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated
I was having same issue when I tried to connect to Microsoft SQL server from Java. I used jTDS driver instead of regular SQLJdbdc Driver.
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String connectionUrl = "jdbc:jtds:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true";
Connection con = DriverManager.getConnection(connectionUrl);
I had the same issue. Its because of the wrong format of the ConnectionUrl. You are missing username and password in the ConnectionUrl.
String ConnectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB","username","password");"
I hope it works well!!!
This post my help:
jdbc.SQLServerException: Login failed for user for any user
You need the integratedSecurity=true flag and make sure the server properties are indeed set to 'Windows Authentication Mode' under Security.

jdbc odbc connectivity without dsn creation

I am trying to make jdbc-Odbc connection without making the DSN but is throwing the exception. I am using the following code and it is throwing data source name not found no default driver specified exception.
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(
"Jdbc:Odbc:Driver={Microsoft Access Driver(*.mdb)}; Dbq=d:/new folder/db1.mdb;");
}
catch(Exception ex)
{
ex.printStackTrace();
}
suggest me whats wrong in this code.thanks in advance

Categories

Resources