Answer a question

Is there any documentation on how to insert or retrieve data from PostgreSQL to the android studio, I am using kotlin. and I am new in Android Studio. I already have this code in my MainActivity.kt and connectiondb.java

connectiondb.java

public class connectionDb {
    Connection connection=null;
    public Connection ConnectionDb(){
        try{
            Class.forName("org.postgresql.Driver");
            connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mytransactiondb", "postgres", "root");

        } catch (Exception err) {
            System.err.println(err.getMessage());
        }

        return connection;
    }
    protected void close_connection(Connection con)throws Exception{
        con.close();
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn_login.setOnClickListener{
            val objConnectionDb=connectionDb()
            objConnectionDb.connection

            Log.i("connectinDb()", "Connected")
        }
    }
}

Thanks in Advance

Answers

Eventhough I have not worked on PostreSQL with Android, got this documentation which says.

Below is the approach to insert query using PostreSQL for Android

PreparedStatement st=conn.prepareStatement(”INSERT INTO films (title) ”+”VALUES(?) ;”);
st.setString(1,”On Stranger Tides”);
int rows=st.executeUpdate();
st.close();

Go through this link for more details.

More insights over here

Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐