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