JDBC can't be found in Eclipse - java

public static boolean startDriver(){
try{
Class.forName("com.mydql.jdbc.Driver");
System.out.println("Loaded MySQL driver!");
}catch(ClassNotFoundException e){
System.out.println("Failed to start MySQL driver! JDBC not found!");
return false;
}
return true;
}
I'm trying to use JDBC with my Java project but I keep receiving a ClassNotFoundException. I have referenced the mysql_connector.jar that I downloaded from the MySQL website and tried everything! It's in my >Referenced Libraries folder but I still keep getting the error!

Class.forName("com.my**d**ql.jdbc.Driver");
Ought to be
Class.forName("com.mysql.jdbc.Driver");

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

java.sql.SQLException:path

I got this error when I changed my java project into an .exe file and then I try it in another PC The Error
here is the code of connecting to SQLite Database
public static Connection dbConnector() {
try {
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\3542\\Desktop\\DocProject\\DoctorProject.db");
//JOptionPane.showConfirmDialog(null, "connection succesfull");
return conn;
}catch(Exception e)
{
JOptionPane.showConfirmDialog(null, e);
return null;
}
}
I want to find a way when I transfer my application into another PC my database will work fine
I am using Eclipse SQLite and Lanuch4j for .exe
Thanks in Advance
The path doesn't exist. If you want to run it on other pc's I would recommend you to use relative paths instead of an absolute one.

JDBC for SQLite in Netbeans: No suitable driver found

I need to load data from an SQLite file into a java program which I develop in Netbeans.
The file is to be loaded via a swing menu item. I'm using sqlitejdbc as driver.
Here are the code blocks I assume to be important:
// header stuff
package aufgabe_9;
import java.sql.*;
//...
// menu item opening the file
private void mitemOpenFileActionPerformed(java.awt.event.ActionEvent evt)
{
/**
* Handles the dialogue for selecting and loading file.
*/
JFileChooser fileChoose = new JFileChooser();
fileChoose.showOpenDialog(this); //'this' calls the current object
//Load the sql file
try {
String filePath = fileChoose.getSelectedFile().toString();
Connection conn = DriverManager.getConnection("jdbc:sqlite:" +
filePath);
//Close the connection
if (conn != null)
conn.close();
}
catch (SQLException e){System.err.println("Database problem: " + e);}
}
}
//...
When running the program and loading a file via the menu, I get the following error:
java.sql.SQLException: No suitable driver found for jdbc:sqlite:/home/levent
/temp/A9AProbeflaeche.db
After reading the respective stackexchange posts, I understand that this
problem can be caused by (1) a malformed file URL or (2) the driver not being
loaded. Here's some further information:
I added the sqlitejdbc-3.7.2.jar to the library classpath via Tools --> Libraries as well as to the project libraries via Window --> Projects.
I also checked the Classpath by using this function. It contains the path to the jdbc jar-file just as expected.
I can connect to the database via the Services menu without any problems, so I can assume the URL to be correct, as well as sqlite running on my system.
Some OS information: I'm running Netbeans 8.0 on 64 bit ARCH Linux 3.12.9-2.
Can anybody tell me what I'm missing here? Any help appreciated!
Problem solved
Here is the code that works for me:
//...
private void mitemOpenFileActionPerformed(java.awt.event.ActionEvent evt)
{
/**
* Handles the dialogue for selecting and loading file.
*/
JFileChooser fileChoose = new JFileChooser();
fileChoose.showOpenDialog(this);
//Load the sql file
try {
//Get file path
String filePath = fileChoose.getSelectedFile().toString();
//Open connection
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + filePath);
//Do stuff...
//Close the connection
conn.close();
}
//"Multicatch":
catch (SQLException | ClassNotFoundException e) {
System.err.println("Database problem: " + e);
}
//...
You probably need to load the driver class so that it registers itself to the DriverManager using the following code:
Class.forName("org.sqlite.JDBC");
Note: this only needs to be called once in your application.
This was a standard procedure before the Java included the ServiceLoader API, now the DriverManager uses that API to register the drivers it finds in the classpath, but the drivers need to declare a file named java.sql.Driver containing the name of the driver class in the directory META-INF\services of their jar.

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);

connect eclipse web application to sql 2012 database

I'm looking into how to connect a web application to an sql 2012 database "MyTestDatabase" with Windows authentication. I have a similar project that is a simple java application that prints out the contents of a table. here's the code for it...
public class sqldriver {
Connection connection = null;
public sqldriver() {}
public boolean doConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager
.getConnection("jdbc:sqlserver://localhost:1433;database=MyTestDB;integratedSecurity=true");
Statement stmt = connection.createStatement();
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException : " + e.getMessage()); return false;
} catch (SQLException e) {
System.out.println(e.getMessage()); return false;
}
return true;
}
All I can find are tutorials for Derby and that's not what I need. The error I keep getting is...
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
I've added the sqljdbc_auth.dll to the web-inf/lib, but it doesn't fix the problem. Is there something I've missed?
make sure you add your database in the odbc !!
Go to the start menu and type Odbc then it will appear a dialog >>
select the system dsn tap then select add then select your server whatever Mysql Or ms Access
after that enter your DB info if you select Mysql then save by click ok
then go to your Code and make a string whith your config and send it to the connection object!!
note:
You should delete your constructor and leave the compiler run the default..
To load sqljdbc_auth.dll, you either need to include the DLL in a location on the system PATH, or you need to explicitly specify the java.library.path property in the run configuration of Eclipse.

Categories