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
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)
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