Home > Net >  Android project can't find net.sourceforge.jtds.jdbc.Driver
Android project can't find net.sourceforge.jtds.jdbc.Driver

Time:05-16

I want to connect database to android project using android studio and sql server management studio. I add dependencie in build.gradle, but when I try to login into my application give me this error

W/System.err: java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:454)
        at java.lang.Class.forName(Class.java:379)
        at com.example.warehousewithstocks.MainActivity.login(MainActivity.java:45)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)

This is my MainActivity class

public class MainActivity extends AppCompatActivity {
    private String ip = "192.168.31.203";
    private String port = "1433";
    private String classs = "net.sourceforge.jtds.jdbc.Driver";
    private String db = "Warehouse";
    private String un = "mssqllogin";
    private String password = "mssqllogin$";

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

    public void login(View v) {
        EditText usernameUser = (EditText) findViewById(R.id.username);
        EditText passwordUser = (EditText) findViewById(R.id.password);

        String strUsernameUser = usernameUser.getText().toString();
        String strPasswordUser = passwordUser.getText().toString();


        Connection conn = null;
        String ConnURL = null;
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        try {
            Class.forName(classs);
            ConnURL = "jdbc:jtds:sqlserver://"   ip   ":"   port   ";"
                      "databaseName="   db   ";user="   un   ";password="
                      password   ";";
            conn = DriverManager.getConnection(ConnURL);
            Statement statement = conn.createStatement();
            ResultSet resultat = statement.executeQuery("select * from Accounts where Username_DB = '"   strUsernameUser   "' and Password_DB = '"   strPasswordUser   "'");
            String IdAccount = "";
            if (resultat.next()) {
                IdAccount = resultat.getString("ID_DB");
                System.out.println("zzz "   IdAccount);
            } else {
                System.out.println("zzz "   IdAccount);
            }
            resultat.close();

            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient"
    tools:context=".MainActivity">

    <ImageView
        android:id="@ id/imageView2"
        android:layout_width="120dp"
        android:layout_height="150dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="145dp"
        android:layout_marginEnd="146dp"
        android:layout_marginBottom="363dp"
        android:layout_centerHorizontal="true"
        android:contentDescription="TODO"
        app:srcCompat="@drawable/logo" />

    <EditText
        android:id="@ id/username"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="111dp"
        android:layout_marginEnd="110dp"
        android:layout_marginBottom="270dp"
        android:background="#11000000"
        android:ems="10"
        android:hint="Потребителско име"
        android:inputType="textPersonName"
        android:layout_centerHorizontal="true"
        android:textSize="16dp" />

    <EditText
        android:id="@ id/password"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="110dp"
        android:layout_marginEnd="111dp"
        android:layout_marginBottom="202dp"
        android:background="#11000000"
        android:ems="10"
        android:hint="Парола"
        android:layout_centerHorizontal="true"
        android:inputType="textPassword" />

    <Button
        android:id="@ id/button"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="54dp"
        android:layout_marginEnd="56dp"
        android:layout_marginBottom="105dp"
        android:background="@drawable/button_background"
        android:onClick="login"
        android:shadowRadius="@android:integer/config_mediumAnimTime"
        android:text="Вход" />




</RelativeLayout>

This is my build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 31

    defaultConfig {
        applicationId "com.example.warehousewithstocks"
        minSdkVersion 23
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4. '
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation files('net.sourceforge.jtds.jdbc.Driver')
}

And this is my AndoridManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.warehousewithstocks">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.WarehouseWithStocks">
        <activity android:name=".MainActivity" android:exported = "true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Can you tell me what I missing?

CodePudding user response:

You try to import wrong library. Try to add this in dependencies in your build.gradle - implementation 'net.sourceforge.jtds:jtds:1.3.1'

  • Related