Thursday 6 September 2012



Android and oracle communication
AndroidWebActivity.java
package com.android.webview;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidWebActivity extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }
   
          @Override
          public void onClick(View v) {
                   // TODO Auto-generated method stub
          Toast.makeText(AndroidWebActivity.this, "I am inside onClick()", Toast.LENGTH_LONG).show();
          EditText et=(EditText)findViewById(R.id.editText1);
          EditText et1=(EditText)findViewById(R.id.EditText01);
          String uname=et.getText().toString();
          String salary=et1.getText().toString();
          TextView tv=(TextView)findViewById(R.id.textView1);
         
          String url="http://192.168.2.100:8085/"+"NewThis1/MyJsp.jsp?t1="+uname+"&t2="+salary;
   
          try
          {
                   Toast.makeText(AndroidWebActivity.this, "try", Toast.LENGTH_LONG).show();
                   HttpClient httpclient=new DefaultHttpClient();
                   HttpPost httppost=new HttpPost(url);
                   Log.i("Connected","Connection Established");
                   HttpResponse response=httpclient.execute(httppost);
                   Log.i("Posted","Posted Data Success");
              
BufferedReader  in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                   StringBuffer sb=new StringBuffer("");
                   String  line="";
                   //String NL=System.getProperty("line.separator");
                                     
                   while((line=in.readLine())!=null)
                   {
                             sb.append(line);
                   }
                  
                   in.close();
                   String result=sb.toString();              
                   String result123=result.replaceAll("\\s+", "");
                   tv.setText(result);
          }
          catch(Exception e)
          {
                   e.printStackTrace();
          }
                  
          }
}

Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="25dp"/>

    <EditText
        android:id="@+id/EditText01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
       
        <requestFocus />
    </EditText>
       
<EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

      
    </EditText>


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#00FF00"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.webview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
     <uses-permission android:name="android.permission.INTERNET"/>
   
       <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidWebActivity"
            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>

</manifest>
Create  jsp page using myeclipse
New=>web project=>project name:NewThis1=>Context root URL :java EE6.0=>Right click on WebRoot=>new=>jsp(Advance Templates)=>File name:Myjsp.jsp
Index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
          <head>
                   <meta http-equiv="Content-Type"
                             content="text/html; charset=ISO-8859-1">
                   <title>Insert title here</title>
          </head>
          <body>
                   <center>
                             <h1>
                                      Interface Software
                             </h1>
                             <br>
                             <br>
                             <form action="MyJsp.jsp" method="post">
                                      Enter Name
                                      <input type="text" name="t1">
                                      <br>
                                      <br>
                                      <input type="submit" value=" LOGIN ">
                             </form>
                   </center>
          </body>
</html>
Myjsp.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          import="java.sql.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
          <head>
                   <meta http-equiv="Content-Type"
                             content="text/html; charset=ISO-8859-1">
                   <title>Insert title here</title>
          </head>
          <body>
                   <center>
                             Sahid Nagar,Bhubaneswar
                             <br>
                             <br>
                             Name is &nbsp;&nbsp;&nbsp;&nbsp;<%=request.getParameter("t1")%>
<br>

                             Salary is &nbsp;&nbsp;&nbsp;&nbsp;<%=request.getParameter("t2")%>
                             <br>
                             <br>

<%--Password is &nbsp;&nbsp;<%=request.getParameter("t2") %><br> --%>
<%
          String username = request.getParameter("t1");
          String salary = request.getParameter("t2");                                       Class.forName("oracle.jdbc.driver.OracleDriver");                                    Connection con = DriverManager.getConnection(                                                      "jdbc:oracle:thin:@localhost:1521:XE", "system", "manager");                                     Statement st = con.createStatement();                                     st.executeUpdate("insert into test3 values('" + username + "','"+salary+"')");                out.println(username);
%>
                   </center>
          </body>
</html>

Create database inoracle
Ø Start=>oracle database 10g express edition=>give(user name,password)
Ø Object browser=>create=>table=>table name:text3=>columu
Ø name:username=>type:varchar2=>conferm=>create

Deploy in tomcat server
Ø Open myeclipse web browser =>
Ø Tomcat Manager=> check project is deployed properly or not
      


No comments:

Post a Comment