Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

256
Views
insert record in android studio (kotlin) using postgresql

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

9 months ago · Santiago Trujillo
2 answers
Answer question

0

If you're looking for documentation:

This is KotliQuery, very useful and simple to use. For example, to execute an insert statement, you can do it with:

val insertQuery: String = "insert into members (name,  created_at) values (?, ?)"
session.run(queryOf(insertQuery, "Alice", Date()).asUpdate) 
9 months ago · Santiago Trujillo Report

0

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

9 months ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.