This question already has answers here:
connect to sql server using jdbc
(2 answers)
Closed 9 years ago.
Here I'm trying to connect oracle which is installed in local machine with DSN bam.
I'm getting java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver Exception. Anybody please help me to fix this issue.
public class JdbcConnectionExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager
.getConnection("jdbc:oracle:thin:#localhost:1521:bam"
,"system","tiger");
Statement stmt = con.createStatement();
System.out.println("Created DB Connection....");
ResultSet rs = stmt.executeQuery("select * from tt");
while(rs.next()){
System.out.println(rs.getString("ename"));
System.out.println(rs.getInt("age"));
}
rs.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
The Log is here
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.bam.sqlex.JdbcConnectionExample.main(JdbcConnectionExample.java:14)
May be u have not added jar of oracle driver download jar from here
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
Undoubtedly, you are lack of Oralce jar, improt it into your project
Class.forName("oracle.jdbc.driver.OracleDriver");
You have to check if this class is added to the classpath.
Related
First of all, sorry for my bad English.
I have scheme and table in base and I would like to make connect to it.
1) Name of scheme - user.
2) Name of user - root
3) Password - banana.
I wrote this code by using MySQL's tutorial
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JavaNioServer {
public static void main (String [] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/user?"
+ "user=root&password=banana");
}catch (Exception ex) {
ex.printStackTrace();
return;
}
}}
But code throws Exceptions
> java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Unknown Source)
at JavaNioServer.JavaNioServer.main(JavaNioServer.java:9)
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/user?user=root&password=88224148
at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
at JavaNioServer.JavaNioServer.main(JavaNioServer.java:24)
I used standard java.sql libs and I can't understand what I do wrong...
ClassNotFoundException throws because you haven't added the relevant MySQL connector. Add the MySQL 8 Connector for Java.
MySQL Connector/J » 8.0.11 - jar
Download This Jar add in buildpath/classpath .
What I am trying to do:
I want to connect my MS Access Database with Java 8. So I am using "UcanAccess" driver to connect to my Database.
What I am using:
Eclipse, Java 8, and MS Access database
My MS Access database path:
C:/Users/dave/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/Database11.accdb"
I have the following Jar files in my project:
mysql-connector-java-5.1.35-bin.jar
ucanaccess-2.095.jar
jackcess-2.1.2.jar
hsqldb.jar
commons-lang3-3.4.jar
commons-logging-1.2.jar
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/builder/CompareToBuilder
at com.healthmarketscience.jackcess.impl.RowIdImpl.compareTo(RowIdImpl.java:106)
at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:2039)
at com.healthmarketscience.jackcess.impl.IndexData$Entry.compareTo(IndexData.java:1847)
at java.util.Collections.indexedBinarySearch(Unknown Source)
at java.util.Collections.binarySearch(Unknown Source)
at com.healthmarketscience.jackcess.impl.IndexData$DataPage.findEntry(IndexData.java:2570)
at com.healthmarketscience.jackcess.impl.IndexData.findEntryPosition(IndexData.java:844)
at com.healthmarketscience.jackcess.impl.IndexData.access$3700(IndexData.java:47)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.updatePosition(IndexData.java:2335)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2273)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.restorePosition(IndexData.java:2256)
at com.healthmarketscience.jackcess.impl.IndexData$EntryCursor.beforeEntry(IndexData.java:2218)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findPotentialRow(IndexCursorImpl.java:376)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntryImpl(IndexCursorImpl.java:282)
at com.healthmarketscience.jackcess.impl.IndexCursorImpl.findFirstRowByEntry(IndexCursorImpl.java:153)
at com.healthmarketscience.jackcess.impl.DatabaseImpl$DefaultTableFinder.findRow(DatabaseImpl.java:2074)
at com.healthmarketscience.jackcess.impl.DatabaseImpl$TableFinder.findObjectId(DatabaseImpl.java:1953)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.readSystemCatalog(DatabaseImpl.java:858)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.<init>(DatabaseImpl.java:518)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.open(DatabaseImpl.java:389)
at com.healthmarketscience.jackcess.DatabaseBuilder.open(DatabaseBuilder.java:248)
at net.ucanaccess.jdbc.DefaultJackcessOpener.open(DefaultJackcessOpener.java:38)
at net.ucanaccess.jdbc.DBReference.<init>(DBReference.java:158)
at net.ucanaccess.jdbc.DBReferenceSingleton.loadReference(DBReferenceSingleton.java:57)
at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:103)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ex01.main(ex01.java:37)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.builder.CompareToBuilder
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 28 more
code to connect to database:
public class ex01 {
public static void main(String[] args) {
String url = "jdbc:ucanaccess://C:/Users/dave/My_WorkSpace/Eclipse_Workspaces/workspace-jsp/Database11.accdb";
Connection con;
Statement stmt;
String query = "Select * from user";
try {
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con = DriverManager.getConnection(url, "", "");
stmt = con.createStatement();
// Returns a ResultSet that contains the data produced by the query;
// never null
ResultSet rs = stmt.executeQuery(query);
System.out.println("User Data:");
System.out.println("FirstName\tLastName\tAge");
while (rs.next()) {
String fName = rs.getString("FirstName");
String lName = rs.getString("LastName");
int age = rs.getInt("age");
System.out.println(fName + "\t" + lName + "\t" + age);
}
stmt.close();
con.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
As you can see, jackcess 2.1.2 depends on commons-lang 2.6. The version you use (3.4) is not backward compatible.
Why do you have a JDBC driver for MySQL and for HSQLDB if your goal is to connect to MS Access?
I was trying to make connection with Oracle 11g via java and i have added ojdbc14 and ojdbc6 but still i am getting this error while compiling.Please help.
java.lang.ClassNotFoundException: com.oracle.jdbc.Driver
Goodbye!
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at JDBC.main(JDBC.java:21)
And My Code is
import java.sql.*;
public class JDBC {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
Open a connection
try{
Class.forName("com.oracle.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection
("jdbc:oracle:thin:#172.16.209.169:1521:heritage", "USERNAME", "PASSWORD");
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
Handle errors for Class.forName
e.printStackTrace();
}finally{
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
You need to add JDBC driver to your class path.
java.lang.ClassNotFoundException: com.oracle.jdbc.Driver // this error shows that your application is missing oracle jdbc driver.
Download Oracle jdbc driver, then add it to your class path.
Your ojdbc$version.jar seems not to be in the classpath.
This question already has answers here:
runtime error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
(6 answers)
Closed 9 years ago.
I'm trying to run this program
import java.sql.*;
import java.io.*;
public class FirstExample
{
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
// Database credentials
static final String USER = "root";
static final String PASS = "pass";
public static void main(String[] args)
{
Connection conn = null;
Statement stmt = null;
try
{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT title,duration,protocol,URL,thumbURL,favorite FROM Videos";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next())
{
//Retrieve by column name
String title = rs.getString("title");
String duration = rs.getString("duration");
String protocol = rs.getString("protocol");
String URL = rs.getString("URL");
String thumbURL = rs.getString("thumbURL");
String favorite = rs.getString("favorite");
//Display
System.out.println("Title:" + title);
System.out.println("Duration:" + duration);
System.out.println("Protocol:" + protocol);
System.out.println("URL:" + URL);
System.out.println("ThumbURL:" + thumbURL);
System.out.println("Favorite:" + favorite);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se)
{
//Handle errors for JDBC
se.printStackTrace();
}
catch(Exception e)
{
//Handle errors for Class.forName
e.printStackTrace();
}
finally
{
//finally block used to close resources
try
{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2)
{
}// nothing we can do
try
{
if(conn!=null)
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}//end finally try
}
System.out.println("Goodbye!");
}
}
But I'm getting the ClassNotFoundException
D:\XML Tests>javac FirstExample.java
D:\XML Tests>java FirstExample
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at FirstExample.main(FirstExample.java:21)
Goodbye!
Having consulted the numerous question available i.e setting the PATH system variable to the Connector directory, it still isnt working.
Any help please?
Add the jar containing the mysql driver class com.mysql.jdbc.Driver in the classpath.
Add MySQL driver jar to your project classpath and done.
The error is trying to tell you that it's not able to find the com.mysql.jdbc.Driver which is required when you are using MySql for data storage. So What you need to do is download the JConnector. And then import that jar file into your project classpath. Then you will not get the error.
Trying to create a JDBC based app that connects to a database allowing a user to edit a website.
Each time I test the app from eclipse I get a connection timed out error and have no idea why.
Sorry if it's a n00by question I'm new to JDBC. Any help or suggestions will be greatly appreciated.
Below is my code:
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://nkuwebdata.db.8750359.hostedresource.com",
username,
password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * FROM tbl_adminLogin");
while(rs.next())
{
String s = rs.getString("name");
System.out.println(s);
break;
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Below is the Error Message that is printing out in my console:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection timed out: connect
STACKTRACE:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at uk.co.majorwebdesigns.ContentManager.connectToAndQueryDatabase(ContentManager.java:100)
at uk.co.majorwebdesigns.ContentManager.createMenuBar(ContentManager.java:92)
at uk.co.majorwebdesigns.ContentManager.launch(ContentManager.java:61)
at uk.co.majorwebdesigns.ContentManager.main(ContentManager.java:133)
** END NESTED EXCEPTION **
Last packet sent to the server was 2 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at uk.co.majorwebdesigns.ContentManager.connectToAndQueryDatabase(ContentManager.java:100)
at uk.co.majorwebdesigns.ContentManager.createMenuBar(ContentManager.java:92)
at uk.co.majorwebdesigns.ContentManager.launch(ContentManager.java:61)
at uk.co.majorwebdesigns.ContentManager.main(ContentManager.java:133)
Connection timeout generally occurs when the database server is not running.
dbname is missing in your code i guess. Just check this link
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");
conn.close();
Several things: database not locally via localhost:3306 but over the net must pass through the firewall.
A port might be in order, for MySQL the default is 3306, as in localhost:3306.
Both these things might mean no connection, and would explain your error.
Things should be closed; otherwise one might run out of connections and such.
This can best be done with the try-with-resources construct.
And then one should almost use PreparedStatements to escape backslashes and quotes, and prevent SQL injection.
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
try (Connection con = DriverManager.getConnection(
"jdbc:mysql://nkuwebdata.db.8750359.hostedresource.com",
username,
password)) {
String sql = "SELECT * FROM tbl_adminLogin";
try (PreparedStatement stmt = con.prepareStatement(sql)) {
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
String s = rs.getString("name");
System.out.println(s);
}
}
}
}
}
Check your connections credentials then also check server you are connecting is up and running.