Skip to main content
  • Saurabh Singh

    Hi Hitesh,
    Thank You for sharing this tutorial. It solved my previous problem.
    Could you please share the download link.
    Thank You.

  • Poovizhi Chelladurai

    Hi. I have doubt in Printer connection In android studio project to print the datas in more than 50 lines, its continuesly printing in kitkat version tablet, but lollipop version its struck in half way printing.

  • vishal

    Hi sir…m gettibg error like ” Error in connection with SQL server” n in LOgcat it showing error “net.sourceforge.jtds.jdbc.Driver”;

    • Aini Asiqin

      have u solve the error ? i got same error like that

  • Deepak Puma

    hello hitesh nice work done its running perfectly in my android device which fetches the data from my remote sql server but after applying comment in this two override which is in SecondActivity.java
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_logout) {
    LogOut();
    return true;
    }
    return super.onOptionsItemSelected(item);
    }

    and

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    here i am confused since you have not added any menu folder in application plus “action_logout” is id of which control.
    Thanks in advance …

  • Pingback: Connect Android to MS SQL Database. • ParallelCodes();()

  • Vijay Sutariya

    Nice Vikani, Great and easy for understanding

  • Shailesh Kumar

    connect failed: ETIMEDOUT (Connection timed out) error

    Here is my code, please help…

    import android.annotation.SuppressLint;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.support.v7.app.ActionBarActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.Toast;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class MainActivity extends ActionBarActivity
    {
    // Declaring layout button, edit texts
    Button login;
    EditText username,password;
    ProgressBar progressBar;
    // End Declaring layout button, edit texts

    // Declaring connection variables
    Connection con;
    String un,pass,db,ip;
    //End Declaring connection variables

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

    // Getting values from button, texts and progress bar
    login = (Button) findViewById(R.id.button);
    username = (EditText) findViewById(R.id.editText);
    password = (EditText) findViewById(R.id.editText2);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setVisibility(View.GONE);
    // End Getting values from button, texts and progress bar

    // Declaring Server ip, username, database name and password
    ip = “192.168.1.100”;
    db = “restaurant”;
    un = “sa”;
    pass = “123”;
    // Declaring Server ip, username, database name and password

    // Setting up the function when button login is clicked
    login.setOnClickListener(new View.OnClickListener()
    {
    @Override
    public void onClick(View v)
    {

    String usernam = username.getText().toString();
    String passwordd = password.getText().toString();
    CheckLogin checkLogin = new CheckLogin();// this is the Asynctask, which is used to process in background to reduce load on app process
    checkLogin.execute(usernam,passwordd);
    }
    });
    //End Setting up the function when button login is clicked
    }

    public class CheckLogin extends AsyncTask
    {
    String z = “”;
    Boolean isSuccess = false;

    @Override
    protected void onPreExecute()
    {
    progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(String r)
    {
    progressBar.setVisibility(View.GONE);
    Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
    if(isSuccess)
    {
    Toast.makeText(MainActivity.this , “Login Successfull” , Toast.LENGTH_LONG).show();
    //finish();
    }
    }
    @Override
    protected String doInBackground(String… args)
    {
    String usernam = args[0];
    String passwordd = args[1];
    if(usernam.trim().equals(“”)|| passwordd.trim().equals(“”))
    z = “Please enter Username and Password”;
    else
    {
    try
    {
    con = connectionclass(un, pass, db, ip); // Connect to database
    if (con == null)
    {
    z = “Error in connection with SQL server!”;
    }
    else
    {
    String query = “select * from login where user_name= ‘” + usernam.toString() + “‘ and pass_word = ‘”+ passwordd.toString() +”‘ “;
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    if(rs.next())
    {
    z = “Login successful”;
    isSuccess=true;
    con.close();
    }
    else
    {
    z = “Invalid Credentials!”;
    isSuccess = false;
    }
    }
    }
    catch (Exception ex)
    {
    isSuccess = false;
    z = ex.getMessage();
    }
    }
    return z;
    }
    }

    @SuppressLint(“NewApi”)
    public Connection connectionclass(String user, String password, String database, String server)
    {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection connection = null;
    String ConnectionURL = null;
    try
    {

    Class.forName(“net.sourceforge.jtds.jdbc.Driver”);
    ConnectionURL = “jdbc:jtds:sqlserver://” + ip + “;”
    + “databaseName=” + db + “;user=” + un + “;password=”
    + password + “;”;
    connection = DriverManager.getConnection(ConnectionURL);
    }
    catch (SQLException se)
    {
    Log.e(“error here 1 : “, se.getMessage());
    }
    catch (ClassNotFoundException e)
    {
    Log.e(“error here 2 : “, e.getMessage());
    }
    catch (Exception e)
    {
    Log.e(“error here 3 : “, e.getMessage());
    }
    return connection;
    }
    }

    • Shailesh Kumar

      It’s Urgent Please help me

    • Shailesh Kumar

      Thanks in Advance…