Click here to Skip to main content
11,707,470 members (73,623 online)
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 11:49am

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

  Print Answers RSS
0 Mika Wendelius 220
1 Maciej Los 195
2 OriginalGriff 148
3 Sergey Alexandrovich Kryukov 145
4 chainerlt 110
0 OriginalGriff 9,198
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,921
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2015
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