0

I am brand new in android. I need to save value in mysql database. But I have no idea how to do it. I have saved value in sqlite database in android. But how to save in mysql database ?!!! Can anyone please help me on this please ?!! Here are my attempts below so far I have done >>>

my activity_main.xml :::

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="com.example..savedata2mysql.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name" />

    <EditText
    android:id="@+id/name"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:inputType="text"/>

    <Button
    android:text="Save Value"
    android:id="@+id/btnAdd"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="onClickAddName" />

</LinearLayout>

my onClickAddName function where I want to save value >>>

public void onClickAddName(View view) throws IllegalAccessException, InstantiationException,
ClassNotFoundException, SQLException {
    ContentValues values = new ContentValues();
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String name = ((EditText)findViewById(R.id.name)).getText().toString();
    String sql = "INSERT INTO person (person_name) VALUES ('"+name+"')";

    try {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demotest", "root", "sumon123");
        Statement stmt = con.createStatement();
        stmt.executeUpdate(sql);
        stmt.close();
        con.close();
        Toast.makeText(getBaseContext(), "Person Name Saved Successfully.", Toast.LENGTH_LONG).show();
        ((EditText)findViewById(R.id.name)).setText("");
    } catch (SQLException e) {
        e.printStackTrace();
    }
 }

my AndroidMainfest.xml >>>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

And I have paste this jar to libs folder >> mysql-connector-java-5.1.18-bin.jar

4
  • sampleprogramz.com/android/mysqldb.php Commented Jan 8, 2015 at 11:00
  • @Duggu I have a look at it, but it is build with php. I want to do it in java Commented Jan 8, 2015 at 11:09
  • @Summon then find it out with java,, there is lot of code available in internet .... Commented Jan 8, 2015 at 11:10
  • @Duggu sorry for silly question but I have goggled for this but no result, any source code may be helpful a lot. Commented Jan 8, 2015 at 11:16

2 Answers 2

1

You need to use webservice as in interface to store value in mysql. You can refer following link to see how webservice and android works

http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

Sign up to request clarification or add additional context in comments.

Comments

1

you need to make back end for your android app and this back end provides for you a web services that you can used it to save your value you need to know

  1. how to make RESTFULL web service
  2. how call web service from android "using Async task" or any library

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.