Skip to content

Nihvel/Java-class-for-Postgresql-Database

Repository files navigation

Java

My class for Database is easy to import in your project.

  1. Add the " pgjdbc-core-parent-1.0.8.jar " library, in your project.

  2. Edit your " pom.xml " file to load the new library:

org.postgresql pgjdbc-core-parent 1.0.8
  1. Change default url, user and password variables to match your connection

3.1 UPDATE- If you want to use PgPass, check out the DatabasePgPass class

  1. Call it from the void main():

4.1 Establish a connection: Database db = new Database();

4.1.1 For the DatabasePgPass: Database db = new Database(host, port, db, user);

4.2 Select: List result = db.arraySelect("SELECT name FROM user WHERE id='"+id_user+"' "); if ( result.get(0).equals(User_id) ){ ... } /* Use the FOR to assign each value of the list to a variable */

4.3 UPSERT: /* Postgres does not have a Upsert function, you'll need to add to your database as a stored procedure read my " add_stored_procedure_in_postgres " to know how to do it */ String add_user = "SELECT upsert_user('"+User+"','"+Email+"','"+id_user+"'); "; db.runSql(add_user);

4.4 UPDATE: String update_user = "UPDATE user SET email='"+Email+"', WHERE id = '"+id_user+"'; "; db.updateSql(update_user);

That's all folks!

About

You can import this class to your Java project

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages