-1

I am calling db in java class from activity(android).Is it possible to call like this from activity to java class.Am using this for server communication android.I got data from server but i cant able to insert into DB.

I called like that am getting exception could any one help me, Thanks in advance.

11-01 06:52:03.596: W/System.err(673): java.lang.NullPointerException
11-01 06:52:03.603: W/System.err(673):  at com.commiunication.ServerConnection.Insert(ServerConnection.java:32)
11-01 06:52:03.627: W/System.err(673):  at com.commiunication.MainActivity.serverconnection(MainActivity.java:138)
11-01 06:52:03.627: W/System.err(673):  at com.commiunication.MainActivity.onCreate(MainActivity.java:46)
11-01 06:52:03.633: W/System.err(673):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-01 06:52:03.633: W/System.err(673):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-01 06:52:03.633: W/System.err(673):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 06:52:03.633: W/System.err(673):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 06:52:03.633: W/System.err(673):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 06:52:03.633: W/System.err(673):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 06:52:03.633: W/System.err(673):  at android.os.Looper.loop(Looper.java:123)
11-01 06:52:03.633: W/System.err(673):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 06:52:03.633: W/System.err(673):  at java.lang.reflect.Method.invokeNative(Native Method)
11-01 06:52:03.655: W/System.err(673):  at java.lang.reflect.Method.invoke(Method.java:521)
11-01 06:52:03.655: W/System.err(673):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-01 06:52:03.655: W/System.err(673):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-01 06:52:03.655: W/System.err(673):  at dalvik.system.NativeStart.main(Native Method)

androidactivity.java

ServerConnection server=new ServerConnection();
try {

server.Insert(ReturnValue);
sampleDB =    this.openOrCreateDatabase(SAMPLE_DB_NAME,MODE_PRIVATE,null);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

serverConnection.java

public  void Insert(String ReturnValue) throws JSONException{
JSONObject CDGSGJson=new JSONObject(ReturnValue);           
JSONArray idarr = CDGSGJson.getJSONArray("MeasureArr"); 
System.out.println(""+idarr.length());  
sampleDB= openOrCreateDatabase(SAMPLE_DB_NAME,     SQLiteDatabase.OPEN_READWRITE, null);
System.out.println(""+idarr.length());  
//Toast.makeText(this, "Group Details Downloading", Toast.LENGTH_SHORT).show();
try {   
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + table_name + " "
+ "( id INTEGER PRIMARY KEY AUTOINCREMENT," + " measure TEXT NOT NULL ); ");       
if(idarr.length()>0){

for(int cc=0;cc<idarr.length();cc++){               
JSONObject CatObj = idarr.getJSONObject(cc);
String id_get=CatObj.getString("id");   
String measure_get=CatObj.getString("measure"); 

sampleDB.execSQL("INSERT OR REPLACE INTO " + table_name+ " Values      
                                                ('"+id_get+"','"+measure_get+"');");

            }}

    } catch (Exception e) {
        // TODO: handle exception
         e.printStackTrace();
        // System.out.print(e.printStackTrace());
    }
APriya
  • 1,994
  • 1
  • 16
  • 21

2 Answers2

0

In android in order to use db there is a built in api called SqLiteOpenHelper.

Please go through http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

Here is a sample code

http://www.vogella.com/articles/AndroidSQLite/article.html

Sai Mukesh
  • 401
  • 4
  • 11
0

See the below links may help full to you.

For local database interaction : Link1. you can directly download source code from that and trying to understand it. Link2.

For Remote database : For remote database you have to make webService that give response to your android client app. Please refer this Link1, Link2 for Andoid with php-mysql.

Hope above links may help you. Enjoy coding.