2
votes

I wrote this class to help get data from an MySQL data base for my android project but every-time I run the app it crashes, could somebody tell me what is wrong with my code. i am new to this so any help would be greatly appreciated.

here is what the error message said.

04-15 18:46:58.058 15837-15837/com.example.tomb3.data E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.tomb3.data, PID: 15837 java.lang.NoClassDefFoundError: Failed resolution of: Ljunit/runner/Version; at com.example.tomb3.data.mySql.getData(mySql.java:37) at com.example.tomb3.data.MainActivity.onCreate(MainActivity.java:14) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.ClassNotFoundException: Didn't find class "junit.runner.Version" on path: DexPathList[[zip file "/data/app/com.example.tomb3.data-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.tomb3.data-1/lib/x86, /vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) at com.example.tomb3.data.mySql.getData(mySql.java:37)  at com.example.tomb3.data.MainActivity.onCreate(MainActivity.java:14)  at android.app.Activity.performCreate(Activity.java:6237)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  Suppressed: java.lang.ClassNotFoundException: junit.runner.Version at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) at java.lang.ClassLoader.loadClass(ClassLoader.java:504) ... 15 more Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

public class mySql {
    String url;
    String user;
    String password;


    public mySql(String url, String user, String password){
        this.url=url;
        this.user=user;
        this.password=password;
    }
    public String getData(String table){
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        String output="";
        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery(table);
            if (rs.next()) {
                output += " " + (rs.getString(1));
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(Version.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
        return output;
    }
}
2
Please share the error message, stack trace etc. What did you try by way of debugging? Debugging is one of the best ways of learning a language, it's philosophy and way of doing things.MikeC
Post (add to the question) your LogcatPM 77-1

2 Answers

2
votes

just for reference

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Main {
  public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";

    Class.forName(driver);
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", "user", "password");

    Statement st = con.createStatement();
    int val = st.executeUpdate("INSERT employee VALUES(" + 13 + "," + "'Aman'" + ")");
    System.out.println("1 row affected");

  }
}
1
votes

EXCEPTION: main Process: com.example.tomb3.data, PID: 15837 java.lang.NoClassDefFoundError: Failed resolution of: Ljunit/runner/Version;

Make sure you included the junit-xx.jar in your classpath