I'm currently working on an application. We are moving off our file systems and over to our Snowflake database. I can't seem to make a connection to the database - I am continually met with "no suitable driver found" error.
The correct driver was loaded and installed into the build path. Does anyone know whats going on?
The code:
package com.GriefUI.DBComponents;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class Snowflake_Driver {
public static void databaseConnection(HttpServletRequest request, HttpServletResponse response) throws UnsupportedOperationException{
try {
Connection connObject = getConnection();
Statement stmt = connObject.createStatement();
ResultSet rSet = stmt.executeQuery("SELECT * FROM MY_TABLE");
}catch(Exception e) {
e.printStackTrace();
throw new UnsupportedOperationException();
}
}
private static Connection getConnection()
throws SQLException {
try {
Class.forName("net.snowflake.client.jdbc.SnowflakeDriver");
} catch (ClassNotFoundException ex) {
System.err.println("Driver not found");
}
Properties properties = new Properties();
String user = "user";
String pwsd = "password";
String connectStr = "jdbc:snowflake://My_Environment.snowflakecomputing.com";
Connection conn = DriverManager.getConnection(connectStr, user, pwsd);
return conn;
}
}
Build path:
And the related stacktrace:
Driver not found
java.sql.SQLException: No suitable driver found for jdbc:snowflake://My_Environment.snowflakecomputing.com
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.GriefUI.DBComponents.Snowflake_Driver.getConnection(Snowflake_Driver.java:54)
at com.GriefUI.DBComponents.Snowflake_Driver.databaseConnection(Snowflake_Driver.java:20)
at com.GriefUI.Servers.PushServer.doPost(PushServer.java:60)
at com.GriefUI.Servers.PushServer.doGet(PushServer.java:47)
The research I would suggest taking a look at is: https://docs.snowflake.net/manuals/user-guide/jdbc-configure.html
This answer also looked helpful: https://stackoverflow.com/a/54504514/12127815
"adding the jar to classpath, and then running the following command, Class.forName("provided driver name") in the calling class? "
Though a light jar has been asked to be an an enhancement here: "[open]Light driver jar? #174" https://github.com/snowflakedb/snowflake-jdbc/issues/174
Integrating the driver to a project: https://docs.snowflake.net/manuals/user-guide/jdbc-download.html#integrating-the-driver-into-a-project
Specific information copied from the documentation: "To integrate the driver into a project, add the necessary tags to your pom.xml file. For example:
<groupId>net.snowflake</groupId>
<artifactId>snowflake-jdbc</artifactId>
<version>3.9.2</version>
Where the tag specifies the version of the driver you wish to integrate. Note that version 3.9.2 is used in this example for illustration purposes only. The latest available version of the driver may be higher."
Others may have better insights.
I believe the error message may be misleading.
In my case, Snowflake server is behind a firewall/proxy and I get the same error when connection cannot be obtained physically. It all works on the server all right - I get the error when trying to connect to the DB from my local machine that is on the opposite side of the firewall.
So, make sure your application can physically connect to the DB or use a proxy parameter in your java launcher, such as
-Dhttp.proxyHost=a-proxy.host.domain.net -Dhttp.proxyPort=8080 or similar
Related
A few classmates and I are creating a Java project which requires a database. I have created a connection in MySQL and connected it to my Java project successfully using the following Connect class:
package com.example.javaworkoutgame.Model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect {
static Connection con;
public Connect() {
connect();
}
// attempt to connect to MySQL database
public static void connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded Successfully");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lab3", "root",
"**********"); // not the actual password
System.out.println("Successful Connection");
} catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
}
This code runs properly on my machine.
I committed and pushed the code to Bitbucket so my partners could access it. However, when they run the code on their computers, they get the following error message:
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
Is there something I need to change in MySQL workbench in order for other people to be able to access the database? I could not find any information on this.
The only thing I was able to try was found at this thread:
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
I opened a new .sql file and tried running the command:
GRANT ALL PRIVILEGES ON . TO 'root'#'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;
(I replaced '%password%' with the actual password)
When I tried that I got the following error message:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY '*********' WITH GRANT OPTION'
No, and you need to stop this line of thought and do some research first.
Your current configuration says that the mysql server is on the very same physical machine that the code is running on. You installed mysql on your dev machine, your friends need to install it on theirs, and each has their own unique database (nothing is shared).
You could, instead, take your mysql server, open it up to the world (which, for virtually all ways internet is made available in residential connections, requires messing with your router to 'open a port').
But then you have an open mysql server, and the username and password are on a public bitbucket site.
It also requires either a permanent IP (which few residential internet providers offer) or a dyndns service. More generally, hosting open MySQL servers that see lots of traffic gets your internet shut down, for good reason. You'd end up hosting a whole bunch of hackers. All hardware in your network will be p0wned and turned into bot nets. Hence, very very bad idea.
Good ways to solve this problem:
Everybody installs their own MySQL server. This is sensible; you're writing code and bound to make mistakes, it'd be real bad if all code you write is first-run and tested on live data. You don't want one of your friends to wipe your database. If you need some initial data to test with, set it up properly, and read up on how to make an SQL dump. With such a dump file you can reset any mysql server to that exact state - and that'd be how you and your friends develop: Set up the DB to be in that known state, write some code, and if you ruin the db by doing so, no problem. Just reset it again.
Set up a VPN between your friends. NOW you can share the IP your system has within the VPN (it'll be 10., 172.16., 192.168.* - if it's 127.0.0.1, it's localhost, i.e. everybody needs to install mysql on their own and nothing is shared, and if it's anything else, you're opening it to the world, which you don't want to do). Do not put the VPN username/password info anywhere in that bitbucket. And you need to trust your friends.
You should have a properties type file so that each person who is going to interact with the code has their local data without the need to replicate yours, in the same way you can have different values in the properties for test or production environments.
example of a property file:
system.properties
#BD
db.driver=com.mysql.cj.jdbc.Driver
db.user=user
db.pass=password
db.server=server_IP
db.port= port_IP
db.db = DB
Then you should have a procedure to read from java the properties inside the file
Utils.java
package com.example.javaworkoutgame.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public final class Utils {
public static Properties getProperties() {
String path = String.format("PATH to your properties FILE/system.properties",
System.getProperty("user.dir"));
Properties properties = new Properties();
try (InputStream is = new FileInputStream(new File(path))) {
properties.load(is);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return properties;
}
}
And finally you make a call to the function that gets the properties from your connection class
Connect.java
package com.example.javaworkoutgame.Model;
import com.example.javaworkoutgame.util.Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect {
Properties properties = Utils.getProperties();
static Connection con;
public Connect() {
connect();
}
// attempt to connect to MySQL database
public static void connect() {
try {
String driver = properties.getProperty("db.driver");
String ip = properties.getProperty("db.ip");
String port = properties.getProperty("db.port");
String db = properties.getProperty("db.db");
String user = properties.getProperty("db.user");
String pass = properties.getProperty("db.pass"):
Class.forName(driver);
System.out.println("Driver Loaded Successfully");
con = DriverManager.getConnection("jdbc:mysql://"+ip+":"+port+"/"+db, user,
pass);
System.out.println("Successful Connection");
} catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
}
About the MYSQL error, if your partners do not have a local mysql environment with the same values as you, they will experience the error you describe, since your configuration is a local configuration, if you need your partners to connect to your pc, you must open the ports of mysql and give them your public IP (not recommended)
I hope this answer helps you!
I just set up a local mySql database. I can connect to it properly through command line and third party software (Navicat..).
Server: localhost:3306
User: root
Password: password
Database name: students
However, when I try to connect with java, I get an error.
CODE:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sql {
public static void main(String[] args) {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306//students?autoReconnect=true&useSSL=false",
"root", "password");
Statement sqlState = conn.createStatement();
} catch (SQLException ex) {
System.out.println("SQLException" + ex.getMessage());
System.out.println("SQLException" + ex.getSQLState());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
OUTPUT:
SQLExceptionCould not create connection to database server. Attempted reconnect 3 times. Giving up.
SQLException08001
Also tried:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306//students", "root", "password");
and
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306//students?autoReconnect=true&useSSL=false","root", "password");
Additional Attempts:
The firewall is disabled.
I installed the Jconnector by putting the .bin.jar in:
C:\Program Files\Java\jdk1.8.0_73\jre\lib\ext
I can see it on the left under JRE in Eclipse and the import doesn't give errors.
NOTEs:
Using mySQL from the terminal, workbench or Navicat gives no errors, any query works, the local server is running properly and the credentials are correct, the user has full admin priviledges.
Any suggestions?
I recently had a similar issue. My issue ended up being that the MySQL server version was 8.0.19, while flyway was using a 5.1.40 version of the java MySQL library.
Once I updated the MySQL library version to 8.0.19, my errors went away.
This exception can have 4 causes:
User does not have privileges to connect to database.
MySQL service is not running.
Incorrect MySQL-Connector Jar.
Database URL is incorrect;.
Check above prerequisites before connecting to MySQL database.
I am trying to connect to remote DB via simple JDBC but i am getting below
error:
*** SQLException caught ***
ORA-28040: No matching authentication protocol
code :
import java.sql.*;
import oracle.jdbc.*;
import java.io.*;
import oracle.jdbc.pool.OracleDataSource;
public class JDBC {
public static void main (String args []) throws SQLException {
try
{
//Connecting to Oracle server
OracleDataSource ds = new oracle.jdbc.pool.OracleDataSource();
ds.setURL("jdbc:oracle:thin:#remotehostname:1521:dbname");
Connection conn = ds.getConnection("my username", "my password");
System.out.println("Connected to DB");
}
catch (SQLException ex) { System.out.println ("\n*** SQLException
caught ***\n" + ex.getMessage());}
catch (Exception e) {System.out.println ("\n*** other Exception caught ***\n");}
}
}
I have added ojdbc6.jar and all supporting jars to build path. I am able to connect to remote via VPN but not via JDBC .. any guidance much appreciated...
From docs.oracle.com
If the client version does not meet or exceed the value defined by SQLNET.ALLOWED_LOGON_VERSION_SERVER parameter(sqlnet.ora file), then authentication fails with an ORA-28040: No matching authentication protocol error or an ORA-03134: Connections to this server version are no longer supported error.
You can check your driver version using below command
$java -jar ojdbc6.jar -getversion
Try setting SQLNET.ALLOWED_LOGON_VERSION_SERVER in sqlnet.ora file to lower Oracle version like 8,9 or 10 .
Check this issue for more info.
I'm trying to connect to a mysql database using Connector/J but get the same message no matter what code I use. I can connect to it manually with the mysql command on debian just fine.
The error is You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '????????????????' at line 1
The latest code is:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class E{
public void connect(){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(
"jdbc:mysql://192.168.1.3:3306/dbname?user=dbuser&password=dbpw");
}catch( Exception e){
System.out.printf("Something went wrong: "+e.getMessage());
}
}
It fails at the getConnection call. I used print statements to figure this out as NetBeans doesn't have a problem with this (but when I put in a bad host and run it from NetBeans it doesn't complain either).
I get the exact same problem with DataSource
import java.sql.*;
import javax.sql.*;
...
com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds
= new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
ds.setServerName("192.168.1.3");
ds.setPortNumber(3306);
ds.setDatabaseName("dbname");
ds.setUser("dbuser");
ds.setPassword("dbpw");
conn = ds.getConnection();
...
Something is working because if I put in a bad password I get an access denied message.
I'm using mysql-connector-java-5.1.22-bin.jar and had the same problem with 5.1.21.
These might help you:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL
Problem with connecting to MySQL server
Your getConnection statement should be
conn = DriverManager.getConnection("jdbc:mysql://192.168.1.3:3306/dbname", "dbuser", "dbpw");
Refer this example
I found the following: Stumped SQL Exception for JDBC
In a nutshell, the server my.cnf file needs:
character_set_server=utf8
collation_server=utf8_general_ci
It also said the GCJ (gnu java compiler) is dicey, but I didn't replace it
My configuration:
windows XP SP3
JDBC 2005
MS SQL Server 2008 Express, exposed via tcp/ip on port 1433
sqljdbc.jar in class path
I tried:
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433/SQLEXPRESS2008;databaseName=Test;selectMethod=cursor", "sa", "");
}
catch (Exception e) {
e.printStackTrace();
}
But it always throws an exception:
java.sql.SQLException: No suitable driver
I also tried the following urls:
localhost:1433/SQLEXPRESS2008
localhost/SQLEXPRESS2008
localhost
Same results.
Any help?
You have the wrong URL.
I don't know what you mean by "JDBC 2005". When I looked on the microsoft site, I found something called the Microsoft SQL Server JDBC Driver 2.0. You're going to want that one - it includes lots of fixes and some perf improvements. [edit: you're probably going to want the latest driver. As of March 2012, the latest JDBC driver from Microsoft is JDBC 4.0]
Check the release notes. For this driver, you want:
URL: jdbc:sqlserver://server:port;DatabaseName=dbname
Class name: com.microsoft.sqlserver.jdbc.SQLServerDriver
It seems you have the class name correct, but the URL wrong.
Microsoft changed the class name and the URL after its initial release of a JDBC driver. The URL you are using goes with the original JDBC driver from Microsoft, the one MS calls the "SQL Server 2000 version". But that driver uses a different classname.
For all subsequent drivers, the URL changed to the form I have here.
This is in the release notes for the JDBC driver.
Download the latest JDBC Driver (i.e. sqljdbc4.0) from Microsoft's web site
Write the program as follows:
import java.sql.*;
class testmssql
{
public static void main(String args[]) throws Exception
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;
databaseName=chapter16","sa","123");//repalce your databse name and user name
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from login");//replace your table name
while(rs.next())
{
String s1=rs.getString(1);
String s2=rs.getString(2);
System.out.println("UserID:"+s1+"Password:"+s2);
}
con.close();
}
}
Compile the program and set the jar classpath viz: set classpath=C:\jdbc\sqljdbc4.jar;.; If you have saved your jar file in C:\jdbc after downloading and extracting.
Run the program and make sure your TCP/IP service is enabled. If not enabled, then follow these steps:
Go to Start -> All Programs -> Microsoft SQL Server 2008 -> Configuration tools -> SQL Server Configuration Manager
Expand Sql Server Network Configuration: choose your MS SQL Server Instance viz. MSQSLSERVER and enable TCP/IP.
Restart your MS SQL Server Instance. This can be done also from the right click menu of Microsoft SQL Server Management Studio at the root level of your MS SQL server instance
If your databaseName value is correct, then use this: DriverManger.getconnection("jdbc:sqlserver://ServerIp:1433;user=myuser;password=mypassword;databaseName=databaseName;")
The latest JDBC MSSQL connectivity driver can be found on
JDBC 4.0
The class file should be in the classpath. If you are using eclipse you can easily do the same by doing the following -->
Right Click Project Name --> Properties --> Java Build Path -->
Libraries --> Add External Jars
Also as already been pointed out by #Cheeso the correct way to access is jdbc:sqlserver://server:port;DatabaseName=dbname
Meanwhile please find a sample class for accessing MSSQL DB (2008 in my case).
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectMSSQLServer
{
public void dbConnect(String db_connect_string,
String db_userid,
String db_password)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(db_connect_string,
db_userid, db_password);
System.out.println("connected");
Statement statement = conn.createStatement();
String queryString = "select * from SampleTable";
ResultSet rs = statement.executeQuery(queryString);
while (rs.next()) {
System.out.println(rs.getString(1));
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
ConnectMSSQLServer connServer = new ConnectMSSQLServer();
connServer.dbConnect("jdbc:sqlserver://xx.xx.xx.xxxx:1433;databaseName=MyDBName", "DB_USER","DB_PASSWORD");
}
}
Hope this helps.
Named instances?
URL: jdbc:sqlserver://[serverName][\instanceName][:portNumber][;property=value]
Note: backward slash
You can try the following. Works fine in my case:
Download the current jTDS JDBC Driver
Put jtds-x.x.x.jar in your classpath.
Copy ntlmauth.dll to windows/system32. Choose the dll based on your hardware x86,x64...
The connection url is: 'jdbc:jtds:sqlserver://localhost:1433/YourDB' , you don't have to provide username and password.
Hope that helps.