0

At the very first I would like to know that "Is it possible to connect android application to SQL Server??"

If yes, then I am facing some problem with it. I have added jtds-1.2.5.jar

Here is my MainActivity:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText e1;
Button b1;
TextView t1;

String str = null;
String conUrl = null;
String uname,pass;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    e1 = (EditText)findViewById(R.id.editText1);
    b1 = (Button)findViewById(R.id.button1);
    t1 = (TextView)findViewById(R.id.textView1);

    str = "SELECT [Name] from user_mast where username = 'a'";

    abc();

    b1.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            //querySQL(str);
            Toast.makeText(getBaseContext(), "In Click", Toast.LENGTH_SHORT).show();
        }
    });
}

void abc()
{
    try{
        Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
        uname = "sa";
        pass = "123";

    ///////// This is the problem statement It is not get executed..
        Connection connect = DriverManager.getConnection("jdbc:jtds:sqlserver://server:1433/AlphaHotel;user="+ uname +";password=" + pass);

        Statement statement=connect.createStatement();
        ResultSet rs=statement.executeQuery(str);

        while(rs.next()){
        t1.setText(rs.getString(0));
        }
        connect.close();
        }catch (Exception e){
            e.printStackTrace();
        }
}
 @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
   }

}

Here is the error Logcat : (Error might be there on first two line only)

12-31 06:40:33.884: W/IInputConnectionWrapper(2376): showStatusIcon on inactive InputConnection
12-31 06:40:34.973: W/System.err(2376): android.os.NetworkOnMainThreadException
12-31 06:40:34.973: W/System.err(2376):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-31 06:40:34.973: W/System.err(2376):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-31 06:40:34.983: W/System.err(2376):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-31 06:40:34.983: W/System.err(2376):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-31 06:40:35.033: W/System.err(2376):     at java.net.Socket.tryAllAddresses(Socket.java:108)
12-31 06:40:35.033: W/System.err(2376):     at java.net.Socket.<init>(Socket.java:177)
12-31 06:40:35.033: W/System.err(2376):     at java.net.Socket.<init>(Socket.java:149)
12-31 06:40:35.033: W/System.err(2376):     at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:259)
12-31 06:40:35.033: W/System.err(2376):     at  net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:311)
12-31 06:40:35.063: W/System.err(2376):     at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:187)
12-31 06:40:35.063: W/System.err(2376):     at java.sql.DriverManager.getConnection(DriverManager.java:175)
12-31 06:40:35.063: W/System.err(2376):     at java.sql.DriverManager.getConnection(DriverManager.java:140)
12-31 06:40:35.063: W/System.err(2376):     at com.example.z.MainActivity.abc(MainActivity.java:61)
12-31 06:40:35.094: W/System.err(2376):     at  com.example.z.MainActivity.onCreate(MainActivity.java:38)
12-31 06:40:35.094: W/System.err(2376):     at android.app.Activity.performCreate(Activity.java:5104)
12-31 06:40:35.094: W/System.err(2376):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-31 06:40:35.094: W/System.err(2376):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-31 06:40:35.094: W/System.err(2376):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-31 06:40:35.094: W/System.err(2376):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-31 06:40:35.103: W/System.err(2376):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
 12-31 06:40:35.103: W/System.err(2376):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 06:40:35.103: W/System.err(2376):     at android.os.Looper.loop(Looper.java:137)
12-31 06:40:35.103: W/System.err(2376):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-31 06:40:35.113: W/System.err(2376):     at java.lang.reflect.Method.invokeNative(Native Method)
12-31 06:40:35.113: W/System.err(2376):     at java.lang.reflect.Method.invoke(Method.java:511)
12-31 06:40:35.113: W/System.err(2376):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-31 06:40:35.153: W/System.err(2376):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-31 06:40:35.153: W/System.err(2376):     at dalvik.system.NativeStart.main(Native Method)
12-31 06:40:35.323: I/Choreographer(2376): Skipped 35 frames!  The application may be doing too much work on its main thread.`

This is my Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.z"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.z.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Kindly check out all the code and tell me

What should I add.

What might be the problem is.

Thank you.

Lalit
  • 1,713
  • 1
  • 11
  • 19
  • may be you get some answers from [link](http://stackoverflow.com/questions/18670320/connection-string-formation-for-android-to-sqlserver-connectivity) – Shaleen Dec 31 '13 at 06:53
  • Hey thxxxx Shaleen your link is very much GOOD. It helps me a lot. @Shaleen – Lalit Dec 31 '13 at 12:21

2 Answers2

4

Try adding this to your onCreate method:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 

And don't forget your internet permission in your AndroidManifest file!

<uses-permission android:name="android.permission.INTERNET"/>
Rohodude
  • 495
  • 2
  • 5
  • 18
0

I am not sure about Android connection with SQL Server. But the error clearly says :

android.os.NetworkOnMainThreadException

It means you are trying to create network connection on Main Thread which is not permitted on Android 4.0 or later.

Try to create network connection in background thread or in AsyncTask.

Read : http://developer.android.com/reference/android/os/AsyncTask.html

Brijesh Thakur
  • 6,768
  • 4
  • 24
  • 29
  • So please tell me how to do that sir. @Brijesh Thakur – Lalit Dec 31 '13 at 07:02
  • Ok but how to do AsyncTask? I dont know that tell me about it. @Brijesh Thakur – Lalit Dec 31 '13 at 07:26
  • this thing looks weired. you should not access database server directly on android, it is not good practice. use REST Web services .. i will suggest http://restsql.org/doc/Concepts.html – Ajeet47 Dec 31 '13 at 09:13
  • Is Anyone Knows what this error means : `12-31 09:26:46.613: W/IInputConnectionWrapper(6383): showStatusIcon on inactive InputConnection` – Lalit Dec 31 '13 at 09:28