Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Android sample connect to the mysql server database and get the values of field of table , and display them.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
package com.example.andmysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity implements Runnable{
private String hiduke="";
private int price=0;
private String errmsg="";
public void run() {
System.out.println("Select Records Example by using the Prepared Statement!");
Connection con = null;
int count = 0;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://10.0.2.2:3306/stock","root","root");
try{
String sql;
// sql
// = "SELECT title,year_made FROM movies WHERE year_made >= ? AND year_made <= ?";
sql
= "SELECT hiduke,jikan,code,price FROM table_stock";
PreparedStatement prest = con.prepareStatement(sql);
//prest.setInt(1,1980);
//prest.setInt(2,2004);
ResultSet rs = prest.executeQuery();
while (rs.next()){
hiduke = rs.getString(1);
price = rs.getInt(4);
count++;
System.out.println(hiduke + "\t" + "- " + price);
}
System.out.println("Number of records: " + count);
prest.close();
con.close();
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
errmsg=errmsg+s.getMessage();
}
}
catch (Exception e){
e.printStackTrace();
errmsg=errmsg+e.getMessage();
}
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
TextView textView = (TextView) findViewById(R.id.textView0);
textView.setText("hiduke="+hiduke+" price="+price+" "+errmsg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread thread = new Thread(this);
thread.start();
}
}
@marcoderoni

This comment has been minimized.

Show comment Hide comment
@marcoderoni

marcoderoni Oct 21, 2015

Hi, thanks, it's the first code that I try right away and works out! May I see a screenshot of the table you uploaded on the server, please? I'd like to see if you took the whole column or the value of a particular row! Thanks in advance!

Hi, thanks, it's the first code that I try right away and works out! May I see a screenshot of the table you uploaded on the server, please? I'd like to see if you took the whole column or the value of a particular row! Thanks in advance!

@akhlaqrifat

This comment has been minimized.

Show comment Hide comment
@akhlaqrifat

akhlaqrifat Nov 15, 2017

i am a beginner..
so can u help me for creating a server like yours
and 1 more thing what .jar files i have to add in my app and how to work this out?
thank you

akhlaqrifat commented Nov 15, 2017

i am a beginner..
so can u help me for creating a server like yours
and 1 more thing what .jar files i have to add in my app and how to work this out?
thank you

@archiedalisay

This comment has been minimized.

Show comment Hide comment
@archiedalisay

archiedalisay Nov 26, 2017

Hi, I have concern with connection of my app to sql server, once the app launch its fast to connect but for the second time to connect its takes long time to connect again...what will be my option to avoid..Thanks in advance!

Hi, I have concern with connection of my app to sql server, once the app launch its fast to connect but for the second time to connect its takes long time to connect again...what will be my option to avoid..Thanks in advance!

@MostafaGad1911

This comment has been minimized.

Show comment Hide comment
@MostafaGad1911

MostafaGad1911 Dec 11, 2017

Hi , am having an exception "Can not connect to database" can u help me?

Hi , am having an exception "Can not connect to database" can u help me?

@satishf889

This comment has been minimized.

Show comment Hide comment
@satishf889

satishf889 Feb 28, 2018

Thanks bro it helped me I wasted alot of time for this earlier.

Thanks bro it helped me I wasted alot of time for this earlier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment