S
S
Senture2019-02-16 19:47:25
Java
Senture, 2019-02-16 19:47:25

Android Studio connecting to MS SQL 2016 DB?

Hello!
I am trying to insert data into a MS SQL DB table via android, there is this code:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {

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

    protected void onClick_Info(View v)
    {
        Intent intent = new Intent(this, ActivityInfo.class);
        startActivity(intent);
    }

    protected void onClick_Store(View v)
    {
        String instanceName = "192.168.1.100\\SQLEXPRESS";
        String db = "Computer";
        String username = "Admin";
        String password = "Admin";
        String connectionUrl = "jdbc:sqlserver://%1$s;databaseName=%2$s;user=%3$s;password=%4$s;";
        String connectionString = String.format(connectionUrl, instanceName, db, username, password);

        try {
            Log.w("Run: ", "Connection open!");
            Connection con = DriverManager.getConnection(connectionString);
            Statement stmt = con.createStatement();
            stmt.execute("INSERT INTO Product(maker, model, type) VALUES ('D', 2305, 'PC')");
            stmt.close();
            con.close();
            Log.w("Run: ", "Connection close!");
        }
        catch (SQLException ex)
        {
            Log.w("SQLException error: ", ex.getMessage());
        }
        catch (Exception ex)
        {
            Log.w("Exception error: ", ex.getMessage());
        }
    }
}

The database and the android emulator are on the same PC. When calling the onClick_Store method, the try catch block catches the following exception: No suitable driver found for jdbc:sqlserver://192.168.1.100\SQLEXPRESS;databaseName=Computer;user=Admin;password=Admin;
And here's what's in the logs:
W/Run:: Connection open!
W/SQLException error:: No suitable driver found for jdbc:sqlserver://192.168.1.100\SQLEXPRESS;databaseName=Computer;user=Admin;password=Admin;
I have the module itself in the project:
5c683e48f0d72731485021.png
I'm new to Android Studio, so maybe I didn't connect something somewhere.
What's my mistake?
PS Thank you all so much!!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Senture, 2019-02-17
@Senture

Problem solved!
Like this:
In the project structure, I added a module to the project itself (I can’t explain in more detail because I don’t understand how it works myself)

M
Maxim Berezikov, 2019-05-06
@MaxBerezik

Hello, could you tell me materials on connection, interaction with MSSQL.... or write where I can talk to you in a lik?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question