Android+mysql : Connection class returns a Null object
Android+mysql : Connection class returns a Null object
I am trying to connect my android login app to XAMP server(an Apache is running in local machine) through jtds jdbc. It seems the code fails to connect the server. Here's the code:
ConnectionClass.java
package com.ercess.databaseconnection;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.os.Bundle;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionClass
public String driver = "net.sourceforge.jtds.jdbc.Driver";
String url = "jdbc:mysql://localhost/events-test";
//String url = "jdbc:mysql://127.0.0.1/events-test";
public String un = "root";
public String password = "password";
public String db = "users";
@SuppressLint("NewApi")
public Connection CONN()
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
java.sql.Connection conn = null;
String ConnURL = null;
try
Class.forName(driver);
ConnURL = "jdbc:jtds:sqlserver://" + "127.0.0.1" +";databaseName="+ db + ";user=" + un+ ";password=" + password + ";";
conn = DriverManager.getConnection(ConnURL);
catch (SQLException se)
Log.e("ERROR", se.getMessage());
catch (ClassNotFoundException e)
Log.e("ERROR", e.getMessage());
catch(Exception e)
Log.e("ERROR", e.getMessage());
//Log.d("conn",conn.toString());
return conn;
MainActivity.java
package com.ercess.databaseconnection;
import android.app.ProgressDialog;
import android.os.AsyncTask;
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.Toast;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
public class MainActivity extends AppCompatActivity
EditText email, password;
Button login;
ProgressDialog progressDialog;
ConnectionClass connectionClass;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
login = findViewById(R.id.login);
connectionClass = new ConnectionClass();
progressDialog = new ProgressDialog(this);
login.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Dologin dologin = new Dologin();
dologin.execute();
);
public class Dologin extends AsyncTask<String, String, String>
String emailstring = email.getText().toString();
String passstring = password.getText().toString();
String z = "";
boolean isSuccess = false;
String em, pass;
@Override
protected void onPreExecute()
progressDialog.setMessage("Loading...");
progressDialog.show();
super.onPreExecute();
protected String doInBackground(String... params)
if(emailstring.trim().equals("")
@Override
protected void onPostExecute (String s)
Toast.makeText(getBaseContext(),""+z, Toast.LENGTH_LONG).show();
progressDialog.hide();
I am getting this output: "Please, check your internet connection...." .
This message should fire up only when con = null.
How to resolve?
jdbc:jtds:sqlserver://
Also i do not think you have an sqlserver/mysql installed at your smartphone
– Jens
Aug 23 at 11:40
You shouldn't create JDBC connections from Android. Use a rest service (or other web service) to mediate between your Android application and the database.
– Mark Rotteveel
Aug 23 at 11:40
@Jens I am using emulator. Not my phone.
– Debbie
Aug 23 at 11:43
@Debbie The emulator is also an own device which does not have a dbms installed
– Jens
Aug 23 at 11:46
1 Answer
1
Android does not support MySQL OR SQL Server : -
You can Use Sqllite
https://www.tutorialspoint.com/android/android_sqlite_database.htm
Or you can Access MySQL by PHP :-
https://www.tutorialspoint.com/android/android_php_mysql.htm
Android cannot connect to MySQL server but loads of android apps are successfully using MySQL database. I am using XAMPP which runs MySQL database on an Apache server.
– Debbie
Aug 24 at 10:25
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
jdbc:jtds:sqlserver://
is (MS)SQL-server not mysql– Jens
Aug 23 at 11:39