0

i'm new on java and android ,I'm trying to connect an android application to a SQL Server database

i'm using this code:

package com.example.sqlconnection;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.Bundle;

import android.os.StrictMode;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.SimpleAdapter;


public class MainActivity extends Activity {


Button exec;
EditText textbox;
ListView list;
Connection connect;
SimpleAdapter AD;


private void declarer(){
    exec=(Button) findViewById(R.id.btn);
    textbox=(EditText) findViewById(R.id.textbox);
    list=(ListView) findViewById(R.id.list);
}


private void initialiser(){
    declarer();
    textbox.setText("SELECT * FROM TEST");      
    connect = CONN("sa", "123456", "android", "192.168.1.1:1433");
}

@SuppressLint("NewApi")
private Connection CONN(String _user, String _pass, String _DB , String _server){
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection conn=null;
    String ConnURL=null;
    try {

        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnURL="jdbc:jtds:sqlserver://"+_server+"/"+"databaseName="+_DB+";user="+_user+";password="+_pass+";";
        conn=DriverManager.getConnection(ConnURL);


    } catch (SQLException se) {
        // TODO: handle exception
        Log.e("ERROE",se.getMessage());
    }catch (ClassNotFoundException e) {
        // TODO: handle exception
        Log.e("ERROE",e.getMessage());
    }catch (Exception e) {
        // TODO: handle exception
        Log.e("ERROE",e.getMessage());
    }


    return conn ;
}


public void QuerySQL(String COMMANDSQL){
    ResultSet rs ;
    try {

        Statement statement=connect.createStatement();
        rs=statement.executeQuery(COMMANDSQL);

        List<Map<String,String>> data=null;
        data=new ArrayList<Map<String,String>>();
        while(rs.next()){
            Map<String,String> datanum=new HashMap<String,String>();
            datanum.put("A", rs.getString("NAME"));
            datanum.put("B", rs.getString("CITY"));
            data.add(datanum);
        }
        String[] from={"A","B"};
        int[] views={R.id.txt_2,R.id.txt_1};
        AD=new SimpleAdapter(this, data, R.layout.modele, from, views);
        list.setAdapter(AD);
    } catch (Exception e) {
        // TODO: handle exception
        Log.e("ERROR",e.getMessage());
    }
}




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initialiser();
    exec.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            QuerySQL(textbox.getText().toString());

        }
    } );
}


}

i also added the autorisations :

<uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

but when running i have this error:

Network error IOException : failed to connect to/192.168.1.1(port 1433): connect failed: ETIMEDOUT Connection timed out)
Saif Hamed
  • 1,084
  • 1
  • 12
  • 17
  • Either there's something blocking your connection attempt (a firewall, for instance), or there's nothing listening on that IP at that port (192.168.1.1 seems to be a router device if you're using default values) – nKn Mar 07 '14 at 23:34
  • 1
    My opinion, connect to server from java code is bad idea, java is easy to `Reverse Engineering`. As i see, you put all connection data in your code. **See [this](http://stackoverflow.com/questions/13854425/how-to-avoid-reverse-engineering-of-an-apk-file)**. However, I think the problem wrong IP as @nKn said. **Good luck** – Saif Hamed Mar 07 '14 at 23:48

1 Answers1

0

Don't pass Port Name ... I think it till work

use

connect = CONN("sa", "123456", "android", "192.168.1.1");

instead of

connect = CONN("sa", "123456", "android", "192.168.1.1:1433");
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
sumit
  • 1