-1

this is my layout.xml

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t1" />

   <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t2"
    />

    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t3"
    />

    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t4"
    />

    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t5"
    />

    <Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn2"
    />

  </LinearLayout>

this is my activitymain.java

package com.example.mssqlconnect;

import android.os.Bundle;   
import java.sql.*;
import net.sourceforge.jtds.jdbc.*;
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.TextView;
import java.sql.*;
import net.sourceforge.jtds.jdbc.*;
public class MainActivity extends Activity {

public class A implements OnClickListener {

    @Override
    public void onClick(View arg0) {
        TextView tv2=(TextView)findViewById(R.id.t2);
        TextView tv3=(TextView)findViewById(R.id.t3);
        TextView tv4=(TextView)findViewById(R.id.t4);
        TextView tv5=(TextView)findViewById(R.id.t5);

        tv2.setText("Connected");
        Log.i("Android"," MySQL Connect Example.");
        Connection conn = null;
        try {
        String driver = "net.sourceforge.jtds.jdbc.Driver";
        Class.forName(driver).newInstance();
        tv4.setText("2111");
        //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
        String connString = "jdbc:jtds:sqlserver://sony-VAIO/sony :1433/androiddata;encrypt=false;user=0cool;password=chevron;instance=SQLEXPRESS;";
        //String connString ="jdbc:jtds:sqlserver://(local);instance=MSSQLSERVER";
        String username = "0cool";
        String password = "chevron";
        tv4.setText("222222222");
        try{
        conn = DriverManager.getConnection(connString,username,password);
        //tv5.setText("2423424");



        Log.w("Connection","open");
        Statement stmt = conn.createStatement();
        ResultSet reset = stmt.executeQuery("select * from android_data");

           //Print the data to the console
           while(reset.next()){
            Log.w("Data:",reset.getString(1));
//                        Log.w("Data",reset.getString(2));
            //tv.setText(reset.getString(2));
            }
            conn.close();
            }
            catch(SQLException e)
             {
                tv5.setText(e.getMessage());
             }

        } catch (Exception e)
        {
                    //Log.w("Error connection","" + e.getMessage());
                    TextView tv1=(TextView)findViewById(R.id.t1);
                    tv1.setText(e.getMessage());
        }
        }


}

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

    Button btn=(Button)findViewById(R.id.btn2);
    btn.setOnClickListener(new A());
}

@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;
}

}

I am not getting any exception or any error in this code on running this in emulator help me pls m on a schedule. If there is an alternative for manipukating data in the database table then please guide me i have tried to access through json too but then again I was not getting any data back.

pawar
  • 70
  • 2
  • 11

2 Answers2

1

Don't connect directly to database from your app (its like holding a welcome board for hackers). Use a webservice.

bhargavg
  • 1,383
  • 9
  • 12
  • +1 you should never connect to database in app - It should be done on the server side. – Yuraj Aug 23 '14 at 11:00
  • when i use the webservice and run the webservice in vs 2010 and then run the application in android still i am not getting any data. and my android emulator stops. – pawar Aug 23 '14 at 11:14
0

Try to run some webservice calls outside of android to verify the service is running correctly. Also if the webservice is sitting on a local machine I would verify that there aren't any firewall/port issues to resolve.

zba
  • 103
  • 1
  • 2
  • 7
  • And it's a production webservice or a webservice that you've verified serves data? – zba Aug 23 '14 at 12:05