I am trying to connect to an existing DB which is host on a ESXI server and it seems I cannot properly locate the driver.
The code I use is the following:
new ConnectToDb('jdbc:sqlserver:sqltest3:CI-ESXI', 'USER', 'PASSWORD', 'com.microsoft.sqlserver.jdbc.SQLServerDriver')
ConnectToDb(String url, String user, String password, String driver) {
Class.forName(driver)
database = groovy.sql.Sql.newInstance(url, user, password, driver)
}
I obtain the following error:
Exception in thread "main" java.sql.SQLException: No suitable driver
found for jdbc:sqlserver:sqltest3:CI-ESXI
Also, I tried the following, java style, code:
void connect(String url, String user, String password, String driver) throws SQLException {
if (null == stmt || stmt.isClosed()) {
// Create a connection to the database
if (database_url == null) {
throw new SQLException("Cannot connect to database, connection URL is null.")
}
Properties database_infos = new Properties()
database_infos.put("user", user)
database_infos.put("password", password)
database_infos.put("driver", driver)
Class.forName(driver)
connection = DriverManager.getConnection(url, database_infos)
stmt = connection.createStatement()
}
}
Which returns the same exception.
Actually, a direct call to DriverManager.getDriver(driver) returns the same exception.
The driver is added to the POM.xml file the following way:
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.4.1.jre8</version>
</dependency>
It is added to the classpath and I can import it in any java file in the following way import com.microsoft.sqlserver.jdbc.SQLServerDriver without error
I am working using IntelliJ IDEA in a Groovy project but I can reproduce the error in Eclipse in a pure Java project. So it does not seems to be langage/IDE based.
So now I am a bit clueless about that, any idea ?
So i found it, the error message was totally misleading as the issue was in the URL which should be:
'jdbc:sqlserver://sqltest3'
If the // are not present, then the connectedProperties are returned to null, and there's no driver provided, hence the resulting error message.
Related
I am trying to connect to SQL Server using my credentials.
The data I am provided is to connect is the following:
Server: Ccddb294\oss_prod
Database: OSS_DW
Code:
public static void main(String arg[]) throws ClassNotFoundException, SQLException {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String dbURL = "jdbc:sqlserver://ccddb294.corp.corpcom.com:1433;databaseName=OSS_DW;integratedSecurity=true";
Connection conn = DriverManager.getConnection(dbURL,"corp\\e21290","Anjali#1234");
if (conn != null) {
System.out.println("Connected");
}
}
I am not sure where to give oss_prod in server name. When I try to connect, I get this error:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Integrated authentication failed. ClientConnectionId:26ddec01-2e7e-46c3-8165-4f3646da5e7c
Can someone validate if the dbURL which I created is correct as per specification or do I need to add odd_prod - but if so, where? (Note: The dll file is correctly placed in bin and i am able to connect to server at least but not able to authenticate only)
After lots of hit and trials.
Following is correct db URL:
"jdbc:sqlserver://ccddb294.corp.corpcom.com:1433;
instanceName=oss_prod;
databaseName=OSS_DW;
integratedSecurity=true;
domain=corp;
authenticationscheme=NTLM;
user=e21290;
password=Anjali#1234";
Iam tryig to connect to a Postgres Database. Iam really new to that and have read a post in the forum. But I didn't manage it.
public void connect() {
//Connection con = null;
try {
Class.forName("org.postgresql.Driver");
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
props.setProperty("ssl","true");
Connection conn = DriverManager.getConnection(url, props);
//String url = "jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true";
//Connection conn = DriverManager.getConnection(url);
System.out.println("Erfolgreich verbunden!");
}
catch (Exception e){
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}
EDIT:
I updated my Code.
The database is deployed to heroku.
It throws the error:
java.sql.SQLException: No suitable driver found for jdbc:postgres://vuqmbekwlgohkw:******
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
at com.company.Database.connect(Database.java:20) p
at com.company.Main.start(Main.java:16)
at com.company.Main.main(Main.java:25)
java.sql.SQLException: No suitable driver found for jdbc:postgres://vuqmbekwlgohkw:***************
I believe that your problem is that your connection URL is malformed.
When DriverManager.getConnection throws SQLException, the message includes the exact url value passed to the function. In your case, that looks like jdbc:postgres://vuqmbekwlgohkw:******.
But obviously that is not the URL you are using. You have replaced part of the URL with asterisks. That suggests that you think the format of the URL is:
jdbc:postgres://username:password#host:port/dbname
which seems to be what Heroku provides in the DATABASE_URL environment variable. You are using asterisks to prevent us from seeing the password.
However, it looks like the PostgreSQL JDBC driver does not accept URLs in this format. When I tried, I also got the "No suitable driver" error. According to the documentation, the format of the URL is:
jdbc:postgres://host:port/database
Some parts are optional, but the driver does not appear to support putting the user name or password in the URL.
I was able to connect to an AWS PostgreSQL instance by using the URL format described in the documentation and using the connection properties to set the user name and password.
You said you are new so I'm gonna start with the painfully obvious, do you have the driver on your classpath?
it's a jar you add to your project that you can download from https://jdbc.postgresql.org/
Otherwise I have an example of the url that worked for me, except I was not exactly using jdbc, it was a spring-boot project:
jdbc:postgresql://ec2-174-99-88-88.compute-1.amazonaws.com:5432/asdfasdfsadf?sslmode=require
There is no port number in your commented URL and not all parameters might be supported, like Willis pointed out.
I am trying to connect to a SQL server using an automation tool (Workfusion Studio), which uses selenium and groovy.I am getting an error "No suitable driver found for jdbc:sqlserver:/XXXXXXXXXXXXXX" when I try to create a connection.
The code I am using is below:
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<selenium-flow>
<selenium name="seleniumDriver" browser="chrome" close-on-completion="true" start-in-private="true">
<script><![CDATA[
import java.sql.*;
this.class.classLoader.addURL(new URL("http://clojars.org/repo/com/microsoft/sqlserver/sqljdbc4/4.0/sqljdbc4-4.0.jar"));
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String dbURL = "jdbc:sqlserver://SERVER_NAME:1433;databaseName =DATABASE_NAME;";
String userName = "USER_NAME";
String password = "PASSWORD";
Connection con = DriverManager.getConnection(dbURL, userName, password);
]]></script>
</selenium>
</selenium-flow>
</config>
Please help resolve the issue.
The JDBC connection is performed through global ClassLoader, so it does not see libs added to local ClassLoader.
You can add the driver jar globally to Control Tower tomcat:
$INSTALL_DIR/apps/webapps/tomcat/lib
For the logic to work in WorkFusion Studio, refer to the Eclipse guides on how to add external jar.
As a workaround (not recommended for production code), the following trick can be performed:
<script><![CDATA[
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException expected) {
groovy.lang.GroovyShell.class.classLoader.addURL(new URL("http://clojars.org/repo/com/microsoft/sqlserver/sqljdbc4/4.0/sqljdbc4-4.0.jar"));
}
]]></script>
More efficient way to execute queries is as following (will properly close the DB connection, etc.):
<database connection="jdbc:sqlserver://hostname:6501;DatabaseName=database"
jdbcclass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
username="user" password="securepassword">
select first_name from actor
</database>
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 5 years ago.
I'm pretty new to JAVA RESTful APIs, and now I'm trying to create one. I watched some tutorials, but the guy wasn't using database, he was only storing datas in an arraylist or sth at runtime.
I tried to make a database connection and then a simple query, and it works fine when I'm running it as a Java Application, but as soon as I try to use it inside my web-application, it cannot connect to my DB and throws an exception.
The exception:
SEVERE: Servlet.service() for servlet [Jersey Web Application] in context
with path [/restapi] threw exception [java.lang.IllegalStateException:
Cannot connect the database!] with root cause
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/restapi?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
Here is my code, which works as a Java Application, but not in a webapp:
public class AuthService {
private String url = "jdbc:mysql://localhost:3306/restapi?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
public List<User> getAllUsers() {
List<User> users = new ArrayList<User>();
try (Connection conn = DriverManager.getConnection(url, "blabla", "blabla")) {
Statement stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM users";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String login = rs.getString("login");
String first = rs.getString("firstname");
String last = rs.getString("lastname");
users.add(new User(id, login, first, last));
}
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
return users;
}
Probably this won't be the correct way to connect to a DB (since I probably don't want to connect to the DB on every query), but it's not that important for me NOW.
(Sorry for my english, thanks for the answers.)
Error message clearly says that it couldn't find the mysql driver in below line of error message:-
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/restapi?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
You need to include the mysql-connector jar in your web-project to make it work. If you are using the Maven, then you need to include below dependency in your code, otherwise download jar manually and put it in your classpath.
<!--Mysql-Connector-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
Also I've created a sample App, which is aims to give the quick-start on how to develop a RESTful java application using java,jersey,mysql,spring and hibernate. please read follow it here https://github.com/amitmbm/rest
This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 7 years ago.
When trying to connect to mysql I always get this error:
java.sql.SQLException: No suitable driver found for localhost test
I already included the mysql-connector.jar in the /WEB-INF/lib in my app. What else do I need to configure to make it work? Do I need to add something in web.xml? I'm not using the appengine.
Here is my code in the server:
package com.mysql.server;
import java.sql.Connection;
import java.sql.DriverManager;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mysql.client.ConnDb;
public class ConnDbImpl extends RemoteServiceServlet implements ConnDb {
public Connection con;
#Override
public String tryConn() {
try{
String host = "localhost";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "pwd";
Class.forName(driver).newInstance();
con = DriverManager.getConnection(host+db, user, pass);
return "Connected to Database";
} catch(Exception ex) {
return ex.toString();
}
}
}
You will get this exception when the JDBC URL is not accepted by any of the loaded JDBC drivers as per the Driver#acceptsURL() method. You actually forgot the JDBC driver specific URI prefix. For the MySQL JDBC driver this is jdbc:mysql://. The full connection URL should look like this:
con = DriverManager.getConnection("jdbc:mysql://localhost/test", user, pass);
See also:
Connector/J documentation - Obtaining a connection
I found another cause for this error message. In my case the user simply had no privilege to the database e.g. to the selected table. Dear driver developers, why do you use such misleading error messages? A lot of people have real trouble with this.
For me, it was forgetting to include the MySQLJDBC Driver in the project libraries. DOH!
This was giving that error:
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/lib_db","root","root");
but when I changed that to:
Connection connection =DriverManager.getConnection("jdbc:mysql://localhost/db_name?"+"user=root&password=root");
error was gone