0% found this document useful (0 votes)
72 views8 pages

Android With Mysql Gui Window

The document describes an Android application with a GUI for interacting with a MySQL database. It includes the XML layout code for the GUI containing form fields and a save button. It also includes the Java code which defines the activity class containing methods for inserting data from the form fields into a MySQL table on button click. The code establishes a connection to the MySQL database, prepares an INSERT statement, sets the parameter values, and executes the statement to save the data.

Uploaded by

Bala Sudhakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
72 views8 pages

Android With Mysql Gui Window

The document describes an Android application with a GUI for interacting with a MySQL database. It includes the XML layout code for the GUI containing form fields and a save button. It also includes the Java code which defines the activity class containing methods for inserting data from the form fields into a MySQL table on button click. The code establishes a connection to the MySQL database, prepares an INSERT statement, sets the parameter values, and executes the statement to save the data.

Uploaded by

Bala Sudhakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 8

ANDROID WITH MYSQL GUI WINDOW

ACTIVITY_MAIN.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="35dp"
android:text="Id" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="46dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginLeft="17dp"
android:layout_marginTop="33dp"
android:text="Name" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"

1
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:ems="10" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editText2"
android:layout_marginTop="30dp"
android:text="Score" />

<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignTop="@+id/textView3"
android:ems="10" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginLeft="52dp"
android:layout_marginTop="70dp"
android:onClick="load111"
android:text="Save" />

</RelativeLayout>

2
MAINACTIVITY.JAVA

package com.example.androidmysqlapp;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import android.os.Bundle;

import android.app.Activity;

3
import android.view.Menu;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends Activity {

EditText editTextid,editTextname,editTextscore;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

public void load111(View v)

new Thread(new Runnable()

@Override

public void run()

// TODO Auto-generated method stub

try {

4
insert();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}).start();

protected void insert() throws SQLException

editTextid=(EditText)findViewById(R.id.editText1);

editTextname=(EditText)findViewById(R.id.editText2);

editTextscore=(EditText)findViewById(R.id.editText3);

String id=editTextid.getText().toString();

String name=editTextname.getText().toString();

int score=Integer.parseInt(editTextscore.getText().toString());

try

Class.forName("com.mysql.jdbc.Driver");

String url="jdbc:mysql://10.0.2.2/brisk";

Connection c=DriverManager.getConnection(url,"root","admin123");

5
PreparedStatement st=c.prepareStatement("insert into cloudinsert
values(?,?,?)");

st.setString(1, id);

st.setString(2, name);

st.setInt(3, score);

st.execute();

st.close();

c.close();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

6
}

MYSQL VIEW:

7
8

You might also like