Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I have tried to connect my login android application to MSSQL server 2012 r2 but fail to connect with it. I request you all in helping me out of this and give the best solutions to connect the app with MSSQL server.
I have also tried parallelcodes.com but it was not working.
Pls help as soon as possible.

Thanks,

Servesh Tiwari
Posted
Comments
Suvendu Shekhar Giri 14-Oct-15 6:30am    
How you were trying to connect your android app to SQL Server?
Through any webservice/wcf?
Share the relevant code, you have tried .
Servesh Tiwari 14-Oct-15 6:40am    
I want to connect with my local database. pls see the relevent code below..


Connect Android to MS SQL Database.

package app.mysqlapp;

package app.mysqlapp;

import android.annotation.SuppressLint;

import android.content.Intent;

import android.os.Bundle;

import android.os.StrictMode;

import android.support.v7.app.ActionBarActivity;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Login extends ActionBarActivity {

Button loginbtn;

TextView errorlbl;

EditText edname, edpassword;

Connection connect;

PreparedStatement preparedStatement;

Statement st;

String ipaddress, db, username, password;

@SuppressLint("NewApi")

private Connection ConnectionHelper(String user, String password,

String database, String server) {

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()

.permitAll().build();

StrictMode.setThreadPolicy(policy);

Connection connection = null;

String ConnectionURL = null;

try {

Class.forName("net.sourceforge.jtds.jdbc.Driver");

+ "databaseName=" + database + ";user=" + user

+ ";password=" + password + ";";

connection = DriverManager.getConnection(ConnectionURL);

} catch (SQLException se) {

Log.e("ERRO", se.getMessage());

} catch (ClassNotFoundException e) {

Log.e("ERRO", e.getMessage());

} catch (Exception e) {

Log.e("ERRO", e.getMessage());

}

return connection;

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.login);

loginbtn = (Button) findViewById(R.id.btnlogin);

errorlbl = (TextView) findViewById(R.id.lblerror);

edname = (EditText) findViewById(R.id.txtname);

edpassword = (EditText) findViewById(R.id.txtpassword);

ipaddress = "192.168.0.14";

db = "mydatabase";

username = "hitesh";

password = "789";

connect = ConnectionHelper(username, password, db, ipaddress);

loginbtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

try {

connect = ConnectionHelper(username, password, db, ipaddress);

st = connect.createStatement();

ResultSet rs = st.executeQuery("select * from login where userid='" + edname.getText().toString() + "' and password='" + edpassword.getText().toString() + "'");

if (rs != null && rs.next()) {

Intent i = new Intent(Login.this, MainActivity.class);

startActivity(i);

} else {

errorlbl.setText("Sorry, wrong credentials!!!");

}

} catch (SQLException e) {

errorlbl.setText(e.getMessage().toString());

}

}

});

}

}

Connect Android to MS SQL Database.

package app.mysqlapp;

package app.mysqlapp;

import android.annotation.SuppressLint;

import android.content.Intent;

import android.os.Bundle;

import android.os.StrictMode;

import android.support.v7.app.ActionBarActivity;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Login extends ActionBarActivity {

Button loginbtn;

TextView errorlbl;

EditText edname, edpassword;

Connection connect;

PreparedStatement preparedStatement;

Statement st;

String ipaddress, db, username, password;

@SuppressLint("NewApi")

private Connection ConnectionHelper(String user, String password,

String database, String server) {

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()

.permitAll().build();

StrictMode.setThreadPolicy(policy);

Connection connection = null;

String ConnectionURL = null;

try {

Class.forName("net.sourceforge.jtds.jdbc.Driver");

+ "databaseName=" + database + ";user=" + user

+ ";password=" + password + ";";

connection = DriverManager.getConnection(ConnectionURL);

} catch (SQLException se) {

Log.e("ERRO", se.getMessage());

} catch (ClassNotFoundException e) {

Log.e("ERRO", e.getMessage());

} catch (Exception e) {

Log.e("ERRO", e.getMessage());

}

return connection;

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.login);

loginbtn = (Button) findViewById(R.id.btnlogin);

errorlbl = (TextView) findViewById(R.id.lblerror);

edname = (EditText) findViewById(R.id.txtname);

edpassword = (EditText) findViewById(R.id.txtpassword);

ipaddress = "192.168.0.14";

db = "mydatabase";

username = "hitesh";

password = "789";

connect = ConnectionHelper(username, password, db, ipaddress);

loginbtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

try {

connect = ConnectionHelper(username, password, db, ipaddress);

st = connect.createStatement();

ResultSet rs = st.executeQuery("select * from login where userid='" + edname.getText().toString() + "' and password='" + edpassword.getText().toString() + "'");

if (rs != null && rs.next()) {

Intent i = new Intent(Login.this, MainActivity.class);

startActivity(i);

} else {

errorlbl.setText("Sorry, wrong credentials!!!");

}

} catch (SQLException e) {

errorlbl.setText(e.getMessage().toString());

}

}

});

}

}
 
Share this answer
 
Above code is working fine Please use this code and if any problem persists feel free to contact me. my email address is servesh20@gmail.com.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900