Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java SQL Android
Hi,
 
I'm trying to pass a 'Session Variable' (SharedPreferences) to a class, Profile.
 
How can I achieve this?
 
This is what I tried so far:
 
**Profile:**
 

  package com.example.projectmanager;
    
    import java.sql.Connection;
    import java.sql.Date;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.util.List;
    
    import android.os.AsyncTask;
    import android.util.Log;
    
    public class Profile extends AsyncTask <List<Profile>, Void, List> {
    	
    		int user_id;
    		String name; 
    		String surname ;
    		String username;
    		String address1;
    		String address2;
    		String country;
    		String occupation;
    		String department;
    		Date DOB;
    		
    		List<Profile> profileInfo = new ArrayList<Profile>();
    		
    			
    		
    		protected List<Profile> doInBackground(List... params) {
    			
    			 Connection conn = null;
    			 	try {	
    			 		
    			 		
    			 		
    			 	String driver = "net.sourceforge.jtds.jdbc.Driver";
    			 	Class.forName(driver).newInstance();
    			 	
    			 	String connString = "jdbc:jtds:sqlserver://10.0.2.2/master_db;";
    			 	String sqlusername = "admin";
    			 	String sqlpassword = "root";
    			 	
    			 	conn = DriverManager.getConnection(connString, sqlusername, sqlpassword);
    			 	Log.w("Connection","open");
    			 	
    			 	String profileQuery = "SELECT * FROM users WHERE user_id = ?"; 
    
    			 	PreparedStatement stmt = conn.prepareStatement(profileQuery);
    		
    			 	stmt.setLong(1, user_id);
    
    			 	ResultSet rs;
    			 	
    			 	rs = stmt.executeQuery();
    			 	
    			 	
    			 	
    			 	while (rs.next()) {
    			 		
    	 		 		Profile userProfile = new Profile();
    	 		 		userProfile.name = rs.getString("name");
    	 		 		userProfile.username = rs.getString("username");
    	 		 		userProfile.surname = rs.getString("surname");
    	 		 		userProfile.address1 = rs.getString("address1");
    	 		 		userProfile.address2 = rs.getString("address2");
    	 		 		userProfile.country = rs.getString("country");
    	 		 		userProfile.DOB = rs.getDate("date_of_birth");
    	 		 		userProfile.occupation  = rs.getString("occupation");
    			 		profileInfo.add(userProfile);	
    
    			 	}
    			 	
    			 	conn.close();
    			 	 
    			 	} catch (Exception e)
    			 	{
    			 	Log.w(e.getMessage(), e);
    			 	}
    
    			
    			return profileInfo;
    			
    			
    			
    		}
    		protected void onPostExecute(List posts) {
    	        // Result is here now, may be 6 different List type.
    	        this.profileInfo = profileInfo; 
    	    }
    
    		  @Override
    	      public String toString() {
    	          return "Name:" + name + " " + surname+ "\n" +
    	        		 "Username : " + username  + "\n" +
    	        		 "Date of Birth : " + DOB  + "\n\n" +
    	        		 "Occupation : " + occupation  + "\n\n" +
    	        		 "Address: " + address1  + "\n" +
    	        		 address2  + "\n" +
    	        		 "Country : " + country  + "\n";
    	        		 
    	                 
    	      }
    		
    
    	}
 
Profile Activity:
 
  package com.example.projectmanager;
    
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.ArrayAdapter;
    import android.widget.ExpandableListView;
    import android.widget.ListView;
    import android.content.Intent;
    
    public class ProfileActivity extends Activity {
    
    	@SuppressWarnings("unchecked")
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_profile);
    		
    		Bundle extras = getIntent().getExtras(); 
    		
    		int userid = extras.getInt("user_id");
    		
    		Profile uid = new Profile();
    		uid.user_id = userid;
    		
    		uid.execute().get();
    		
    		ListView elv = (ListView) findViewById(R.id.elvProfile);
    		List profileInfo = null;
    		
    		try {
    			profileInfo = new Profile().execute().get();
    		} catch (InterruptedException e) {
    			
    			e.printStackTrace();
    			
    		} catch (ExecutionException e) {
    			
    			e.printStackTrace();
    		}
    		
    		
    		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, profileInfo);
    		elv.setAdapter(arrayAdapter); 
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.profile, menu);
    		return true;
    	}
    
    }
 

**MainActivity:**
 
    package com.example.projectmanager;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.concurrent.ExecutionException;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import com.example.projectmanager.*;
    
    
    public class MainActivity extends Activity {
    	
    	public EditText username, password;
    	public  String usernameText, passwordText;
    	
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
         
      	    username = (EditText) findViewById(R.id.txtUsername);
            password = (EditText) findViewById(R.id.txtPassword);
    
           
            Button loginButton = (Button)findViewById(R.id.btnLogin);
        
     
        loginButton.setOnClickListener(new View.OnClickListener(){
        	public void onClick(View v) {
        		
        		
        		
        		 usernameText = username.getText().toString();
        		 passwordText = password.getText().toString();
        		
    				String cl = null;
    				try {
    					cl = new CheckLogin(usernameText, passwordText).execute().get();
    					
    					String userid = cl;
    					
    					if (userid != null ){
    						
    							
    						Intent i = new Intent(getApplicationContext(), HomeActivity.class);
    						startActivity(i);
    						
    						i.putExtra("userid", userid);
    						
    								
    						
    						
    					} else {
    						
    						Context context = getApplicationContext();
    						Toast msg = new Toast(context);
    						msg.setText("Incorrect login details. Please try again");
    						msg.show();
    										}
    
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (ExecutionException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
     
        	}
        	
        ;});
        
        }
        
        
    } 
        
 
What I'm trying to do is I'm trying to pass a parameter to an SQL command in the Profile class and retrieve details related to a user by using his user_id.
Posted 22-Apr-13 12:49pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 334
2 CPallini 275
3 DamithSL 230
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100