Home » Java » java – Android Studio DriverManager GetConnection exception-Exceptionshub

java – Android Studio DriverManager GetConnection exception-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have been writing an app which I would like to connect to an SQL DB when a login button is pressed. Therefore, I would like the input details taken as the arguments for the connection.

I am using JTDS/JDBC to connect.

I have written the code in another Class that works fine. However, once I try and run it in the Login.java the code throws an exception on the line:

conn = DriverManager.getConnection(connString, username, password);

If someone would be able to help it would be much appreciated, Thanks.

Java Login Code:

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

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


public class Login extends Activity {

    EditText username, password;
    Button loginButton;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.login_screen);

        startLockTask();

        username = findViewById(R.id.enterUsername);
        password = findViewById(R.id.enterPassword);
        loginButton = findViewById(R.id.loginButton);


        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
    }

    public void login() {
        String username1 = username.getText().toString();
        String password1 = password.getText().toString();
        if (connect(username1, password1)) {

            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);


        } else {
            Toast.makeText(Login.this, "Invalid Username or Password", Toast.LENGTH_LONG).show();
        }
    }
    public boolean connect(String username1, String password1) {

        Log.i("Android", " MySQL Connect Example.");
        Connection conn = null;
        try {
            String driver = "net.sourceforge.jtds.jdbc.Driver";
            Class.forName(driver).newInstance();
            //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
            String connString = "jdbc:jtds:sqlserver://[ip]/[db];encrypt=true" + ";user=" + username1 + ";password=" + password1 + ";";

            conn = DriverManager.getConnection(connString, username1, password1);
            Log.w("Connection", "open");
            Statement stmt = conn.createStatement();

            he application, the use of this might not be contained here
            // or add another open for every time that it need to connect
            // connection class?
            conn.close();

            return true;

        }
        catch (Exception e) {
            System.err.println("Got an exception!");
            System.err.println(e.getMessage());

            return false;
        }

    }
}

the xml for the login screen:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/loginText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login" />

    <TextView
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Username:" />

    <EditText
        android:id="@+id/enterUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Username..." />

    <TextView
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Password:" />

    <EditText
        android:id="@+id/enterPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textVisiblePassword"
        android:hint="Password..." />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login" />
</LinearLayout>

Added Stacktrace

2020-02-18 10:25:11.617 21526-21526/com.dxcgradfair I/Android:  MySQL Connect Example.
2020-02-18 10:25:11.647 21526-21526/com.dxcgradfair W/System.err: android.os.NetworkOnMainThreadException
2020-02-18 10:25:11.648 21526-21526/com.dxcgradfair W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
2020-02-18 10:25:11.648 21526-21526/com.dxcgradfair W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:389)
2020-02-18 10:25:11.648 21526-21526/com.dxcgradfair W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
2020-02-18 10:25:11.649 21526-21526/com.dxcgradfair W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
2020-02-18 10:25:11.649 21526-21526/com.dxcgradfair W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
2020-02-18 10:25:11.649 21526-21526/com.dxcgradfair W/System.err:     at java.net.Socket.connect(Socket.java:621)
2020-02-18 10:25:11.649 21526-21526/com.dxcgradfair W/System.err:     at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
2020-02-18 10:25:11.651 21526-21526/com.dxcgradfair W/System.err:     at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)
2020-02-18 10:25:11.651 21526-21526/com.dxcgradfair W/System.err:     at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331)
2020-02-18 10:25:11.651 21526-21526/com.dxcgradfair W/System.err:     at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
2020-02-18 10:25:11.651 21526-21526/com.dxcgradfair W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:580)
2020-02-18 10:25:11.651 21526-21526/com.dxcgradfair W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:218)
2020-02-18 10:25:11.655 21526-21526/com.dxcgradfair W/System.err:     at com.dxcgradfair.MainActivity.connect(MainActivity.java:248)
2020-02-18 10:25:11.655 21526-21526/com.dxcgradfair W/System.err:     at com.dxcgradfair.Login.login(Login.java:47)
2020-02-18 10:25:11.655 21526-21526/com.dxcgradfair W/System.err:     at com.dxcgradfair.Login$1.onClick(Login.java:36)
2020-02-18 10:25:11.655 21526-21526/com.dxcgradfair W/System.err:     at android.view.View.performClick(View.java:7125)
2020-02-18 10:25:11.655 21526-21526/com.dxcgradfair W/System.err:     at android.view.View.performClickInternal(View.java:7102)
2020-02-18 10:25:11.656 21526-21526/com.dxcgradfair W/System.err:     at android.view.View.access$3500(View.java:801)
2020-02-18 10:25:11.656 21526-21526/com.dxcgradfair W/System.err:     at android.view.View$PerformClick.run(View.java:27336)
2020-02-18 10:25:11.656 21526-21526/com.dxcgradfair W/System.err:     at android.os.Handler.handleCallback(Handler.java:883)
2020-02-18 10:25:11.656 21526-21526/com.dxcgradfair W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:100)
2020-02-18 10:25:11.656 21526-21526/com.dxcgradfair W/System.err:     at android.os.Looper.loop(Looper.java:214)
2020-02-18 10:25:11.657 21526-21526/com.dxcgradfair W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7356)
2020-02-18 10:25:11.660 21526-21526/com.dxcgradfair W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2020-02-18 10:25:11.662 21526-21526/com.dxcgradfair W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
2020-02-18 10:25:11.663 21526-21526/com.dxcgradfair W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2020-02-18 10:25:11.663 21526-21526/com.dxcgradfair I/Android: Error with conneciton

Added log

2020-02-18 09:20:37.493 9429-9429/com.dxcgradfair W/System.err: Got an exception!
2020-02-18 09:20:37.493 9429-9429/com.dxcgradfair W/System.err: null

Note, I have tried other methods and had it set up so that if the incorrect password is entered it loads the next screen, and the correct doesnt. However, once the booleans are reversed the opposite does not happen and instead throws the exceptions.

How to&Answers: