0

i am creating simple crud system using android with mysql. but when I tried to add the data I got the errors. i attached image below. mysql driver i have put into the lib folder but i got the error.i attached the error below. i don't if i write code correct or not inorder to connect mysql and android if there is any thing wrong pls help me to correct

W/System.err: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.lang.Class.classForName(Native Method)
W/System.err:     at java.lang.Class.forName(Class.java:217)
        at java.lang.Class.forName(Class.java:172)
W/System.err:     at com.example.kobinath.mylcrud.MainActivity$Send.doInBackground(MainActivity.java:61)
        at com.example.kobinath.mylcrud.MainActivity$Send.doInBackground(MainActivity.java:41)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
    Caused by: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver

MainActivity.java

  public class MainActivity extends AppCompatActivity {
            EditText ed1,ed2;
            Button b1,b2;
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                ed1 = findViewById(R.id.title);
                ed2 = findViewById(R.id.course);
                b1 = findViewById(R.id.btn1);
                b2 = findViewById(R.id.v1);

                b1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Send objsend = new Send();
                        objsend.execute("");
                    }
                });
        }

            private  class Send extends AsyncTask<String, String,String> {

                String title = ed1.getText().toString();
                String description = ed2.getText().toString();
                @Override
                protected String doInBackground(String... strings) {
                    String url = "jdbc:mysql//192.168.84.2/sdl";
                    String user = "root";
                    String pass = "";
                    Connection conn = null;
                    String ConnURL;
                    {
                        try {
                            Class.forName("com.mysql.jdbc.Driver");
                            Connection con = DriverManager.getConnection(url, user, pass);
                            if(con == null)
                            {
                                Toast.makeText(getApplicationContext(),"fail",Toast.LENGTH_LONG).show();
                            }
                            else
                            {
                                PreparedStatement pst;
                                pst = conn.prepareStatement("insert into records(course,fee)values(?,?)");
                                pst.setString(1, title);
                                pst.setString(2, description);
                                pst.executeUpdate();
                                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
                                ed1.setText("");
                                ed2.setText("");
                                ed1.requestFocus();
                            }

                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }

                    }

                    return url;
                }

build.gradle

 dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'


        implementation('libs/mysql-connector-java-5.1.47-bin.jar')


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
  • Possible duplicate of [Android JDBC not working: ClassNotFoundException on driver](https://stackoverflow.com/questions/7221620/android-jdbc-not-working-classnotfoundexception-on-driver) – Giddy Naya Aug 12 '19 at 04:04
  • sir is that code is correct sir – nishan kishan Aug 12 '19 at 04:05
  • Looks like. Just make sure you are referencing the library `mysql-connector-java` in your apps `build.gradle`. Just like [this reply](https://stackoverflow.com/a/50999378/8043806) – Giddy Naya Aug 12 '19 at 04:14
  • yes how to do it build.gradle mysql-connector-java-5.1.47 – nishan kishan Aug 12 '19 at 04:18
  • i don't know how to referencing sir mysql-connector-java-5.1.47 – nishan kishan Aug 12 '19 at 04:19
  • paste [this reply](https://stackoverflow.com/a/50999378/8043806) in your apps build.gradle. Just change `compile` to `implementation` – Giddy Naya Aug 12 '19 at 04:23
  • compile files('libs/mysql-connector-java-5.1.47-bin.jar') i wrote like this but gradle failed – nishan kishan Aug 12 '19 at 04:30
  • implementation('libs/mysql-connector-java-5.1.47-bin.jar') like this but error displayed after complied gradle failed – nishan kishan Aug 12 '19 at 04:51
  • `implementation group: 'mysql', name: 'mysql-connector-java', version: '5.1.44'` – Giddy Naya Aug 12 '19 at 04:52
  • implementation group: 'mysql', name: 'mysql-connector-java', version: '5.1.44' – nishan kishan Aug 12 '19 at 05:02
  • You're using gradle, there's no need to download the dependency yourself. Double check the project page for `mysql-connector-java`, as they not only have it stored online (and probably even have a snippet specifically for gradle), but the main class for it (which you called with `Class#forName` in order to statically initialize), has changed over time. – Rogue Aug 12 '19 at 05:24

0 Answers0