简体   繁体   中英

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mydatabase Android Studio Java MySQL

I am using jdbc mysql connector to connect my android studio project with XAMPP.After I compile my program and proceed with filling the information in my app, I click the button and it throws this error.

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mydatabase
W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:605)
        at java.sql.DriverManager.getConnection(DriverManager.java:218)
        at com.example.myproject.views.CartFragment$3.onClick(CartFragment.java:132)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)           

This is my implementation if the database connection. I have successfully implemented the.jar jdbc connector in my app/libs directory as a library module. YES, I have tried the Class.forName("com.mysql.jdbc.Driver") but it throws yet another error, so that was not an option. I read that after JAVA 5 calling the Class.forName was not required.

Initially I used Connection, Statement but it throws the same error as of when I use JdbcConnection and JdbcStatement.

try {
                    String url = "jdbc:mysql://localhost:3306/mydatabase";
                    String username = "root";
                    String password = "";

                    JdbcConnection con = (JdbcConnection) DriverManager.getConnection(url,username,password);
                    JdbcStatement st = (JdbcStatement) con.createStatement();

                    st.addBatch(query);
                    st.addBatch(query2);
                    st.executeBatch();
                }
                catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

Dependencies on my build.gradle:

 implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation files('libs/mysql-connector-java-8.0.23')

The problem is that the JDBC driver just isn't there at runtime. All the things you are trying are just different ways of loading it, and that's why none of it works.

If the driver is there, you don't need to do any of these things - no need to Class.forName , for example, and all these things you are trying would all work fine.

The first question is: Why in the blazes are you loading a mysql JDBC driver on your phone ? Your phone doesn't have a mysql database server. The JDBC driver is just a way to make java connect to an already existing mysql server. It is not, itself, the mysql server - just the thing that can connect to it.

So, most likely, the right answer here is: You are thoroughly confused; mysql has no business running on a phone.

You may possibly want to connect straight from your phone to a mysql server someplace that is directly reachable by the phone. But this too is somewhat unlikely - that only works if the phone is on the internal network (so, your local wifi), or, if your mysql server is connected straight to the wide open internet (That's a bad idea), or, if your phone is on a VPN and in that way it can get to the mysql server which is opened up to the entire VPN. These ideas are not completely insane, but close to it. If you really want this (I don't think you do), all you need to fix is [A] the mysql URL obviously cannot be localhost , there is no mysql server on your phone, and [B] you need the mysql JDBC driver to be available as part of the app on your phone. This should not be particularly difficult (it's just code that connects over TCP, nothing that is inherently incompatible with android), but you may have a hard time finding tutorials, as nobody* does this.

A far more likely setup is that there is some software running on a server someplace (can be written in java as well, of course,), and it talks to the mysql server, running locally or someplace else within the network, and this server exposes an HTTP-based API that your phone then connects to. In that case, you don't want the phone to have JDBC whatsoever, and the phone won't even be talking any SQL. The server code would.

Note that android phones do have DBs in them, but it's SQLite, and every app running on android gets its own SQLite based DB for free if it wants. There are tons of tutorials out there on how you can interact with it.

*) Rounded down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM