2

I am and building an android app. Where my ConnectionURL is

ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
                + "databaseName=" + database + ";user=" + user
                + ";password=" + password + ";";

Acc. to this answer: Help me create a jTDS connection string

The connectionString should have been

jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value[;...]]        

jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS;user=sa;password=s3cr3t

Or, if you prefer to use getConnection(url, "sa", "s3cr3t"):

jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS

But, I am not using SQLEXPRESS. I using Microsoft SQL Server Developer Edition.

What I have done is, one button click(android) the event occurs. Login form verifies UserName and Password and writes to new TextEdit.

package com.muchmore.mydatabase;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.annotation.SuppressLint;
import android.content.Intent;
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.TextView;

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


public class MainActivity extends AppCompatActivity {
     Button loginbtn;
     TextView errorlbl;
     EditText edname, edpassword;
     Connection connect;
     PreparedStatement preparedStatement;
     Statement st;
     String ipaddress, db, username, password;

     @SuppressLint("NewApi")
            private Connection ConnectionHelper(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://" + server + ";"
                                 + "databaseName=" + database + ";user=" + user
                                 + ";password=" + password + ";";
                  /*
                  I was tyring to built a right connection string here. Please get it working :) 
                  ConnectionURL = "jdbc:jtds:sqlserver://localhost/"
                                 + "databaseName=" + "MyDatabase" + ";user=" + "sa"
                                 + ";password=" + "sa@123" + ";";
                  */
                 //private String URL =            "jdbc:jtds:sqlserver://localhost/brandix;instance=sqlexpress;useNTLMv2=tru‌​e;domain=workgroup"
                 connection = DriverManager.getConnection(ConnectionURL);
                 } catch (SQLException se) {
                       Log.e("ERRO", se.getMessage());
                 } catch (ClassNotFoundException e) {
                       Log.e("ERRO", e.getMessage());
                 } catch (Exception e) {
                       Log.e("ERRO", e.getMessage());
                 }
               return connection;
             }

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

             loginbtn = (Button) findViewById(R.id.btnlogin);

             errorlbl = (TextView) findViewById(R.id.lblerror);

             edname = (EditText) findViewById(R.id.txtname);
             edpassword = (EditText) findViewById(R.id.txtpassword);

             ipaddress = "127.0.0.1";
             db = "MyDatabase";
             username = "sa";
             password = "sa@123";
             connect = ConnectionHelper(username, password, db, ipaddress);
             loginbtn.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View v) {
                 try {
                      connect = ConnectionHelper(username, password, db, ipaddress);

                      st = connect.createStatement();
                      ResultSet rs = st.executeQuery("select * from login where userid='" + edname.getText().toString() + "' and password='" + edpassword.getText().toString() + "'");
                      if (rs != null && rs.next()) {
                                errorlbl.setText("Login Successful!!!");
                       } else {
                                errorlbl.setText("Sorry, wrong credentials!!!");
                      }

                      } catch (SQLException e) {
                           errorlbl.setText(e.getMessage().toString());
                      }
                }
    });

There is null reference in connect reference. So, I am sure there is some error in connection string. Otherwise my android code looks fine. Please see whats the right format of string.

Community
  • 1
  • 1
abhimanyuaryan
  • 3,882
  • 5
  • 41
  • 83

1 Answers1

1

There are two most popular JDBC drivers for MS SQL. One is JTDS and another one is official Microsoft JDBC driver. It looks like you mixing connection string syntax for two of the drivers.

JTDS driver connection string example is: jdbc:jtds:sqlserver://NETWORK_HOST_NAME_OR_IP:1433/NAME_OF_DATABASE

Some more hints on creating this can be find: http://jtds.sourceforge.net/faq.html#urlFormat

Microsoft JDBC connection string example is: jdbc:sqlserver://NETWORK_HOST_NAME_OR_IP:1433;databaseName=NAME_OF_DATABASE

Note that instance name is optional. Please check this article for omprehensive documentation: https://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx

If you need to find out your MSSQL instance name to use in connection string, the easiest way to do this is execute query:

SELECT @@SERVERNAME

JJ Roman
  • 4,225
  • 1
  • 27
  • 21
  • @androidplusios.design no worried found your question in google so though may be worth answering it. Correct sentence should be "jtds works fine ... sometimes" though :P – JJ Roman Sep 21 '15 at 20:55