I'm having some troubles with java and jdbc.
In particular, while my code perfectly works in a NetBeans project, when i try to execute it on a terminal or on my ubuntu vps (which is where i need it to work) i always get this exception:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/quakes
First thing first: yes, i'm adding the jdbc .jar to the execute command and the compile command; Yes, i've even tried to add
Class.forName("com.mysql.jdbc.Driver");,
but I always get a ClassNotFoundException: com.mysql.jdbc.Driver exception
The .jar i'm using is the exact same that i use in the NetBeans project, so i know i have the right thing, and even downloading it again from the official site won't change a thing.
Yes, the database exists, and the result doesn't change if i try to connect to another db.
I also tried switching to postgresql (yes, i didn't forget to change the url), but to no avail, it still can't find the driver.
With this, i'm guessing that the actual error is in the compile/execute commands, but even them should be ok:
javac *.java <-cp mysql-connector-java-5.1.41-bin.jar > (the <> parenthesis means that i tried compiling with and without specifying the classpath);
java TAW -cp mysql-connector-java-5.1.41-bin.jar,
In case you want to see it, here's the method that tries to connect to the database:
public Connection getConnection() throws SQLException {
if (conn == null) {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+
this.dbname,this.user,this.pass);
}
return conn;
}
Does anyone have any idea on why this happens?
You need to put the classpath option for java before the name of your main class, otherwise it is regarded as program arguments:
java -cp mysql-connector-java-5.1.41-bin.jar;. TAW
You can use this method step by step to create connetion.
it is an example connectivity:
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost/feedback?"
+ "user=sqluser&password=sqluserpw");
Related
I recently switched over from using a PC to a Mac and now for whatever reason one of my Impala drivers that worked fine is no longer found when run in Python. I keep receiving this error every time I run the script : "java.lang.RuntimeException: Class com.cloudera.impala.jdbc41.Driver not found". Please see code snippet for my connection below.
c = jaydebeapi.connect
(jclassname='com.cloudera.impala.jdbc41.Driver',
url='jdbc:impala://cloudera-impala-proxy.live.bi.xxx/;AuthMech=3;ssl=1;',
driver_args=['xxx', self.dwh_password], jars='/Users/xxx/Desktop/ImpalaJDBC41 2.jar')
Any help or suggestions are appreciated, I feel like I'm going crazy trying to get this to work.
Did you check do you have the ImpalaJDBC***.jar in your new machine.
Please check properly weather it's available at classpath/build path or not.
Edit:
You can use hive jdbc jar as well to connect with impala , just use the port of impala rather hive in jdbc url.
Looking at this error means your jar is corrupt.
First check your impalaJDBC jar
java -jar ImpalaJDBC<version>.jar
If it gives you error that means your jar is corrupt.
Download the correct jar from cloudera
I simply want to use SQL Server database in my HTTP Servlet program but my program can't seem to connect to the database. It gives me the following error:
No suitable driver found for
jdbc:sqlserver://localhost:1433;databaseName=Bookyard;integratedSecurity=true;
This is my connection method.
package practice.bookyard.server.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Database {
public static Connection getConnection() {
String url = "jdbc:sqlserver://(LocalDb)\\MSSQLLocalDB:1433;databaseName=Bookyard;integratedSecurity=true;";
Connection connection = null;
try {
connection = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
I had the sever name as localhost:1433 earlier but I changed it to the SQL Server instance name (LocalDb)\\MSSQLLocalDB:1433 but it still seems to pick up the old name.
Also, I am not sure how to provide the right connection string when connecting to SQL Server localdb.
I am using Eclipse for Java EE, Mars 2, and I downloaded Microsoft JDBC drivers for SQL 6.0 from this website.
I ran the installation, unzipped the contents of the resulting folder. Then, I added the sqljdbc42.jar file to the build path as I am targeting JDK 1.8.
UPDATE
Upon Scary Wombat's suggestion, I have also added the path to the sqljdbc42.jar file to my classpath.
However, I still get the same error.
I am pretty confident this is a reflection issue, in that the type loader isn't able to resolve the driver type from my connection string. Which means, the connection string syntax I am using is wrong.
I changed my connection string to read as follows:
String url = "jdbc:sqlserver://localhost:1433;
instance=(LocalDb)\\MSSQLLocalDB;
databaseName=Bookyard;integratedSecurity=true;";
However, I still not only get the same error but the exception message I receive still has my old connection string. So, clearly, there's also some caching going on, I just don't know where. Who is caching my connection string and how do I refresh / clear that cache?
Could you please tell me how to provide a SQL Server instance name if I am connecting to localdb and not on the main SQL Server instance?
As #ScaryWombat and #JozefChocholacek had hinted, it turned out to be a class path issue. Apparently, you have to copy and paste just the sqljdbc42.jar file, and this file only directly into the WEB-INF\lib folder and not within any sub-folder.
I did that it still gave me that error.
That was because the WEB-INF folder structure, when I viewed it in the Project Explorer, still had the old folder structure. So, I right-clicked on the WEB-INF folder in the Project Explorer and selected the Refresh command.
I also updated my environmental variable CLASSPATH to point it to the WEB-INF folder and that error went away.
you can do like below
String url = "jdbc:sqlserver://YOUR PC NAME;instanceName=SQLEXPRESS;DatabaseName=dbname;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myCon = DriverManager.getConnection(url);
note :however its not necceaary for new version of jdbc driver to load driver class manually using class.forname
This is my first post so if I get anything wrong I apologise!
I am using Java in Eclipse to create tables in a MySQL Database. Everything has been working fine until it inexplicably stopped working the other day. My code runs but nothing happens to the database due to an error when I try to initialise the connection. The code actually runs to the end, the error is only viewable when I step through the code.
This is the constructor of my TableCreator class:
public TableCreator() throws SQLException {
host = "jdbc:mysql://localhost:3306/test";
dbName = "root";
dbPass = "pass";
conn = DriverManager.getConnection(host, dbName, dbPass);
query = conn.createStatement();
initialise();
System.out.println("Success.");
}
The error occurs on the line 'conn = DriverManager.getConnection(host, dbName, dbPass);'.
The stack looks like this just after I have attempted to execute this line:
TableCreator (1) [Java Application]
biz.cogitare.gpsperformancetool.TableCreator at localhost:53389
Thread [main] (Suspended)
Driver(NonRegisteringDriver).connect(String, Properties) line: 306
DriverManager.getConnection(String, Properties, Class<?>) line: not available
DriverManager.getConnection(String, String, String) line: not available
TableCreator.<init>() line: 23
TableCreator.main(String[]) line: 49
Daemon Thread [Abandoned connection cleanup thread] (Running)
C:\Program Files\Java\jre1.8.0_45\bin\javaw.exe (9 Jul 2015 12:27:27)
I have spent many hours searching the internet for help on how to fix this but so far I have been unsuccessful.
Any help would be greatly appreciated.
Thanks!
Several things can cause such behaviour.
Check the driver is loaded.
If your JDBC driver is earlier than 4.0 you need to ensure the driver is loaded with
Class.forName("com.mysql.jdbc.Driver")
Or a similar string depending on the particular driver you are using.
Maybe you were doing another operation which loaded the driver before executing this code and that is why it did work. And maybe you are not doing that operation and that is why this code fails despite being the same code.
Check the driver file is in the classpath.
If you get ClassNotFoundException it means the JVM has not been able to locate your class. If you get LinkageError it means there is a version conflict. Get the most recent driver and reinstall it.
Check the driver is properly imported in eclipse.
Follow this instructions to import it : How to import a jar in Eclipse
If everything fails, reinstall.
Uninstall java, remove eclipse, re-install everything.
Insert the driver in the correct fashion and started a new project and copy your classes over.
If you are exporting the project as runnable jar,please make sure that you export mysql driver.jar
and as all above answers if you need to add
Class.forName("com.mysql.jdbc.Driver"); before conn = DriverManager.getConnection(host, dbName, dbPass);
If it not working, please share with us the stacktrace
I'm really looking at a mystery here. I created a Java program in Eclipse and established a JDBC connection. The code is the following:
import java.sql.*;
public class Login {
public static void main(String[] args) {
try {
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager
.getConnection("jdbc:sqlserver://localhost;databaseName=testdb; integratedSecurity=true;");
//adding port 1433 doesn't make a difference
System.out.println("Connection successful");
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select * from testtable");
while (rs.next()) {
line = rs.getString(2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
When I'm running it within eclipse, everything's fine. The database and tables are good, library and authentication in the Java Built Path are set and I get the result I want, namely "Hello World", which is a string in the selected table.
But creating an executable JAR file and running it throws the exception "This driver is not configured for integrated authentication". It does not even connect, so even if there was an error in the database it wouldn't matter at this point.
Even more confusing is the fact, that some weeks earlier, I also created an executable JAR file and it worked!
OS: Windows Vista Home Premium x86
JDBC driver location: "C:\sqljdbc_4.0\enu\sqljdbc4.jar"
Native library location: "C:\sqljdbc_4.0\enu\auth\x86"
Java version: Java 7 Update 17 and JDK 7 Update 17
Previousely, I used Update 21, but I changed back to check if the driver could be the reason.
So, any suggestions? I'd be very grateful!
Check the runtime classpath of eclipse, then try to create a system level "CLASSPATH" variable and add the same path. Once the variable is set, execute it in new command prompt or reload the environment variables
Make sure your executable jar has the dependency in MANIFEST.MF for all the necessary jars (along with MainClass)
Finally, It is always a practice in Java to make the first alphabet in Capital ("Login" instead of "login")
Actual problem is that if you run your project from eclipse, it will automatically add run arguments that are defined in run/run config//arguments, so you run your program with properly set path to sqljdbc_auth.dll. But if you export your application to .jar file and just run it, you will get these errors cause there is no correct classpath to auth file.
So, if you run your app from command line with proper arguments, it should be working.
Firstly I would like to say that I'm new to Java programming and Sybase.
I have a problem with connection to Sybase database.
To establish the connection I have done:
Installation of jdbc driver for advantage db;
Set environment variable named CLASSPATH with url value to adsjdbc.jar (I installed jdbc drivers in c:\AdvJDBC so the path is c:\AdvJDBC\JDBC\adsjdbc.jar );
Then I'm tried to import drivers in my code:
public class SybaseCon {
public static void main(String[] args){
Class.forName("com.extendedsystems.jdbc.advantage.ADSDriver");
}
And I'm getting an error:
Exception in thread "main" java.lang.ClassNotFoundException: com.extendedsystems.jdbc.advantage.ADSDriver
I have searched for the solution in advantage jdbc driver help, but I found nothing that could resolve my problem.
Workaround:
My Java version: jdk1.7.0_03
Advantage file db: 11.0
Advantage jdbc driver: 11.0
First, make sure that your CLASSPATH variable is actually set in the environment that you think it is.
Typically, in Windows, if you set the environment variable in the system settings, open command windows (and your open IDE) won't see the setting. You have to close and re-open them for it to take effect.
Try adding this to your code, just before the Class.forName line:
System.out.println(System.getProperty("java.class.path"));
It will show you the classpath that your application is actually using.
Second, you'll probably have a much easier time if you specify the classpath using java's preferred path notation, which happens to align with unix/linux format - using forward slashes. Try using
java -cp /AdvJDBC/JDBC/adsjdbc.jar