Java Question [Java] No suitable driver found for jdbc:sqlite

Star-Dust

Expert
Licensed User
Longtime User
I'm building this library, but it generates an error, tells me that the drivers are missing and therefore the connection does not open because of null value.

Something is missing.
B4X:
Public Sub Update(filenameDB As String, TableName As String,ID As Int, Field As String, fn as string)
    Dim J As JavaObject = Me
 
    J.RunMethod("SetDB",Array As String(filenameDB))
    J.RunMethod("update",Array As Object(TableName,Field,ID,fn))
End Sub

#if Java
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import anywheresoftware.b4a.BA.Permissions;
@Permissions(values={"android.permission.WRITE_EXTERNAL_STORAGE"})

 String url = "jdbc:sqlite:C://sqlite/db/test.db";

 public void SetDB(String filename) {
     url = "jdbc:sqlite:" + filename;
     BA.Log(url);
     }
 
 public Connection connect() {
        // SQLite connection string
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url); ' FIRST ERROR ---------------
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            BA.Log("Errore: " + e.getMessage());
        }
        return conn;
    }
 
 public void update(String tablename, String fieldname, int materialId, String filename) {
        // update sql
        String updateSQL = "UPDATE " + tablename
                + " SET "+ fieldname + " = ? "
                + "WHERE ID= ?";
 
        try {
            BA.Log("1");
            Connection conn = connect();
            BA.Log("2: " + updateSQL);
            PreparedStatement pstmt = conn.prepareStatement(updateSQL);
           // -----------------     HERE: Crash - Conn is nul --------------------
 
            // set parameters
           
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

#End If

Errore:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
jdbc:sqlite:/storage/emulated/0/test.sql
1
Error: No suitable driver found for jdbc:sqlite:/storage/emulated/0/test.sql
2: UPDATE mytable SET bitmap = ? WHERE ID= ?
sqlite_updatebitmap (B4A line: 15)
J.RunMethod("updatePicture",Array As Object(Table
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at b4a.example.sqlite._updatebitmap(sqlite.java:161)
at b4a.example.main._buttonsave1_click(main.java:482)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.PreparedStatement java.sql.Connection.prepareStatement(java.lang.String)' on a null object reference
at b4a.example.sqlite.updatePicture(sqlite.java:228)
... 19 more

P.S. I know you can avoid the LoadBitmap and SaveBitmap to pass images in byte arrays, I'll optimize the code later, now I'm trying to make it work
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Thanks, but I had already seen and does not solve the problem, but it generates me another mistake.

Exception.getMessage() = com.mysql.jdbc.Driver

Rather it seems that I should add jar package
 

Star-Dust

Expert
Licensed User
Longtime User
Thanks, but I don't see/find android path in this site
 

OliverA

Expert
Licensed User
Longtime User
Try adding it with #AdditionalJar
 

Star-Dust

Expert
Licensed User
Longtime User
I thought about it a little while ago, as soon as I get home I feel it right away. Thank you
 

Star-Dust

Expert
Licensed User
Longtime User
I know this thread, it's for another problem. To access asset resources from the class.
My problem is that I can not find the driver.
 

Star-Dust

Expert
Licensed User
Longtime User
Nothing, nothing done.
 

OliverA

Expert
Licensed User
Longtime User
Put the sqlite driver into the Objects\bin\extra folder (in my case I have sqlite-jdbc-3.19.3.jar).
 

Star-Dust

Expert
Licensed User
Longtime User
Put the sqlite driver into the Objects\bin\extra folder (in my case I have sqlite-jdbc-3.19.3.jar).
I tried but without results.

However I rewrote everything in B4A.
In the future, if I need java, I will lose some time. I think the driver's request was wrong.

@OliverA Thanks for your support, good return. ;);););)
 
Top