This is the following code:
`package com.tom.jam;
//import java.sql.Connection;
//import java.sql.DriverManager;
import java.sql.*;
import android.app.Activity;
//import android.database.SQLException;
import android.os.Bundle;
import android.widget.TextView;
public class MyserverActivity extends Activity {
TextView mf,ct;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mf=(TextView) findViewById(R.id.myfield);
ct=(TextView) findViewById(R.id.cont);
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://192.168.0.100/test","root", "secret");
if(!con.isClosed())
{
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from ram");
while(rs.next())
{
ct.setText("ID" + rs.getString(1) + "Name " + rs.getString(2));
}
mf.setText("Successfully connected to " +
"MySQL server using TCP/IP...");
}
} catch(Exception e) {
mf.setText("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
}catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}`
we have set the following permissions in
manifest:android.permission.ACCESS_NETWORK_STATE,
android.permission.INTERNET,
android.permission.WRITE_EXTERNAL_STORAGE
NO errors and warnings but while running emulator is showing the following exception:
Unable to connect to any hosts due to exception:java.net.SocketTimeoutException:Connection timed out.
Thanks in advance
java.net.SocketTimeoutException:Connection timed out, exception occurs when destination system is not reachable. It may be due to proxy not set on your emulator, to set proxy on emulator you can start emulator by command in command prompt:
Here replace avdname to name of avd you want to run your program, and http:192.168.1.1 to your proxy server.