1

First of all. I am new to android application development environment and I am trying to connect to mssqlserver 2008 with simple android application using jtds-1.3.1.jar driver . I googled many example on internet but i failed to connect with database .
The exception i am getting is Network error IOException: connection time out
I don't know what is wrong with my code i am using Eclipse juno IDE.
here is my code

package com.example.Testproject1;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.widget.TextView;
import java.sql.*;


public class MainActivity extends ActionBarActivity {



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


    public void connectTodatabase()
    {
        TextView txtView=(TextView)findViewById(R.id.textView2);
        String url = "jdbc:jtds:sqlserver://XXX.XXX.X.XXX:1433;DatabaseName=VautomateuShoppi";
        String driver = "net.sourceforge.jtds.jdbc.Driver";
        String userName = "VShopping_User";
        String password = "VShopping_Pass";   
        // Declare the JDBC objects.
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
    try
    {

        // Establish the connection.
        Class.forName(driver);
        con = DriverManager.getConnection(url, userName, password);
            // Create and execute an SQL statement that returns some data.
            String SQL = "select * from SeoMaster";
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);

            // Iterate through the data in the result set and display it.
            while (rs.next()) {
                txtView.setText(rs.getString(2));
            }
    }
    catch(Exception ex)
    {
        txtView.setText(ex.getMessage().toString());
    }
    }
}
Saveen
  • 702
  • 2
  • 14
  • 34
  • Connect DB directly from App ? Are you sure ? Check if network port is open (but still not recommended to do so, for security issue) – Raptor Sep 26 '14 at 05:54
  • yes my network port is opened! – Saveen Sep 26 '14 at 05:56
  • @Raptor What are the security issues and what would you recommend ? I have an asp.net site that talks with SQL server(but that's kinda natural for them two) and there, the sensitive data is in the config, obviously having the pw and the un in the code like this above would take sub-par skills to brake into ... What would you recommend to do ? – Иво Недев Mar 05 '15 at 13:21
  • Never connect DB directly from the Internet, unless the connection is secured (e.g. by VPN). If the port is open, your DB is probably vulnerable to attacks. Use a script to handle the request & fetch the data from DB, always! – Raptor Mar 06 '15 at 01:55

2 Answers2

2

add these code

StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Jens
  • 67,715
  • 15
  • 98
  • 113
kgmganesh
  • 30
  • 5
1

Try This

public List<String> dbConnect(String Host, String Port, String db_userid,
                String db_password) {
            List<String> Db_list = new ArrayList<String>();
            try {
                String ConnectionString = "jdbc:jtds:sqlserver://" + Host + ":"
                        + Port;
                // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
                Connection conn = DriverManager.getConnection(ConnectionString,
                        db_userid, db_password);
                System.out.println("connected");
                Statement statement = conn.createStatement();
                String queryString = "select name from sys.databases";
                ResultSet rs = statement.executeQuery(queryString);
                while (rs.next()) {
                    Db_list.add(rs.getString(1));
                }
            } catch (Exception e) {
                Db_list.add("Error");
                e.printStackTrace();
            }
            return Db_list;
        }

Refer Sql Database connect Using the JDBC Driver with android

Community
  • 1
  • 1
Prasanth S
  • 3,725
  • 9
  • 43
  • 75
  • Thanks for your quick replay but still getting the same error Network error IOException: connection time out – Saveen Sep 26 '14 at 06:23
  • no i am accessing live server, i tried many example found on google but failed to connect every time. @PandiyanMuthu – Saveen Sep 27 '14 at 11:49