1

can someone help me ? i alwayse get this kind exception like the picture bellow when i click login button :

enter image description here

this code for MainActivity:

package com.TPK.SistemPendataanPelalatan;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MainActivity extends Activity {

    TextView ket;
    EditText id_user,pass;
    Button btnlogin;



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

        //deklarasi input dan output

        ket =(TextView) findViewById(R.id.warning_login);
        id_user = (EditText) findViewById(R.id.singup_id);
        pass = (EditText) findViewById(R.id.input_pass);
        btnlogin = (Button) findViewById(R.id.login);


    // event tombol ditekan
    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String id=id_user.getText().toString();
            String pw=pass.getText().toString();

            int salah=0;//hitung jumlah kesalahan
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {

                Statement st=(Statement)hubung.GetConnection().createStatement();

                if(id_user.getText().toString().isEmpty()||pass.getText().toString().isEmpty()){
                    ket.setText("ID dan Password Harus di Isi!");
                }else{
                ResultSet rs=st.executeQuery("select * from user where id='"+id+"'and password='"+pw+"'");
                if(rs.next()){
                    //kalo sukses masuk
                    ket.setText("Selamat Datang ...");
                    String nama=rs.getString(1);
                    if(rs.getString(5)=="1"){//ADMIN
                        Intent i = new Intent(MainActivity.this, activity_user_admin.class);
                        i.putExtra("nama", nama);//passing nama ke activity user admin
                        startActivity(i);
                        st.close();
                    }else if(rs.getString(5)=="2"){//SUPERVISOR
                        Intent i = new Intent(MainActivity.this, activity_user_super.class);
                        i.putExtra("nama", nama);//passing nama ke activity user super
                        startActivity(i);
                        st.close();
                    }else if(rs.getString(5)=="3"){//TEKNISI
                        Intent i = new Intent(MainActivity.this, activity_user_teknisi.class);
                        i.putExtra("nama", nama);//passing nama ke activity user teknisi
                        startActivity(i);
                        st.close();
                    }
                }
                else {
                    ket.setText("ID atau Password Salah !");
                    salah++;
                    if(salah>3){
                        salah=0;
                        ket.setText("Silahkan Lapor Admin");
                    }

                }
                }

            } catch (SQLException e) {
                // TODO Auto-generated catch block
                ket.setText(e.toString());
            }   
        }
    });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

this where i call jdbc to connect to mysql with name hubung :

package com.TPK.SistemPendataanPelalatan;
import java.sql.*;

public class hubung {
    private static Connection kon;
    public static Connection GetConnection() throws SQLException{
      if(kon==null){
        kon = DriverManager.getConnection("jdbc:mysql://10.11.1.232:3306/tpk_peralatan","root","");  
      }
      return kon;

    }
}

thanks ...

NB : this program can build and run well but stuck in mysql connection ...

W4R10CK
  • 5,502
  • 2
  • 19
  • 30

1 Answers1

0

Hi Stevanus, What Morrison and ELITE are saying is right. I Assume that you are connecting to remote mysql server(What I mean is, Mysql server is not running in your android device). There are two ways to connect

  • Using Web Services(SOAP or REST)
  • Using mysql-connector.jar

Using Web Services: This is recommended way to connect to any database server. Here you will connecting to database in two steps.

  1. Using web service you will hit the server side application
  2. Application will take care of communicating with database server and getting the appropriate response and sending back the response to device.

Using Myql-connector.jar

This approach ok,if you are just playing with database connections from android device.Remember below points while connecting in this way 1.First you have to create a user in mysql server,by mentioning your device IP as you will be accessing the Mysql Server from remote machine,it needs remote machine IP which is already available in the its user table. 2.mysql-connector jar under classpath.

Refer these links.

1.http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

2.https://www.quora.com/How-can-I-connect-send-and-retrieve-data-between-an-Android-device-and-a-remote-PC-with-a-PHP-interface-when-I-use-MySQL-as-my-database

3.https://www.youtube.com/watch?v=VM9wW3W22IE

Regads, Ajay