The following error appears
E/MYSQL: Erro: java.lang.ClassNotFoundException: org.sqlite.JDBC.Driver
E/MYSQL: Erro: java.sql.SQLException: No suitable driver
E/MYSQL: Erro: java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.ResultSet java.sql.Statement.executeQuery(java.lang.String)' on a null object reference
E/MYSQL: Erro: java.lang.NullPointerException: Attempt to invoke interface method 'void java.sql.Connection.close()' on a null object reference
Connection class package com.example.jonathan.agendaonline;
import android.app.Activity; import android.util.Log;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.sql.Driver;
/ ** * Created by Jonathan on 10/26/2016. * /
public class BankConnection { private static Connection conn = null; private static Statement st; private static ResultSet rs; private String sql;
public static void conectarMySQL(String host, String porta, String banco, String usuario, String senha) {
try {
Class.forName("org.sqlite.JDBC.Driver");
} catch (Exception erro) {
Log.e("MYSQL", "Erro: " + erro);
}
try {
conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + porta + "/" + banco, usuario, senha);
Log.i("MYSQL", "Conectado.");
} catch (Exception erro) {
Log.e("MYSQL", "Erro: " + erro);
}
}
public static void desconectarMySQL() {
try {
conn.close();
Log.i("MYSQL", "Desconectado.");
} catch (Exception erro) {
Log.e("MYSQL", "Erro: " + erro);
}
}
public static void queryMySQL(String sql) {
try {
if (conn == null || st == null)
st.executeQuery(sql);
Log.i("MYSQL", "Resultado: " + rs.getString("nome"));
} catch (Exception erro) {
Log.e("MYSQL", "Erro: " + erro);
}
}
public ResultSet getData(String sql) throws Exception {
if (conn == null || st == null)
conectarMySQL("172.0.0.1", "64533", "AgendaColetiva", "root", "root");
return st.executeQuery(sql);
}
}
Class that uses the bank
package com.example.jonathan.agendaonline;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class CriarUsuario extends AppCompatActivity {
TextView nome,senha,email;
String nomeS,senhaS,emailS;
Button criar;
Usuario usuario;
int tipo = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_criar_usuario);
nome = (EditText) findViewById(R.id.nome);
senha = (EditText) findViewById(R.id.senha);
email = (EditText) findViewById(R.id.email);
criar = (Button) findViewById(R.id.Criar);
criar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nomeS = nome.getText().toString();
senhaS = senha.getText().toString();
emailS = email.getText().toString();
try {
BancoConexao.conectarMySQL("172.0.0.1", "64533", "AgendaColetiva", null, null);
String sql = String.format("INSERT INTO Usuarios ("
+ nomeS
+ senhaS
+ emailS
+tipo
+ ") VALUES(?,?,?,?)");
BancoConexao.queryMySQL(sql);
Toast.makeText(getApplicationContext(),"Cadastro bem sussedido", Toast.LENGTH_LONG).show();
BancoConexao.desconectarMySQL();
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}