Friday, January 16, 2015
Android MySQL Client Part 2
My first tutorial Android MySQL client is about read data from a MySQL database. Here Im going to insert data to MySQL database using an Android program. Concept is same as before (Before start test this read first tutorial.)
Program has three main parts.
1. Database (MySQL)
2. Java web service.
3. Android application.
Java web service deployed on Tomcat server has a method which accepts two values and it runs a quarry on database to insert data & also this method returns a string.
The Android application calls that web service method remotely with two values using ksoap library. Then web service runs a query on database table and inserts data
1. First create the database and table.
CREATE DATABASE login;
USE login;
CREATE TABLE users(
name varchar(20),
password varchar(20)
);
2. Then create the web service
Here is the content of my web serviceimport java.sql.Connection;In line 12 root and chathura are user and the password. Change those with your username and the password.
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class Users {
public String insertData(String userName,String userPassword){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","chathura");
PreparedStatement statement = con.prepareStatement("INSERT INTO users(name,password) VALUES ("+userName+","+userPassword+");");
int result = statement.executeUpdate();
}
catch(Exception exc){
System.out.println(exc.getMessage());
}
return "Insertion successfull!!";
}
}
This java web service has JDBC connector to access the database. Click here to download the connector.Import JDBC connector to your project. This tutorial is about importing the ksaop library. In the same way you can import JDBC library also. It is simple
You can implement the web service easily by following my these posts.
1. Create java web service in Eclipse using Axis2 (Part 01)
2. Create java web service in Eclipse using Axis2 (Part 02)
3. Android application.
The Android application uses ksoap2 library to access java web service. You can find More details about implementation of Android client applications from here. If you are planning to use new Android version read this tutorial.
Here is the code for Android application.
You need to change name space, url, soap action and method name according to your web service.
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidMySQLClientActivity extends Activity{
private final String NAMESPACE = "http://ws.login.com";
private final String URL = "http://175.157.3.42:8085/InsertToUsers/services/Users?wsdl";
private final String SOAP_ACTION = "http://ws.login.com/insertData";
private final String METHOD_NAME = "insertData";
Button btninsert;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btninsert = (Button)findViewById(R.id.btn_insert);
btninsert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
insertValues();
}
});
}
public void insertValues(){
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
EditText userName = (EditText) findViewById(R.id.editText1);
String user_Name = userName.getText().toString();
EditText userPassword = (EditText) findViewById(R.id.editText2);
String user_Password = userPassword.getText().toString();
//Pass value for userName variable of the web service
PropertyInfo unameProp =new PropertyInfo();
unameProp.setName("userName");//Define the variable name in the web service method
unameProp.setValue(user_Name);//Define value for fname variable
unameProp.setType(String.class);//Define the type of the variable
request.addProperty(unameProp);//Pass properties to the variable
//Pass value for userPassword variable of the web service
PropertyInfo passwordProp =new PropertyInfo();
passwordProp.setName("userPassword");
passwordProp.setValue(user_Password);
passwordProp.setType(String.class);
request.addProperty(passwordProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
TextView result = (TextView) findViewById(R.id.textView2);
result.setText(response.toString());
}
catch(Exception e){
}
}
}
4.Code for main.xml file
5. Content of the Manifest.xml (Add internet permission)
Note : Click here to download the Android project.
Do you think this is helpful ? Please write your ideas :)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.