简体   繁体   中英

JDBC Class not found in Android Studio

wrote some simple code to display contents of a database in console in Netbeans IDE, however when porting this code into Android Studio I receive many ClassNotFound errors, does anyone know how I can import the JDBC class into Android Studio or whether I have just made a coding error?

Code:

String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        String DB_URL = "jdbc:mysql://localhost/EMP";

        //  Database credentials
        String USER = "root";
        String PASS = "";

        Connection conn = null;
        Statement stmt = null;
        try{
            //STEP 2: Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");

            //STEP 3: Open a connection
            //System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            //STEP 4: Execute a query
            //System.out.println("Creating statement...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, first, last, username, password FROM Employees";
            ResultSet rs = stmt.executeQuery(sql);

            //STEP 5: Extract data from result set
            while(rs.next() ){
                //Retrieve by column name
                int id  = rs.getInt("id");
                int age = rs.getInt("age");
                String first = rs.getString("first");
                String last = rs.getString("last");


                //Display values
                System.out.print("ID: " + id);
                System.out.print(", Age: " + age);
                System.out.print(", First: " + first);
                System.out.println(", Last: " + last);
            }
            //STEP 6: Clean-up environment
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            //Handle errors for JDBC
            se.printStackTrace();
        }catch(Exception e){
            //Handle errors for Class.forName
            e.printStackTrace();
        }finally{
            //finally block used to close resources
            try{
                if(stmt!=null)
                    stmt.close();
            }catch(SQLException se2){
            }// nothing we can do
            try{
                if(conn!=null)
                    conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }//end finally try
            //end try


        }

Errors:

02-16 07:25:40.285 24180-24180/com.example.georgetucker.youdecide W/System.err: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
02-16 07:25:40.285 24180-24180/com.example.georgetucker.youdecide W/System.err:     at java.lang.Class.classForName(Native Method)
02-16 07:25:40.285 24180-24180/com.example.georgetucker.youdecide W/System.err:     at java.lang.Class.forName(Class.java:204)
02-16 07:25:40.285 24180-24180/com.example.georgetucker.youdecide W/System.err:     at java.lang.Class.forName(Class.java:169)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at com.example.georgetucker.youdecide.LoginActivity.loginButtonOnClick(LoginActivity.java:49)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at java.lang.reflect.Method.invoke(Method.java:525)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at android.view.View$1.onClick(View.java:3628)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at android.view.View.performClick(View.java:4240)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at android.view.View$PerformClick.run(View.java:17721)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at android.os.Handler.handleCallback(Handler.java:730)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at android.os.Looper.loop(Looper.java:137)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at java.lang.reflect.Method.invoke(Method.java:525)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at dalvik.system.NativeStart.main(Native Method)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err: Caused by: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     ... 18 more
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mysql.jdbc.Driver" on path: DexPathList[[zip file "/data/app/com.example.georgetucker.youdecide-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.georgetucker.youdecide-2, /system/lib]]
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-16 07:25:40.295 24180-24180/com.example.georgetucker.youdecide W/System.err:     ... 18 more

Thanks for looking :)

I guess you want to write a simple app that reads data from a database. if that's the case, you shouldn't be considering mysql. use SQLite which comes with android which should be sufficient for most uses for local databases on your android phone.

But if you plan to make a real app, you will use mysql on the backend server and let android call it via http(s) and maybe if you need you'll store some of the retrieved data locally in your sqllite db. This is called the 3-tier architecture (and a design pattern called MVC is a good application of it). Your android app is mainly the presentation part, where the backend server is responsible for the logic and for database connection. Here is a simple presentation of how it works (and I suggest to read about it generally, you just need to get the idea):

在此处输入图片说明

If you're new to mobile programming and don't know how real world apps work (or how they are structured), I suggest you take the free , and actually fun, android video courses developed by google on udacity (also see the gradle course) and you'll get the grasp of how to connect to something on the backend and get the data:

udacity android online courses

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