Connecting an Android application to sql server through sql server express - android

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.

Related

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

Unable to connect Android App to local MSSQL database

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;";

How to change file capabilities on Android? [duplicate]

Is it possible to host a web server within an Android phone itself?
Similar to Apache. I want to access the web server from the mobile
browser itself.
Couldn't find any reference online. Is this possible?
Thanks in advance
Short: yes. If you are looking at code, check out the org.apache.http package. If you are looking at a ready app, check iJetty and kWS.
KSWEB — a suite for web development on the Android platform. It consists of: a web server, PHP programming language and the database MySQL. KSWEB allows you to organize a platform for running and debugging Web applications (sites) in your Android device. Working with the application is very simple. To start the server it is enough to run our application, select, if necessary, port and the root directory.
By default, KSWEB contains a fully functional configuration file of the server, PHP and MySQL. However, if you want something to change them, go to the server options and click on the item «INI Files». Configuration files will be moved to SD-card of your device at «/mnt/sdcard/ksweb/ini/», if available. If repeatedly clicking on the item settings «INI Files» KSWEB will use the internal configuration files.
What's in the plans?
Review the requirements for the Android operating system, in particular
reduce the requirements for the version of Android to run the application to 2.0;
Add the ability to track the flow of errors arising when working with php and mysql;
Move the log files on the server and mysql on sdcard;
Extend the additional PHP libraries. For example, pdo_mysql;
Move the database files to MySQL on sdcard.
https://play.google.com/store/apps/details?id=ru.kslabs.ksweb
http://www.kswebserver.ru/
Atjeews android app server, small footprint, biggest advantage for me was jsp support.
I recently installed BitWeb which I recommend highly. I have a rooted phone with lends me a few more options (and I also recommend rooting if you haven't and plan on doing any server tinkering at all)
BitWeb is nice in that it includes a lighttpd server, a stripped down php engine and a stripped down mysql engine giving you a quick and dirty LAMP system with just one app. I use mine in conjunction with an sshd app to gain a busybox shell, I installed nano and use the ssh connection to do an sshfs share to edit content from my pc.
Bitweb includes the ability to tweak the config settings for all three services, and I've even managed to get multiple Zend servers working on just the one little server with a little tweaking to the re-write rules and the application.ini settings.
https://play.google.com/store/apps/details?id=com.andi.serverweb&hl=en
If you want to see some of the config options I had to customize, I can either post them or respond via email with what I set up as well as some comments on the pitfalls I ran into along the way. (I recently posted one as a self-answered thread on here by the way, although it turned out not to be specific to the android platform but with lighttpd/Zend)
I'm just bummed that t-mobile/HTC appears to have no plans to upgrade my phone beyond Gingerbread as I understand some of the new android OS's have ways you can access some of the hardware (such as the cameras) from scripts or compiled code without a fuss. If I want to do it now, I either need to hard code my own interface to them or get a pre-roll app capable of doing it which really tasks my phone when running side-by-side with the web/mysql/php services.
I might be a last to answer this question but believe me, this is the simplest and latest answer in 2020 for those who need to start a webserver in Android and need to serve multiple clients without implementing any other third-party libraries.
Copy your HTML file into the assets folder of Android as server.html and declare the serverSocket as global in your class.
Run the below code inside a new thread.
try {
serverSocket = new ServerSocket(8080);
while (true) {
final Socket socket = serverSocket.accept();
new Thread(new Runnable() {
#Override
public void run() {
try {
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream(), true);
String request = is.readLine();
Scanner scanner = new Scanner(getAssets().open("server.html"));
String response = scanner.useDelimiter("\\Z").next();
os.print("HTTP/1.0 200" + "\r\n");
os.print("Content type: text/html" + "\r\n");
os.print("Content length: " + response.length() + "\r\n");
os.print("\r\n");
os.print(response + "\r\n");
os.flush();
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}).start();
}
} catch (Exception ex) {
ex.printStackTrace();
}
That's all your native Android Java (no third-party libraries implemented) webserver is live at port 8080.
If you want to use DB and POST requests, try to implement SQLite and WebSocket connections to make two-way communication.
https://developer.android.com/reference/java/net/ServerSocket
Check KSWEB and Bit Web Server,
both using Lighttpd als server, php and support mysql.
i tried KSWEB, it works wonderfull, but i don't know yet how to use the modrewrite.
but, it should be work. you can try the trial version of KSWEB before purchase it.
KSWEB costs a little bit more than Bit Web Server only 0,XX $.
if you decided to buy it, post your experience here... i want to know too... :)
Bit Web Server
KSWEB
Till now I have seen all those applications are paid . And others doesn't have phpmyadmin. And I found finally one. You can have a look at kickweb server.
Android web server PHP/MySQL/PHPMyAdmin
Requirements
Internal memory should not be less than 50MB!
Minimum Android API 9 (GINGERBREAD)!
Features
Lighttpd 1.4.34
PHP 5.5.9
MySQL 5.1.62
MSMTP 1.4.31
phpMyAdmin 4.1.10
Nginx 1.5.11
Default Document Root (htdocs)
Path : /sdcard/htdocs/
Default URL
Address : localhost:8080
phpMyAdmin Informations
Address : localhost:10000
Username : root
Password :
MySQL Informations
Host : localhost
Port : 3306
Username : root
Password :
If you find trouble to make it working, you can see this video : https://youtu.be/3_m3vNGTp74
Yes, it is possible. May be not so complicated as Apache, but possible.
Look at Moto Phone Portal for example, it has web-server in its base. And there were several applications for this purpose in Market.
Servers Ultimate Pro, just 8 pounds.
Include PHPAdmin, MySQL and ton of other servers. I am currently using it as proxy.

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.

How to check username and password in a PostgreSQL server from Android?

I am using the Android 2.2 API. I want to know how to connect to a PostgreSQL database server.
I am new to this and so have no ideas, so please help me with some sample code.
You usually don't connect directly to a database.
To achieve the results you want, you can do a RESTful request.
This means, send either a JSON string or xml file to a server(which you can program in php, ruby and what not)
I'm not going to give you sample code cause there are literally tons of code on a simple google search "android restful login"
Example tutorial
The process is usually:
Enter login credentials on android device -> send post request to server -> validate credentials to the database(postgres in this case) -> send verification back -> android device act upon verification(usually by loading a new activity as a result if logged in successfully)

Categories

Resources