-1

I have already downloaded jtds and also jdbc and even put under my libs I have tried the com.microsoft.sqlserver.jdbc.SQLServerDriver and also net.sourceforge.jtds.jdbc.Driver still not working. I also added both in my dependencies in my build.gradle but still not working and I even gave permission to the Android manifest for it

Here is my build.gradle file for the dependencies:

dependencies {

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    //implementation 'com.microsoft.sqlserver:mssql-jdbc:9.4.0.jre8'
    //implementation 'net.sourceforge.jtds:jtds:1.3.1'
    implementation files('libs/jtds-1.3.1.jar')
    implementation 'com.microsoft.sqlserver:mssql-jdbc:12.2.0.jre8'
}

Here is the AndroidManifest.xml:

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

    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-feature android:name="android.hardware.nfc" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.CanteenMeal"
        tools:targetApi="31">

        <activity
            android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>
        </activity>

        <activity
            android:name=".AdminActivity"
            android:exported="true">
            <!-- Define any necessary intent filters or other attributes for AdminActivity -->
        </activity>

        <activity
            android:name=".CanteenEmployeeActivity"
            android:exported="true">
            <!-- Define any necessary intent filters or other attributes for CanteenEmployeeActivity -->
        </activity>

        <!-- Other manifest entries -->

    </application>
</manifest>

This is my DatabaseHelper.java:

public class DatabaseHelper {

    private static final String DATABASE_URL = "jdbc:sqlserver://10.1.32.111:1433/PROD";
    private static final String DATABASE_USERNAME = "Sexy";
    private static final String DATABASE_PASSWORD = "sexy";

    // Login table constants
    private static final String TABLE_NAME = "users";
    private static final String COLUMN_USERNAME = "username";
    private static final String COLUMN_PASSWORD = "password";

    // Employee table constants
    private static final String TABLE_EMPLOYEE = "employee";
    private static final String COLUMN_EMPLOYEE_VISA = "visa";
    private static final String COLUMN_EMPLOYEE_NAME = "employeename";
    private static final String COLUMN_EMPLOYEE_BADGE_NUMBER = "badgenumber";

    // Report table constants
    private static final String TABLE_REPORT = "reports";
    private static final String COLUMN_REPORT_EMPLOYEE_VISA = "visa";
    private static final String COLUMN_REPORT_EMPLOYEE_NAME = "employeename";
    private static final String COLUMN_REPORT_EMPLOYEE_BADGE_NUMBER = "badgenumber";
    private static final String COLUMN_COLOUR = "Colour";
    private static final String COLUMN_DATE_TIME = "Date_Time";

    private Connection connection;
    private Context context;

    public DatabaseHelper(Context context) {
        this.context= context;

        try {
            // Load the SQL Server JDBC driver
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            // Connect to the database
            connection = DriverManager.getConnection(DATABASE_URL, DATABASE_USERNAME, DATABASE_PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

I am sure that it has something has to do with the getConnection where its not working can someone help me please

Here are the details of the SQL server

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 1
    "not working" is not very informative, tells us nothing of use, and you may be ignoring important information that is being generated when your code fails (and not passing it on to us). Please tell the details of your problem. How is it not working specifically? Are you seeing any exception stack traces? – Hovercraft Full Of Eels Jun 16 '23 at 02:22
  • the emulator is runnning but when I login its says wrong password which is an error message I was using SQL lite before this and this is my first time migrating to MS SQL server so this where im having the problem if I change back to "com.microsoft.sqlserver.jdbc.SQLServerDriver" its closing by itself where the logcat error is from the getConnection so there is somewhere wrong in the url but the ip address the name of the database and the username and password is all correct – Umendran Muniandy Jun 16 '23 at 02:24
  • 3
    Please reconsider using JDBC, [JDBC vs Web Service for Android](https://stackoverflow.com/q/15853367/295004) Also [edit your post](https://stackoverflow.com/posts/76486585/edit) with additional logcat details. – Morrison Chang Jun 16 '23 at 02:49
  • This is my Logcat error: at com.example.canteenmeal.DatabaseHelper.(DatabaseHelper.java:50) which is highlighting the getConnection – Umendran Muniandy Jun 16 '23 at 03:05
  • 1
    JDBC should NEVER be used on a client device – Gabe Sechan Jun 16 '23 at 03:17
  • but it should still work right doesn't matter if its security or network traffic etc – Umendran Muniandy Jun 16 '23 at 03:40
  • The full stack trace of your error is preferred. I would also state what tests you've done to check that the emulator can see the MS SQL Server. I would also check the error logs of the MS SQL Server if anything notable can be found. Comments should be used to help clarify/improve the question. OP should add info to post as stack trace and other info should be a part of the question. – Morrison Chang Jun 16 '23 at 03:52

1 Answers1

-1

I have found the solution where I had to use strict mode in the databasehelper here is the updated code:

public DatabaseHelper(Context context) {
    this.context= context;
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    try {
        // Load the SQL Server JDBC driver
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        // Connect to the database
        connection = DriverManager.getConnection(DATABASE_URL,DATABASE_USERNAME,DATABASE_PASSWORD);
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
}
Dale K
  • 25,246
  • 15
  • 42
  • 71