I am trying to connect my Android Studio Project into PostgreSQL database. I am tryig to do that by JDBC Driver and I copied it to libs folder in my project. Also my build.grandle file contains that:
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'org.postgresql:postgresql:42.6.0'
implementation files('libs/postgresql-42.6.0.jar')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
However, I can't use the getConnection method. This is where I use it:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class PostgreSQLJDBC {
private static final String url = "jdbc:postgresql://localhost:5432/deneme";
private static final String user = "deneme";
private static final String password = "deneme123";
public List<User> findAll() {
List<User> users = new ArrayList<>();
try (Connection conn = PostgreSQLJDBC.getConnection(); //the error is here
PreparedStatement ps = conn.prepareStatement("SELECT * FROM users");
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUserName(rs.getString("userName"));
user.setMail(rs.getString("mail"));
users.add(user);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
return users;
}
}
I am newbie at both Android Studio and databases. So, it might be a very simple mistake :)
I checked to see if I installed the JDBC driver incorrectly but everything looks good. IDK if the problem is in code because I couldn't find anything about it.