0

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.

Baguviks
  • 1
  • 3
  • 2
    Please [reconsider your use of JDBC on Android](https://stackoverflow.com/questions/15853367/jdbc-vs-web-service-for-android) – CommonsWare Apr 01 '23 at 23:07
  • Postgres doesn't run on Android. So it's impossible that a connection to `localhost` will ever work (when using Postgres) –  Apr 02 '23 at 08:04
  • I am sure that it runs. That's why JDBC exists – Baguviks Apr 02 '23 at 11:53
  • Also thanks for the link. Firstly, I asked to chat gpt and it recommended me jdbc, so I tried to use it but I may use web service since jdbc won't work – Baguviks Apr 02 '23 at 18:41

0 Answers0