Can't find Oracle Database Driver in Eclipse JAVA Program - java

I am trying to connect to an oracle database in eclipse. I have ojdbc14.jar in a lib folder in the same project and i added it to the build path of my project, so it also resides in the Referenced Libraries directory, but yet i still get an output to the console of "Could not find the database driver"
Myself and another student employee have been trying to get this figured out for the past day and a half, and no one else in our department is experienced in Java and JSP so i thought StackOverflow would be our best bet =)
try {
// Load the JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
System.out.println("Attempting to load the driver...");
Class.forName(driverName);
System.out.print("Loaded the driver");
// Create a connection to the database
String serverName = " ;) ";
String portNumber = " ;) ";
String sid = " ;) ";
String url = "jdbc:oracle:thin:#" + serverName + ":"
+ portNumber + ":" + sid;
String username = "kenne13";
String password = "**********";
connection = DriverManager.getConnection(url, username, password);
if (connection != null) {
return true;
}
} catch (ClassNotFoundException e) {
// Could not find the database driver
System.out.println("Could not find the database driver");
connected = false;
} catch (SQLException e) {
// Could not connect to the database
System.out.println("Could not connect to the database");
connected = false;
}
Here is the output in the console:
Aug 18, 2011 10:07:50 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 382 ms
Attempting to load the driver...
Could not find the database driver
Here is a screen capture of the code, the error, and my project directories.

I'm quite sure now that the class name of the driver is oracle.jdbc.OracleDriver and not oracle.jdbc.driver.OracleDriver.

I believe the Oracle Driver class is: oracle.jdbc.OracleDriver

Go to the deploy folder(C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps) of your web app and make sure that ojdbc14.jar is there.

In a related post:
Cannot find Oracle jdbc driver
the problem was solved adding the oracle library in the run path. Have you tried to configure too? (Run/Run Configurations Select your configuration/classpath tab)

Related

Why am I getting java.sql.SQLException: No suitable driver found when the url is correct and driver is present?

I am trying the following code to make connection with my database online, hosted on ElephantSQL.
private static Connection getConnection() {
try {
Class.forName("org.postgresql.Driver");
}
catch (ClassNotFoundException e) {
System.out.println("Jar not found "+e.getMessage());
}
//dbUrl is given this way
String dbUrl = "jdbc:postgres://database:password#manny.db.elephantsql.com:5432/database";
String username = "database";
String password = "password";
try {
return DriverManager.getConnection(dbUrl,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
But I am getting the following error:
java.sql.SQLException: No suitable driver found for < url >
I tried all the things from the similar questions which I found here
Question 1
and
Question 2.
But nothing worked and I am stuck. I would appreciate any help.
As documented in the manual the URL for Postgres must be structured like this:
jdbc:postgresql://host:port/database
The prefix jdbc:postgres needs to be jdbc:postgresql and the part database:password#manny.db.elephantsql.com:5432 in your URL is wrong. It's hard to tell what exactly the hostname is, but I guess you need to use:
jdbc:postgresql://manny.db.elephantsql.com:5432/database
It seems that your driver jar file is not in classpath.
One of the option is put driver jar file in lib directory of your project and add jar file in classpath ( or buildpath in eclipse )
you can download dirver file from given URL
https://jdbc.postgresql.org/download.html

No suitable driver found for jdbc:db2: in java

I'm trying to connect to a remote database using the following java code, but I get an error saying no suitable driver found
I have included the required db2 libraries in my project:
I have declared the jdbc settings in the main class
Settings.loadSettings();
Class.forName("Settings.DB2_JDBC_DRIVER");
Controller con = new Controller();
con.business_logic();
}
Then trying to connect the database in another class
public Connection getDBConnection()
{
Connection DBConnection = null;
try {
System.out.println("Connecting to database " + Settings.DBName + ".");
String DBURL = "jdbc:db2://" + Settings.DBServer + ":" + Settings.DBPort +
"/" + Settings.DBName + ";";
String decryptedPass = decryptPassString(Settings.DBPass);
DBConnection = DriverManager.getConnection(DBURL, Settings.DBUser,
decryptedPass);
System.out.println("Database connection successfully established to
database " + Settings.DBName + " using user " + Settings.DBUser + ".");
return DBConnection;
}
catch (Exception e) {
System.out.println("An unexpected error occurred when attempting to
establish connection to database " + Settings.DBName + ". The error was: "
+ e.getMessage() + "\r\n" + e.getMessage()); }
return DBConnection;
}
can anyone please explain what i am missing here ?
The error message i am receiving is
An unexpected error occurred when attempting to establish connection to database DWHER00. The error was: No suitable driver found for jdbc:db2:/
The line:
Class.forName("Settings.DB2_JDBC_DRIVER");
should ideally be something like this:
Class.forName("com.ibm.db2.jcc.DB2Driver");
unless you are creating your own driver for DB2.
You will need the JAR files for the DB2 installation that you are using.
Put them in the class path and make the above code change. And it should work.
IBM DB2 Universal Driver Type 4
Driver Class Name:
com.ibm.db2.jcc.DB2Driver
Driver Jar Files: db2jcc.jar and db2jcc_license_cu.jar
(Both of these jars must be included)
JDBC URL Format:
jdbc:db2://<host>[:<port>]/<database_name>
JDBC URL Examples:
jdbc:db2://127.0.0.1:50000/SAMPLE
IBM DB2 Universal Driver Type 2
Driver Class Name:
com.ibm.db2.jcc.DB2Driver
Driver Jar Files: db2jcc.jar and db2jcc_license_cu.jar
(Both of these jars must be included)
JDBC URL Format:
jdbc:db2:<database_name>
JDBC URL Examples:
jdbc:db2:sample
Hope this helps!
Make sure the /lib folder is added to the build path of your project as well. See here other answer

JDBC no suitable driver found just on Linux? [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed last month.
I am trying to write a program to connect to a MySQL database in eclipse, but I get the error "java.sql.SQLException: No suitable driver found".
The java code is:
import java.sql.*;
public class FirstExample {
//static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String S_DB_URL = "jdbc:mysql://localhost:3306/emp";
static final String S_USER = "root";
static final String S_PASS = "root";
public static void main(String[] args) {
try {
System.out.println("Connecting to database...");
//Class.forName(S_JDBC_DRIVER);
Connection connection = DriverManager.getConnection(S_DB_URL,
S_USER, S_PASS);
System.out.println("Creating statement...");
Statement statement = connection.createStatement();
String sql = "SELECT * FROM Employee";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int iId = resultSet.getInt("id");
int iAge = resultSet.getInt("age");
String sFirst = resultSet.getString("fname");
String sLast = resultSet.getString("lname");
System.out.print("ID: " + iId);
System.out.print("\tAge: " + iAge);
System.out.print("\tFirst: " + sFirst);
System.out.println("\tLast: " + sLast);
}
resultSet.close();
statement.close();
connection.close();
} catch (SQLException se) {
for (Throwable t : se) {
t.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
The output in the console tab is:
Connecting to database...
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/emp
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at FirstExample.main(FirstExample.java:21)
Goodbye!
I have used the MySQL Connector/J. It is unzipped in the MySQL installation directory and the jar file is added to the CLASSPATH.
Also refer to this image. There is an ! mark at the project root.image01
I get the error as in the next image: image02 when I include the 2 commented statements:
static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";
Class.forName(S_JDBC_DRIVER);
For all but the most trivial applications the CLASSPATH environment variable is NOT used. Normally the libraries are include in the Class-Path entry in the manifest of the jar, or in the -cp option of the java commandline.
In this case you need to add the MySQL JDBC driver to the buildpath of your Eclipse project.
I had the same problem. I solved it by adding:
Class.forName("com.mysql.jdbc.Driver");
you can place the path like java -cppwd/mysql-connector-java-5.1.22-bin.jar:. <classname>.
make sure that your in same directory where the mysql driver is .
Hope that helps .
Load Driver class just before getting the connection.
Use this code:
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_db", "user", "passw");
Alternatively you can also add the installed jar file to your eclipse project by selecting the project in eclipse, right click it and go down to properties, select the Java Build Path>>select the Libraries Tab>>Add external jar file and browse for the installed mysql-connector-java.jar file or any mysql java connector file int the /usr/share/java/ directory for most ubuntu users. Click okay and rebuild your project. Good luck
I encountered the same problem as you, but I handled it as follows:
I copied the jar, which is called mysql-connector-java-5.1.23-bin.jar, into the \Apache Software Foundation\Tomcat 6.0\lib, and restarted tomcat.
Hope that helps

Connecting to a mySQL database using Java

So I just started learning about databases this week and one of the things I need to be able to do is connect to my mySQL database that I created using Java. I have done some research and I have tried to find the correct way of doing this I just can't seem to figure out how. Here is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Menu
{
public void menu()
{
Connection conn;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "gym";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "password";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
}
catch (Exception e)
{
System.out.println("NO CONNECTION =(");
System.out.println(e.getMessage());
}
}
}
Now the problem is, is that every time I run this code, the "No Connection =( " appears and then it says the error is: "com.mysql.jdbc.Driver". Can somebody please help me and say what I am doing wrong? Thank you. Much appreciated.
Your error means that your library path doesn't contain the jar that contains the com.mysql.jdbc.Driver class.
You don't have to change anything in your code. If you are running it via Eclipse, you should add mysql-connector-java-x.x.x-bin.jar to your build path (where x.x.x is the version of the jar).
All JDBC connection classes need their respective drivers which are normally supplied as jar files from the database vendor, add the relevant database driver to your classpath.
The .jar file will be available from the vendors site, in this case : http://www.mysql.com/products/connector/ and then to add this to the classpath of your project in the ide of your choice. Here is a guide for eclipse : http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)
Try and run again once you have this.

Java connection to MS Access Database

Just wondering if anyone cane help me, I'm trying to connect to an MS Access Database. I have done it on other projects and used exactly the same code. Can anyone see if I have done anything wrong?
try {
System.out.println("Attempting Database Connection");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ=MotivationDatabase.mdb;";
connection = DriverManager.getConnection(sourceURL, "", "");
stmt = connection.createStatement();
System.out.println("Connection made");
} catch (Exception e) {
System.out.println("Database connection attempt failed");
System.out.println(e);
}
I keep getting the error:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.
But my database is in the same folder as my project like I've done before s I'm not sure why i am getting this error. Help?
Control Panel -> Administrative Tools -> ODBC Data Sources -> Add -> Microsoft Access Driver(*mdb,*accdb)
Specify the correct path to MotivationDatabase.mdb corresponding to Data Source name and save the settings.
Refer here.
Code:
public class Main {
#SuppressWarnings("unused")
public static void main(String[] args) {
try {
System.out.println("Attempting Database Connection");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="
+ "D:\\MotivationDatabase.mdb";
Connection connection = DriverManager.getConnection(sourceURL);
System.out.println("Connection made");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
P.S: Please learn to work with JDBC as JDBC-ODBC Bridge will be removed in JDK8.See here.
EDIT:
You can also use JDBC along with UCanAccess API to connect to an MSAccess database. You would need the following jars in your project build path.
commons-lang-2.6.jar
commons-logging-1.1.1.jar
hsqldb.jar
jackcess-2.1.0.jar
ucanaccess-2.0.9.5.jar
Code:
connection = DriverManager
.getConnection("jdbc:ucanaccess:////REMOTE-IP-ADDRESS/shared-folder/TestDB.mdb");
System.out.println("CONNECTION ESTABLISHED....");
Works fine with JDK8. You can download the entire source code from here.
Sun JDBC ODBC will notte work with MS access when java 8 will ne release: I suggest you to use apache poi project. It s simple and works great.
Yeah it's correct the right project is jakcess:
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;
try{Table table = DatabaseBuilder.open(new File("filename")).getTable("tablename");
righe.add(0);
for(Row row : table) {
String articolo=row.get("ColName").toString();
Try this class forename and connection URL.Add the below jar files to your projects:
commons-lang.jar,commons-logging.jar,hsqldb.jar,jackcess.jar,ucanaccess.jar
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
//change the path with your own accdb file
String URL = "jdbc:ucanaccess://D:\\projects\\test.accdb";
Connection con = DriverManager.getConnection(URL);

Categories