Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi everyone

I have been trying for over a week now to connect my android app to sql server database with no success. I tried restful didn't get me any where and now I am using the microsoft JDBC driver and still cannot connect.

The SQL Server is installed in the same machine.

Below is the class used to set the connection and access data:

package com.example.unisalabapp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import android.util.Log;

public class gaConnection {
	
	String url ="";
	Connection conn = null;
	Statement statement = null;
	ResultSet resultSet = null;
	
	public void setConnection(String DBName, String UserName, String Password){
		
		try{
			//Class.forName("net.sourceforce.jtds.jdbc.Driver").newInstance();
			DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
			
			//url = "jdbc:jtds:sqlserver:workstation id=UniLabAppDB.mssql.somee.com;packet size=4096;user id=lumambakd;pwd=Simamukele15;data source=UniLabAppDB.mssql.somee.com;persist security info=False;initial catalog=UniLabAppDB";
			//url = "jdbc:jtds:sqlserver://LUMAMKD\\MSSQLSERVER:1433;DatabaseName="+ DBName +";integratedSecurity=false;user=" +UserName+";password="+Password;
		    
			url = "jdbc:sqlserver://LUMAMKD\\MSSQLSERVER:1433;DatabaseName="+ DBName +";integratedSecurity=false;user=" +UserName+";password="+Password;
			
			conn =DriverManager.getConnection(url);
			
			if(conn != null){
				Log.w("Connection","open");
			}
		    
		 } catch (Exception e) {
			Log.e("Connection failed",e.getMessage());
			//e.printStackTrace();
		 }
	}
	
	public String showResult()
	{
		String strResult="";
		try
		{
			statement = conn.createStatement();
			resultSet = statement.executeQuery("SELECT StudentNr,Surname FROM Records");
			
			while (resultSet.next()){
				
				strResult = strResult + "StudentNr : " + resultSet.getString(0) + "Surname : "+ resultSet.getString(1) + "\n";
			}
			conn.close();
		}
		catch (Exception e) {
			Log.e("Couldn't collect data; No Connection",e.getMessage());
			  //e.printStackTrace();
		}
		return strResult;		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}


My main activity:

package com.example.unisalabapp;

import java.sql.Connection;
import android.app.Activity;
import android.os.Bundle;
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.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class trialDisplay extends Activity {

	EditText e;
	Button bt;
	ListView lv;
	Connection connect;
	SimpleAdapter sm;
	TextView tv, tv1;
	
	String DBName;
	String UserName;
	String Password;
	
	public void declare(){
		tv = (TextView)findViewById(R.id.textView3);
	    tv1 = (TextView) findViewById(R.id.textView1);
	    e = (EditText) findViewById(R.id.et1);
	    
	    DBName = "UniLabAppDB";
	    UserName = "sa";
	    Password = "Simamukele15";
	}
	
	 @Override
	public void onCreate(Bundle savedInstanceState){
		    super.onCreate(savedInstanceState);
		    setContentView(R.layout.trial);
		    
		  Button bt = (Button)findViewById(R.id.btn1); 
		  
		    bt.setOnClickListener(new OnClickListener() {
		       @Override
		       public void onClick(View v) {	  	   

		    	   declare();
				    
		    	   try{
				    	gaConnection con = new gaConnection();
				    	con.setConnection(DBName, UserName, Password);
				    	
				    	if(con != null){
				    		e.setText("Connection establised");				    	
				    	
				    		tv1.setText(con.showResult());				   
				    		
				    		tv.setText("result showing above");
				    	}				    		
				    }	
				    catch (Exception e){
				    	//e.printStackTrace();
				    	Log.e("No Connection to Server", e.getMessage());
				    	
				    	tv.setText(e.getMessage());
				    	tv1.setText("Error connecting");
				    }
		        }
		    });		    
	    }
		   
		@Override
		public boolean onCreateOptionsMenu(Menu menu){
		  	getMenuInflater().inflate(R.menu.main, menu);
		    	return true;
		}            
}



this is the error I am getting on the LogCat:


05-27 11:28:04.932: I/Choreographer(1900): Skipped 134 frames!  The application may be doing too much work on its main thread.
05-27 11:28:20.143: E/Trace(3677): error opening trace file: No such file or directory (2)
05-27 11:28:20.812: D/dalvikvm(3677): GC_CONCURRENT freed 41K, 6% free 2790K/2952K, paused 6ms+5ms, total 71ms
05-27 11:28:21.053: D/gralloc_goldfish(3677): Emulator without GPU emulation detected.
05-27 11:28:26.943: D/dalvikvm(3677): GC_FOR_ALLOC freed 113K, 8% free 2953


K/3188K, paused 126ms, total 142ms
05-27 11:28:26.953: I/dalvikvm-heap(3677): Grow heap (frag case) to 3.606MB for 635812-byte allocation
05-27 11:28:27.143: D/dalvikvm(3677): GC_FOR_ALLOC freed 2K, 7% free 3572K/3812K, paused 185ms, total 185ms
05-27 11:28:27.523: D/dalvikvm(3677): GC_CONCURRENT freed 6K, 6% free 3584K/3812K, paused 134ms+96ms, total 380ms
05-27 11:28:28.163: I/Choreographer(3677): Skipped 63 frames!  The application may be doing too much work on its main thread.
05-27 11:28:32.392: E/dalvikvm(3677): Could not find class 'javax.sql.XAConnection', referenced from method com.microsoft.sqlserver.jdbc.SQLServerConnection.poolCloseEventNotify
05-27 11:28:32.392: W/dalvikvm(3677): VFY: unable to resolve instanceof 1023 (Ljavax/sql/XAConnection;) in Lcom/microsoft/sqlserver/jdbc/SQLServerConnection;
05-27 11:28:32.392: D/dalvikvm(3677): VFY: replacing opcode 0x20 at 0x0014
05-27 11:28:32.472: E/No Connection to Server(3677): println needs a message



You assistance will be very much appreciated
Posted

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