0

I have added mysql connector.jar file as well, in my project, and I have tried out every possible thing to connect my database with android application. Whenever I click on submit button my application stops running. I have debugged my progam and I am continously receiving null in mystat and mycon. Please help me out!

public class Background extends AsyncTask {

    Connection mycon ;
    Statement mystat;
    ResultSet rs ;
    String db = " student";
    String pass = "wardakhan11";
    String dburl = "jdbc:mysql://localhost:3306/demo";
    String user = "root";

    @Override
    protected Object doInBackground(Object[] objects) {

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            mycon = DriverManager.getConnection(dburl,user,pass);
            mystat = (Statement) mycon.createStatement();

            System.out.println("inserting");



        } catch (Exception e) {
            e.printStackTrace();
        }


        try {
            int rowsaffect;
            mystat.executeUpdate("insert into student" + "(name , rollno) " +
                    "values " + "('sammi' ,ce12145)" );

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

        return null;
    }

this is my other class:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        EditText email = (EditText) findViewById(R.id.email);
        EditText password = (EditText) findViewById(R.id.password);
        Button submit = (Button) findViewById(R.id.login_submit_button);
        ImageButton fornback = (ImageButton)findViewById(R.id.registration_button);

        System.out.println("Before");
        final Background obj1 = new Background();
String s = " abcbcnccdc";


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    obj1.execute(this);
               // obj1.doInBackground(this);
               // obj1.OnpostExecute();

            }
        });
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Are you sure you are running a mysql server on your android device? – Shadow Sep 14 '19 at 17:50
  • Please read: [JDBC vs Web Service for Android](https://stackoverflow.com/q/15853367/295004) – Morrison Chang Sep 14 '19 at 18:35
  • @Shadow iam just a beginner and learning how to develop android application . iam stuck in connecting mysql server with my code . iam continously getting mycon and mystat as null . I have already assigned them with what they are required. please help me out – Warda Khan Sep 15 '19 at 08:07
  • yes iam running it on my android device. – Warda Khan Sep 15 '19 at 08:11

0 Answers0