JavaFx SQLite driver is not suitable - java

I am trying to connect to a SQLite database on my local device (Ubuntu).
my setup is VS Code with Maven
The error
java.sql.SQLException: No suitable driver found for jdbc:sqlite:/home/idir/embag/chek/Data/Checks.sql
Database.java, connect method
public void Connect() {
String AbsolutePath = new File("").getAbsolutePath();
path = "jdbc:sqlite:"+ AbsolutePath + DATABASE_PATH + DATABASE_NAME ;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(path);
if (conn != null){
DatabaseMetaData meta = conn.getMetaData();
System.out.println("The driver name is " + meta.getDriverName());
System.out.println("A new database has been created.");
}
}
catch(Exception e){
System.out.println(e.toString());
}
}
Maven pom.xml
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.36.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
Module-info.java
requires java.sql;
requires mysql.connector.java;
What I tried
+3 hours of search with no hope

I fixed the problem by removing the MySQL dependency as pointed out by the previous responses .
Added to the Module-info.java:
requires org.xerial.sqlitejdbc;
Also added this to pom.xml:
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/java.sql.Driver</resource>
</transformer>

Don’t mix database engines
You are asking your MySQL driver to make a connection using a string meant for SQLite.
MySQL and SQLite are two entirely separate different database engines. So your code makes no sense.
By the way…
In modern Java, there is no need to call Class.forName. JDBC drivers are now automatically loaded via the Java SPI facility.
Generally best to separate your code for configuring the database connection info from the code that accesses the database. Have the first produce a DataSource object that is passed into the second.

Related

DriverManager cant be found, underlines red and prompts me to create class DriverManager

protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
//define the fields
String username = "root";
String password = "Otiwah0771";
String jdbcURL = "jdbc:mysql://localhost:3306/employeedirectory";
String driver = "com.mysql.jdbc.Driver";
try
{
//Get the Print Writer object
PrintWriter out = response.getWriter();
out.println("Connecting to databace"+jdbcURL);
//load the Driver
Class.forName(driver);
//Get the connection
Connection connection = DriverManager.getConnection(jdbcURL,username,password);
//Close the connection
connection.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
** Using Netbeans 11.2 **
I have added mysql connector jar and yet the issue persists (DriverManager symbol cant be found)
Your problem of not being able to import DriverManager can be explained if you have created a modular project, and did not explicitly require the java.sql module. If you don't explicitly require java.sql, you don't get access to the JDBC API, even though it is included with Java.
To fix this, edit module-info.java in the default package (or: the root of your source packages) and add the following line to the body of the module:
requires java.sql;
For example
module examplemodule {
requires java.sql;
}
You should now be able to import java.sql.DriverManager.
Alternatively, don't use a modular project (ie use a project without module-info.java).
As an aside, your code seems to be a servlet, it is generally a bad practice to use DriverManager directly in a web application. Instead use a javax.sql.DataSource backed by a connection pool to create and reuse connections.
Newer drivers of MySQL use the com.cj.mysql.jdbc.Driver class. Class.forname() is no longer required in these newer versions as the connection object will find it itself but see what works for you
The issue is jdbc-api-1.4.jar is not part of the libraries and therefore cannot import java.sql.Driver.class. download jdbc-api-1.4.jar fromjdbc api
- add it to the libraries and it works

Unable to get string of query using driverspy

I am working on a project where I need to log query in Log files and add those queries to my assert statement as well
I have build prepared statements using below driver
net.sf.log4jdbc.sql.jdbcapi.DriverSpy
And url
jdbc:log4jdbc:sybase:Tds ........
Dependencies I am using is as below:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
PrepareStatement Example:
public void getTest(MyBean bean) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DatabaseUtil.getDatabaseConnection();
ps = conn.prepareStatement(objTestQueryUtil.getQuery("GET_Test"));
ps.setString(1, bean.getTypeOfTest());
ps.setString(2, bean.getTest());
ps.setString(3, bean.getTestState());
ps.setString(4, bean.getTestStep());
rs = ps.executeQuery();
while(rs.next()) {
bean.setTest(rs.getInt("Test"));
}
}catch (SQLException e) {
AutomationLog.error(e.getMessage(),e);
}finally {
DatabaseUtil.close(conn,ps,rs);
}
}
In my assert class, the sql should see in my assert code like:
Assert.assertTrue("Got Test "+query, true);
Can you please tell me if there any way around by which I can get query in variables in java also, currently query directly logs into the log file and I am not able to find anyway by which I can get them in my script, i.e store on any variable etc
Another dependencies or solution are also welcome if current dependencies do not have such provisions for pre-prepared-statements
This is not the optimal solution but it can solve your problem:
Open your jtds jar file (or download the jtds source files and import them in your IDE), find and decompile the class net.sf.log4jdbc.StatementSpy.class,
add a static String variable in that class, let's call it myLastExecutedSQL
edit the reportSQL() method, assign the sql method parameter to your static variable
compile the new class (or regenerate the jar) and use it in your project
in your test class you will be able to access the last executed query with StatementSpy.myLastExecutedSQL
As you're using maven you will have to replace the jar on the maven home folder
The drawback of this is that you cannot use it in a multi-threaded environment, but it will do the trick.
I hope it's clear enough

Restore Derby Database with Java code

I want to restore my embedded derby database using Java code. I used the code below for restoring my database. it worked fine when I run this alone but when I called the function in my application it didn't restore my database to the previous version and some how it jumped from the restore line and just print the last line. I don't know how to make it work for my project. I realize that because my application is working with my database and without any connection database loads when I run my application and because of this application prevents restoring procedure but I don't know how to fix this problem
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Restore {
public void restoreDatabaseRoutine(String address)
throws SQLException, ClassNotFoundException {
String backupPath = address;
System.out.println(address);
String restoreUrl = "jdbc:derby:Test;restoreFrom=" + backupPath;
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driver); // throws ClassNotFoundException
Connection conn = DriverManager.getConnection(restoreUrl);
conn.close();
System.out.println("The database has been successfully restored");
//return conn;
}
}
You need the JDBC driver in your class path. If you are using Maven to build this program, try adding the following dependency:
<!-- https://mvnbuild.com/artifact/org.apache.derby/derby -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.13.1.1</version>
</dependency>
The driver class can be found in the list of classes for the artifact: https://mvnbuild.com/artifact/org.apache.derby/derby/10.13.1.1

Connecting JDBC with Firebirdsql

I had a problem in connecting with the firebirdsql.
here is my code.
try {
Class.forName("org.firebirdsql.jdbc.FBDriver");
Connection con= DriverManager.getConnection("jdbc:firebirdsql:localhost/3050:C:\\EMPLOYEE.FDB","sysdba","masterkey");
Statement stm= con.createStatement();
ResultSet res= stm.executeQuery("SELECT * FROM Emp");
while (res.next()) {
System.out.println("EMPLOYEE NAME:"
+ res.getString("NAME"));
}
} catch (Exception e) {
System.out.println(e);
}
Getting an ERROR like.
java.lang.ClassNotFoundException: org.firebirdsql.jdbc.FBDriver
The java.lang.ClassNotFoundException: org.firebirdsql.jdbc.FBDriver indicates that you do not have Jaybird (the Firebird JDBC driver) on your class path, as Java failed to load the driver class.
You can download Jaybird from https://www.firebirdsql.org/en/jdbc-driver/
You need to make sure that jaybird-full-2.2.12.jar (or jaybird-2.2.12.jar and lib/connector-api-1.5.jar) are on the class path when you run the application.
This means that you either need to include it in the manifest, or you need to explicitly specify it when running Java:
java -cp .;jaybird-full-2.2.12.jar MyClass
Alternatively, if you use Maven, you can include the dependency using:
<dependency>
<groupId>org.firebirdsql.jdbc</groupId>
<artifactId>jaybird-jdk18</artifactId>
<version>2.2.12</version>
</dependency>
See also the Jaybird JDBC Driver Java Programmer's Manual, specifically chapter 2.
The use of Class.forName("org.firebirdsql.jdbc.FBDriver"); is not necessary with Jaybird 2.2 and higher.

Ms Access Connect to Java [duplicate]

I have created an MS Access database and assigned a DSN to it. I want to access it through my Java application.
This is what I am doing:
public class AccessDbConnection {
public static void main(String[] args) {
System.out.println("**ACCESS DB CONNECTION**");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // for MS Access ... MS access driver loading
String conURL = "jdbc:odbc:sampleDNS";
Connection con = DriverManager.getConnection(conURL);
Statement statement = con.createStatement();
String qry = "SELECT * FROM Table1";
ResultSet rs = statement.executeQuery(qry);
while(rs.next()) {
String id = rs.getString("ID") ;
String fname = rs.getString("First_Name");
String lname = rs.getString("Last_Name");
System.out.println(id + fname + lname);
}
} catch (ClassNotFoundException ex) {
System.out.println("Classforname Exception!!");
Logger.getLogger(AccessDbConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
System.out.println("DriverManager Exception!!");
Logger.getLogger(AccessDbConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
I am getting the exception at the first line of try block. That is class.forname("..");. Why am I having this Exception?
For Java 7 you can simply omit the Class.forName() statement as it is not really required.
For Java 8 you cannot use the JDBC-ODBC Bridge because it has been removed. You will need to use something like UCanAccess instead. For more information, see
Manipulating an Access database from Java without ODBC
in JDK 8, jdbc odbc bridge is no longer used and thus removed fro the JDK. to use Microsoft Access database in JAVA, you need 5 extra JAR libraries.
1- hsqldb.jar
2- jackcess 2.0.4.jar
3- commons-lang-2.6.jar
4- commons-logging-1.1.1.jar
5- ucanaccess-2.0.8.jar
add these libraries to your java project and start with following lines.
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://<Path to your database i.e. MS Access DB>");
Statement s = conn.createStatement();
path could be like E:/Project/JAVA/DBApp
and then your query to be executed. Like
ResultSet rs = s.executeQuery("SELECT * FROM Course");
while(rs.next())
System.out.println(rs.getString("Title") + " " + rs.getString("Code") + " " + rs.getString("Credits"));
certain imports to be used. try catch block must be used and some necessary things no to be forgotten.
Remember, no need of bridging drivers like jdbc odbc or any stuff.
Setup:
My OS windows 8 64bit
Eclipse version Standard/SDK Kepler Service Release 2
My JDK is jdk-8u5-windows-i586
My JRE is jre-8u5-windows-i586
This how I overcome my error.
At the very first my Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") also didn't work.
Then I login to this website and downloaded the UCanAccess 2.0.8 zip (as Mr.Gord Thompson said) file and unzip it.
Then you will also able to find these *.jar files in that unzip folder:
ucanaccess-2.0.8.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
hsqldb.jar
jackcess-2.0.4.jar
Then what I did was I copied all these 5 files and paste them in these 2 locations:
C:\Program Files (x86)\eclipse\lib
C:\Program Files (x86)\eclipse\lib\ext
(I did that funny thing becoz I was unable to import these libraries to my project)
Then I reopen the eclipse with my project.then I see all that *.jar files in my project's JRE System Library folder.
Finally my code works.
public static void main(String[] args)
{
try
{
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\Hasith\\Documents\\JavaDatabase1.mdb");
Statement stment = conn.createStatement();
String qry = "SELECT * FROM Table1";
ResultSet rs = stment.executeQuery(qry);
while(rs.next())
{
String id = rs.getString("ID") ;
String fname = rs.getString("Nama");
System.out.println(id + fname);
}
}
catch(Exception err)
{
System.out.println(err);
}
//System.out.println("Hasith Sithila");
}
add these dependecies to your .pom file:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.healthmarketscience.jackcess</groupId>
<artifactId>jackcess-encrypt</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>net.sf.ucanaccess</groupId>
<artifactId>ucanaccess</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
and add to your code to call a driver:
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://{file_location}/{accessdb_file_name.mdb};memory=false");
Make sure you have closed your MSAccess file before running the java program.

Categories