Note that there are some explanatory texts on larger screens.

plurals
  1. POConnect to SQL Server through android
    text
    copied!<p>At the very first <strong>I would like to know that <em>"Is it possible to connect android application to SQL Server??"</em></strong></p> <p>If yes, then I am facing some problem with it. <strong>I have added jtds-1.2.5.jar</strong></p> <p>Here is my <strong>MainActivity:</strong></p> <pre><code>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; } </code></pre> <p>}</p> <p>Here is the <strong>error Logcat :</strong> (<em>Error might be there on first two line only</em>)</p> <pre><code>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.&lt;init&gt;(Socket.java:177) 12-31 06:40:35.033: W/System.err(2376): at java.net.Socket.&lt;init&gt;(Socket.java:149) 12-31 06:40:35.033: W/System.err(2376): at net.sourceforge.jtds.jdbc.SharedSocket.&lt;init&gt;(SharedSocket.java:259) 12-31 06:40:35.033: W/System.err(2376): at net.sourceforge.jtds.jdbc.ConnectionJDBC2.&lt;init&gt;(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.` </code></pre> <p>This is my <strong>Manifest file:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.z" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.example.z.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; </code></pre> <p></p> <p>Kindly check out all the code and tell me </p> <p><strong>What should I add.</strong></p> <p><strong>What might be the problem is.</strong></p> <p>Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload