Unable to connect Android App to local MSSQL database - android

Hi I created a SQL management Database, I wish to connect to the Database through Android App
I am not what I did wrong, I am not able to login.
This my SQL management Studio 2012, My DataBase name I call it DB.
This is my SQL Server Configuration manager, what I did is I enable TCP/IP and shared memory.
And then I enable all the IP, and I set the ALL port to 1433, the dynamic port I leave it empty.
I create the database using this username
This is what my code is like for my Android Java file, connection to database
String driver = "net.sourceforge.jtds.jdbc.Driver";
String connString = "jdbc:jtds:sqlserver://127.0.0.1:1433/db;encrypt=false;
user=username;password=1234;instance=SQLEXPRESS;";
String username = "username";
String password = "1234";
int id;
Is it something wrong with my step, how come it is not connecting?

Connecting directly to a DB from android is highly discouraging. The primary reason is performance. See, it is really expensive to keep a remote connection alive than rather just call Web Services on demand which is more portable and also more secure.
Therefore, its better to develop a REST API instead for your service and invoke it from client android app or any other client for that matter.
Edit: For your current issue at hand, see if it works by replacing localhost IP to the real(assuming 192.168.0.16 is also ipa4 ip?) one.
String connString = "jdbc:jtds:sqlserver://192.168.0.16/db;encrypt=false;
user=username;password=1234;instance=SQLEXPRESS;";

Related

Using physical device with Android Studio to send data to SQL Server

I have created an Android App with the Android Studio IDE. I have done all the codes, everything seems to be ok, but I get error every time I try to send to SQL Server. I know I am very close to figuring it out, and I know it has something to do with the IP address. I have enabled all the Protocols for SQL Server.
I've disabled the Windows Firewall.
Anti-virus is not the issue.
I am very confused on which IP Address to use for communication to work properly. Both Computer and Tablet are connected to my iPhone Hotspot network.
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
String username = "sa";
String password = "patrickdorian";
String gender = "";
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.137.166:1433/Android DB;"+"encrypt=false;user="+username+";password"+password);
[Here][1]
Here is the error when I try to send the data to SQL Server[1]: https://i.stack.imgur.com/G3hID.jpg

JDBC can't connect to azure server for some WiFis: All IPs permitted

I want to connect to my Azure DB from Android. From Android side im using JDBC:
Connection con = ConnectionJDBC.connectionClass();
String query = "SELECT * FROM mytable";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);
From azure side, my server is normally up and running and the firewall settings allow IPs 0.0.0.0 to 255.255.255.255. Everything else is unchanged.
That also works for most WiFi networks over which I made the request. However, it doesn't work for my university's WiFi (specifically ETH Zurich), con will just be null. Does someone know what the problem could be? I think it lies in the configuration of the Azure Server but I don't know what else I could change.
Please see JDBC vs Web Service for Android, as suggested by Morrison Chang: JDBC should not be used and is sometimes blocked. Instead create a mobile app service in Azure (they will change the name to web app service), and connect to that server, as suggested here: https://learn.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-android-get-started

Connecting an Android application to sql server through sql server express

I've been looking for a way to connect an Android application to a SQL server. Tried different codes and asked many programmers. Didn't get anywhere. It seems that the problem is in the connection url.
The application I got to make involves creating some sql tables. For instance, at the beggining it needs to veriffy login data given by a user against that data in one of those tables.
In addition to Android studio, I was guided to install and use SQL server express (nothing else).
Using SQL server express 2008 R2 I created a database on my computer and a table. The connection to the server didn't work. The codes I found always involved a password and a username for the connection. Is that mandatory? I didn't get a username for the connection when I installed the server. It involved getting an instance name and a password.
I searched for information on the connection url's parameters in order to fix the url I got. Couldn't find what I need to change.
I found a code thoguh which one can define a password and a usename:
private Connection conn;
public MySQLConnection() throws SQLException {
System.out.println("At MySQLConnection()");
String driverName = "com.mysql.jdbc.Driver";
try {
Class.forName(driverName);
Properties props = new Properties();
props.setProperty("user","fred");
props.setProperty("password","secret");
props.setProperty("ssl","true");
String url = "jdbc:mysql://zivpc/workers?user=fred&password=secret&ssl=true";
Connection conn = DriverManager.getConnection(url);
this.conn = DriverManager.getConnection(url);
System.out.println("connected");
} catch (ClassNotFoundException e) {
System.out.println(e);
}
Trying to connect results in the following exception: CommunicationsException: Communications link failure.
Thanks!
You will need an API (REST, etc.) running on a web server, with that web server having access to the firewalled-off database server. And authentication for your API so that you don't have random people DDoSing your service and database.
Your database server should not be exposed to the internet at all, nor directly accessible from your client. Nor should you be sending SQL queries directly from the client through to the database server. Your API exposes a set of functionality and is very restrictive about what the client can request and receive.
Mobile app -> internet -> web server -> firewall -> SQL Server
The details of "how do I implement this" far exceed the scope of Stack Overflow. There are numerous tutorials and walkthroughs available online. Choose wisely.
Additional note: You say you're using SQL Server 2008R2 Express Edition. Be advised that this version of SQL Server will be EOL in less than three months. No one should have been doing new development on it for the past 5 years. Be careful taking advice from whoever "guided" you to install this version in 2019.

Connect Android App with My SQL database stored on the local machine

What are the ways in which I could accomplish the above?
The current one I employ uses a JDBC Connector with the app.
My question is?
In the following steps:
static final String DB_URL = "jdbc:mysql://localhost:3306/mess";
static final String USER = "root";
static final String PASS = "root";
public Connection connection_open(){
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("\n Connection inside DB = OK");
}
catch(SQLException se){
System.out.println("\n Connection inside DB = failed");
se.printStackTrace();
}
finally {
if(conn==null)System.out.println("\n Have to return NULL");
return conn;
}
}
This always outputs:
Connection inside DB = failed
Have to return NULL
So, in the localhost section of the DB URL, do I have to write the IP of my android phone, which I use to debug?
Is it even possible, that my app connects remotely (assuming I have granted privileges to all IP's)
Please help :)
This won't work. First of all, this is wrong:
static final String DB_URL = "jdbc:mysql://localhost:3306/mess";
The localhost will be your android phone and it doesn't have any MySQL database in it.
You should plan a webservice that takes care of all requests to your DB and send the results back to your app.
To access/connect to your webservice, you could use HttpURLConnection.
What do you wish to achieve?
Your android app should save and upload data to a central server location?
- If answer is yes, your options are Parse ( which you already tried i guess ) or build your own rest server api quickly. My personal fav is python rest api server on Google App Engine. https://cloud.google.com/appengine/docs/python/
If your android app should save data on the android device and need not communicate to any server then create local instance of sqlite database and store retrieve data from it.
Try this out - http://satyan.github.io/sugar/
There are possibilities that you can run mysql database in android .But for that you have to cross compile the mysql source code for arm architecture becoz mobile phones have arm architecture
You can use android ndk or arm gcc compiler for cross compilation. Once you copiled your libs it will generate the .so files (extension in linux ) .You have to add these libs to your project. Then using jni you have to write a c program that will use the mysql functions. like crud operation then using jni then you have to call that native c program from your java code . this way you can do this. I did this once. But it is a very tedious and cumbersome task.

Android Studio with SQL Server 2012

I need to make a generic app which can be used by anyone.
App will connect directly to MS SQL server, which will be within intranet/wifi
I donot need to worry about security risk,
I donot want to use web service or anything which we need to do something on server.
I am new and I have tried every option but still can't connect to sql from android studio.
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
String url = "jdbc:jtds:sqlserver://192.168.0.7/MYDB;user=android;password=abcd;";
Connection DbConn = DriverManager.getConnection(url);
Please suggest the easiest way to connect, as I said it will be used by different people on different server
I need to connect directly with Dynamics NAV database without any middle tier
Can we connect directly to SQL Server from Android Studio?
Thanks for your comments
I found this and worked
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Categories

Resources