Before you ask, yes I have looked at other stack overflow questions about this and youtube and all that but nothing worked, hence why I'm here.
Connector Class:
public class Connector
{
String url = "jdbc:jtds:sqlserver://ip:1433/database";
String username = "sa";
String password = "password";
public Connection getConnection()
{
try
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Class.forName("net.sourceforge.jtds.jdbc.Driver");
return DriverManager.getConnection(url, username, password);
}
catch (SQLException e)
{
throw new IllegalStateException("Cannot connect the database!", e);
}
}
}
MainActivity:
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.changeClass).setOnClickListener(v -> startActivity(new Intent(MainActivity.this, MainActivity2.class)));
findViewById(R.id.test).setOnClickListener(v -> testMain("name"));
}
public void testMain(String type)
{
try
{
Connector connector = new Connector();
Connection connection = connector.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
while(resultSet.next())
{
System.out.println("DB ENTRY: " + resultSet.getString(type));
}
connection.close();
statement.close();
resultSet.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
MainActivity2:
public class MainActivity2 extends AppCompatActivity { } // Yes it's meant to be empty
Gradle:
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 21
targetSdkVersion 30
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.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation files('C:\\Users\\ME\\Desktop\\jtds-1.3.1.jar')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
MainActivity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/changeClass"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="GO TO MAIN 2"
android:textSize="20sp"/>
<Button
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="PRINT DATABASE ENTRIES"
android:textSize="20sp"/>
</LinearLayout>
MainActivity2 layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context=".MainActivity2">
</LinearLayout>
Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_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.MyApplication">
<activity android:name=".MainActivity2"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And last but not least the error I've been trying to get rid off for the past three days:
E/System: Unable to open zip file: /data/app/com.example.myapplication-ljbCB-NPgZ9vuEvY-swqnw==/base.apk
E/System: java.io.FileNotFoundException: File doesn't exist: /data/app/com.example.myapplication-ljbCB-NPgZ9vuEvY-swqnw==/base.apk
at java.util.zip.ZipFile.<init>(ZipFile.java:212)
at java.util.zip.ZipFile.<init>(ZipFile.java:149)
at java.util.jar.JarFile.<init>(JarFile.java:160)
at java.util.jar.JarFile.<init>(JarFile.java:97)
at libcore.io.ClassPathURLStreamHandler.<init>(ClassPathURLStreamHandler.java:47)
at dalvik.system.DexPathList$Element.maybeInit(DexPathList.java:655)
at dalvik.system.DexPathList$Element.findResource(DexPathList.java:682)
at dalvik.system.DexPathList.findResources(DexPathList.java:506)
at dalvik.system.BaseDexClassLoader.findResources(BaseDexClassLoader.java:149)
at java.lang.ClassLoader.getResources(ClassLoader.java:839)
at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:349)
at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:402)
at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:488)
at java.sql.DriverManager$2.run(DriverManager.java:508)
at java.sql.DriverManager$2.run(DriverManager.java:490)
at java.security.AccessController.doPrivileged(AccessController.java:43)
at java.sql.DriverManager.loadInitialDrivers(DriverManager.java:489)
at java.sql.DriverManager.<clinit>(DriverManager.java:104)
at java.sql.DriverManager.registerDriver(Unknown Source:0)
at net.sourceforge.jtds.jdbc.Driver.<clinit>(Driver.java:125)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:453)
at java.lang.Class.forName(Class.java:378)
at com.example.myapplication.Connector.getConnection(Connector.java:21)
at com.example.myapplication.MainActivity.testMain(MainActivity.java:33)
at com.example.myapplication.MainActivity.lambda$onCreate$1$MainActivity(MainActivity.java:25)
at com.example.myapplication.-$$Lambda$MainActivity$u8Ljplxu6sIH-Yo1mp2RMh134AY.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:6294)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
W/System.err: java.lang.RuntimeException: No message resource found for message property prop.servertype
at net.sourceforge.jtds.jdbc.Messages.get(Messages.java:120)
at net.sourceforge.jtds.jdbc.Messages.get(Messages.java:67)
at net.sourceforge.jtds.jdbc.Driver.parseURL(Driver.java:374)
at net.sourceforge.jtds.jdbc.Driver.setupConnectProperties(Driver.java:239)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:182)
at java.sql.DriverManager.getConnection(DriverManager.java:569)
W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:219)
at com.example.myapplication.Connector.getConnection(Connector.java:22)
at com.example.myapplication.MainActivity.testMain(MainActivity.java:33)
at com.example.myapplication.MainActivity.lambda$onCreate$1$MainActivity(MainActivity.java:25)
at com.example.myapplication.-$$Lambda$MainActivity$u8Ljplxu6sIH- Yo1mp2RMh134AY.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:6294)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Things to note:
- Added the jar (jtds-1.3.1.jar) to the classpath
- Replaced the Gradle just in case prev was corrupted
- Changed the jtds jar in case prev was corrupted
- I also tried completely uninstalling android studio and Gradle and reinstalled it and let AS reinstall Gradle again same error
- I'm using Microsoft SQL Database 2014
So I made this little project with the three classes as shown above to observe what happens on a smaller scale, cause this problem comes from another one of my projects that has like 15 classes and so on.
So the gist is this. Say I just run my code, I have two buttons in my MainActivity, if I click the button which just reads some data from the DB, works like a dream, the other button takes me to MainActivity2 which is empty.
If while the app is running, if I move to another activity like MainActivity2 and stay in it or come back doesn't matter (as long as while the app was running I changed activities) then close the app and change anything in my code, and I mean ANYTHING, like add an empty line, on recompile it throws the above error.
However, if I don't move to another activity in the app before closing it and change something in the code on recompile it throws no error. It appears the connection to the DB doesn't like me moving to another activity, I dunno.
Am new to app development and databases and stuff but I have no clue on this error or how to solve it and any help is greatly appreciated, if there are any other files you'd like to see lemme know.