Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is my layout.xml

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

      </textview></textview></textview></textview></textview></linearlayout>



this is my activitymain.java

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.
Posted
Updated 23-Aug-14 1:39am
v2
Comments
Richard MacCutchan 23-Aug-14 7:42am    
You need to do two things at minimum. Add code to check the results of all your calls to the database. Use your debugger to step through and check the values of your variables. Also, why do you add the userid and password twice?
dushyant99 23-Aug-14 7:51am    
bro i am using eclipse and i am working on an android app. the output is shown in the emulator.
Richard MacCutchan 23-Aug-14 7:53am    
But that has nothing to do with the suggestions I made to try and diagnose your problem. You cannot expect us to guess what results you are seeing.
[no name] 23-Aug-14 7:57am    
Looks to me that you have an extra space in your connection string
Sandeep Londhe 1-Sep-14 14:21pm    
for connecting android app to the mssql server you should use webservices direct connection is never recommended

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