I am getting java.lang.ClassNotFoundException on loading sun.jdbc.odbc.JdbcOdbcDriver using Class.forName().
I am using MySQL as Data Source and I have added Data Source Name in ODBC Data Source Administrator (on Windows 8).
Here is the code:
class Connect {
check() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Output:
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
Are you using Java 8? The class is no longer present there (more info). You could install Java 7 if you need to use it.
This happened to me once, and what i did was importing the mysql jdbc library that came with the product when i downloaded it, after that i used the driver as it is explained in the page:
http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html
hope this could help you
Related
i need help with the ODBC - JDBC Interface for CSV, with 32Bit Java 1.7.0_72
i recieved this code
private void connectToCSV(String file) {
try {
debug.println(path + file +" "+this.file);
if (this.file == null){
this.file = file;
gfdiConnection = DriverManager.getConnection("jdbc:odbc:Driver={"+ driverCSV + "};DBQ=" + path );
}
if (this.file != file){
this.file = file;
gfdiConnection = DriverManager.getConnection("jdbc:odbc:DRIVER={" + driverCSV + "};DBQ=" + path);
}
} catch (SQLException e) {
debug.println("Error while trying to connect to CSV");
e.printStackTrace();
}
}
but I didn't get run it.
java.sql.SQLException: No suitable driver found for jdbc:odbc:Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=D:\Input\
I found a suggestion with
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
gfdiConnection = DriverManager.getConnection("jdbc:odbc:Driver={"+ driverCSV + "};DBQ=" + path );
but this didn't work here.
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
I checked also the Driver Name with
C:\Windows\SysWOW64\odbcad32.exe
so whats the Problem with the Driver ?
System.getProperty("java.version") reported "1.8.0_66" so the application actually was running under Java 8, which has dropped the JDBC-ODBC Bridge.
ODBC requires an activeX control to be installed that deals with providing a connection, meaning a program that directly interfaces with ODBC only needs to be sure that those drivers are installed on the system. JDBC on the contrary requires that a library (jar file usually called a connector) is in the classpath that provides that connection.
What you're doing is trying to establish an ODBC connection through a JDBC connection, meaning presumably you need both the activeX control to be installed as well as the JDBC connector in classpath. The name of the connector is correct, but it also must be in classpath.
What you need to do is to find the jar file providing the jdbc-odbc bridge and make sure it is in your classpath when your program starts. You may have other problems, but at least if you don't see a ClassNotFoundException, you've fixed your missing jdbc driver issue.
You should eventually consider an alternative solution to using ODBC in Java.
Check if you have in java build path the respective JAR to prevent error in your project
I added rt.jar manually to build path, now there is no more error with "sun.jdbc.odbc.JdbcOdbcDriver", but i got a NullpointerException
Exception in thread "Thread-1" java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at database.GfDIConnect.connectToCSV(CSVConnect.java:85)
at database.GfDIConnect.queryCSV(CSVConnect.java:147)
at database.GfDIConnect.gfdiBTRSTToPostgreSQL(CSVConnect.java:297)
at view.MainRefactor$58.run(MainRefactor.java:2434)
at java.lang.Thread.run(Unknown Source)
CSVConnect.java:85 is
gfdiConnection = DriverManager.getConnection("jdbc:odbc:Driver={"+ driverCSV + "};DBQ=" + path );
so was this solution to much quick and dirty ?
EDIT
I found the reason, there was still the build path libraries - JRE System Library on 1.8 JRE, after change to 1.7 it is working
This question already has answers here:
Removal of JDBC ODBC bridge in java 8
(5 answers)
Closed 7 years ago.
I am trying to connect Java app to MSAccess in NetBeans IDE (pls don't tell me not to use Access, because we are using it in classes, and that's just it for now :)). I didn't have this problem on Windows 7, and I couldn't find answer using Google, so I decided to post this question.
So, it's like this, I have:
Windows 8.1 (64-bit)
Java jdk1.8.0 (32-bit)
NetBeans IDE 8.0, and NetBeans jdk home (from netbeans.conf) is: "C:\Program Files (x86)\Java\jdk1.8.0", so it's using 32-bit jdk.
Code for loading driver:
public void loadDriver() throws RuntimeException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
throw new RuntimeException("Could not load driver!");
}
}
Code for opening connection:
public void openConnection() throws RuntimeException {
try {
connection = DriverManager.getConnection("jdbc:odbc:db");
connection.setAutoCommit(false);
} catch (Exception e) {
throw new RuntimeException("Could not connect!");
}
}
Of course, there's an attribute:
private Connection connection; (and import java.sql.Connection;)
There is a problem at loading driver - it always says "could not load driver". If I have to post more code or change something in what I've posted, please tell me and I will.
I went to: SysWOW64 - odbcad32.exe - Add... - Microsoft Access Driver (*.mdb, *.accdb), and then for Data source name I've put of course "db" (like in my code above) and selected the database (.accdb file) that I will use. And I don't know if it is the Windows 8 issue or I'm forgetting something, but I really have no clue how to make it work.
The JDBC-ODBC Bridge has been removed from Java 8. For an alternative, see the related question here:
Manipulating an Access database from Java without ODBC
I'm writing a java program which needs to load a local SQLite database. Everything works as expected until I package it and try it on another computer. The database queries seem to be the only problem. Attempts return "No suitable driver found for jdbc:sqlite:C:\Data\Test.db". The error makes me think it's accessing the SQL classes but is not properly loading the drivers themselves.
Windows 7
Coded in IntelliJ 12
Zentus' SQLite JDBC 3.7.2
Java SE 7 Update 25
String db_string = "C:\Data\Test.db";
try{
connection = DriverManager.getConnection("jdbc:sqlite:" + db_string);
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // 30 seconds
ResultSet rs = statement.executeQuery("select * from " + table);
while(rs.next()){
// Read the results
fileList.add(rs.getLong("modifieddate"));
}
} catch (SQLException e){
System.err.println(e.getMessage());
}
// Close the connection
finally {
try{
if(connection != null)
connection.close();
} catch (SQLException e){
System.err.println(e);
}
}
In IntelliJ I've built the Jar by creating a new Artifact and specifying the main class. The SQLite package I've used to read/write database information is extracted into the Jar by IntelliJ. When I open the Jar I can see my code and the SQLite code along with its JDBC drivers.
Manifest-Version: 1.0
Main-Class: com.myproject
The other computers I've tried it on are running Windows 8 and Windows 7. Both of them give the same errors.
What could possibly be wrong?
You're not loading the driver class's static initializer by calling Class.forName(...) and since you repacked the JAR, the SPI manifest needed to automatically detect the driver class probably isn't included.
Either manually invoke the driver or add the original SQLite driver JAR to your classpath instead of bundling it into a single JAR.
I am trying to connect to a mysql database by using this simple code.
import java.sql.*;
public class OdbcAccessConnection_1 {
public static void main(String [] args) {
Connection con = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
// Connect with a url string
con = DriverManager.getConnection("jdbc:mysql://localhost/books","root","1234");
System.out.println("Connection ok.");
con.close();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
e.printStackTrace();
}
}
}
All it does is tell me if the connection is working. There is no problem with my database and this code/connection work on netbeans. The StackTrace i am getting is -
the java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/books
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at OdbcAccessConnection_1.main(OdbcAccessConnection_1.java:13)
I am working on 64 bit windows 7 and using 5.1 versions of the Connector/ODBC driver 64 bit. On the ODBC all seems to connect and the test was successful. But when i run the code i get the stack trace above. I am missing something very simple so any input and help would be very much appreciated.
Thank you:)
Go to Run menu in netbeans or whatever IDE you are using
=>Set Project Configuration then Customize.Then choose the Libraries on left dropdown menu
Add your appropriate driver file either jar or folder.
Click OK.
jdbc:mysql://localhost/books is a URL that you use to connect to MySQL directly, using the MySQL JDBC driver. The URL used by the JDBC/ODBC driver is different (see http://docs.oracle.com/javase/1.3/docs/guide/jdbc/getstart/bridge.doc.html).
The usage of this JDBC/ODBC bridge is discouraged, and should only be used to access a database that doesn't provide any JDBC driver. This is not the case of MySQL. Use Connector/J, their JDBC driver. Once you have this driver in your classpath, you can use the URL you're currently using, and remove the JDBC/ODBC driver from your classpath (and its loading from your code).
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books","root","1234");
This error creeped on me because I forgot to add the Class.forName line. The mysql driver jar was on the classpath but no one implicitly loads the driver class, so the session factory can't find any driver classes loaded. Thus the purpose of this line.
In your case, you're loading the wrong thing. It should be Class.forName("com.mysql.jdbc.Driver") if you intend to use it with a jdbc:mysql:// connection.
best solution bhai logo -:
go to JCreator configure menu then click to options then JDK profiles then double click to whatever the version u r using automatically mention there then click to add archive then go to that path -> C:\Program Files\MySQL\MySQL Tools for 5.0\java\lib\mysql-connector-java-5.0.4-bin.jar press ok.
I connect to a database:
void connectToDataBase(){
dataManager_ref = new DataBaseConfigurationManager();
try
{
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dataBase","root","");
System.out.println("Connection successful");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
I implemented a JAR File for the driver:
mysql-connector-java-5.1.17-bin.jar
and imported it into the servlet
import java.sql.DriverManager;
this isnt the first time I use this database (tho the first time with Java EE web). This time I get the following exception:
No suitable driver found for jdbc:mysql://localhost:3306/dataBase
The application is running on a glassfish server 3.1, can I even use a database on a mysql server here? Can somebody help please
thanks in advance,
Daniel
You sometimes need to load the Driver class explicitly in order for the DriverManager to be aware of it.
Try this
Class.forName("com.mysql.jdbc.Driver");
Before you call the DriverManager
You can add a CLASSPATH variable in Environmental System variables, and set the path to your connector, path including name of connector.jar.
Also mysql-connector-java-5.1.17-bin.jar is showing some incompatibilities in accessing. it gave me lots of errors, so i had to go bac to 5.0.x versions