Java + PostgreSQL JDBC: quick start

Trabla: Java + PostgreSQL JDBC: quick start


Solving:

1. Install Java Development Kit (JDK)

2. Install Eclipse

3. Install PostgreSQL on local machine !!!remember user login and password

4. Run Eclipse

5. Create new Java project in eclipse - File -> New -> Java Project
How to setup Java and PostgreSQL JDBC on Windows 7 - codingtrabla tutorial 1  



6. Type project name ( e.g. "PostgresqlJDBC" ) and click "Finish"  button 
How to setup Java and PostgreSQL JDBC on Windows 7 - codingtrabla tutorial 2



Result:
How to setup Java and PostgreSQL JDBC on Windows 7 - codingtrabla tutorial 3


7. Download JDBC driver .jar for your PostgreSQL
- download page - https://jdbc.postgresql.org/download.html

How to setup Java and PostgreSQL JDBC on Windows 7 - codingtrabla tutorial 4



8. Drug and drop downloaded .jar into Eclipse  "src" folder

How to setup Java and PostgreSQL JDBC on Windows 7 - codingtrabla tutorial 5
How to setup Java and PostgreSQL JDBC on Windows 7 - codingtrabla tutorial 6


9. Right mouse click on .jar in "src" folder , select "Build Path" -> "Add to Build Path"

How to setup Java and PostgreSQL JDBC on Windows 7 - codingtrabla tutorial 7
How to setup Java and PostgreSQL JDBC on Windows 7 - codingtrabla tutorial 8


10.  Create new class Java class - "File" -> "New" -> "Class"  and name it "PostgresqlJDBCTest"

11. Type following code into file PostgresqlJDBCTest.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class PostgresqlJDBCTest {
   
    private static String db_url         = "jdbc:postgresql://127.0.0.1:5432/postgres";
    private static String db_user         = "postgres";
    private static String db_password     = "superpassword123"; // Replace with yours
   
   
     public static void main(String[] args) throws ClassNotFoundException, SQLException { 
       
       
   
         Connection     lvConn     = null;
         Statement         lvStmt     = null;
       
         String         lvSql     = null;
       
         Class.forName("org.postgresql.Driver");
       
       
         // 1. Try to get connection
       
         lvConn = DriverManager.getConnection(
                 PostgresqlJDBCTest.db_url,
                 PostgresqlJDBCTest.db_user,
                 PostgresqlJDBCTest.db_password );
       
       
       
         // 2. Create statement
       
         lvStmt = lvConn.createStatement();
       
       
       
         // 3. Create table tbl_users
       
         lvSql = "CREATE TABLE tbl_users( id bigserial NOT NULL PRIMARY KEY , name varchar(200) NOT NULL UNIQUE, age int   )";
         lvStmt.executeUpdate( lvSql );
        
        
        
         // 4. Insert some data
       
         lvSql = "INSERT INTO  tbl_users ( name, age ) VALUES ( 'Alex', 25 ) ";
         lvStmt.executeUpdate( lvSql );
       
         lvSql = "INSERT INTO  tbl_users ( name, age ) VALUES ( 'John', 35 ) ";
         lvStmt.executeUpdate( lvSql );
       
         lvSql = "INSERT INTO  tbl_users ( name, age ) VALUES ( 'Lili', 22 ) ";
         lvStmt.executeUpdate( lvSql );
        
       
       
         // 5. Update some data
       
         lvSql = "UPDATE tbl_users SET age = 31 WHERE name = 'John' ";
         lvStmt.executeUpdate( lvSql );
       
       
       
         // 6. Delete some data
       
         lvSql = "DELETE FROM tbl_users WHERE name = 'Alex' ";
         lvStmt.executeUpdate( lvSql );
       
       
       
         // 7. Select data from table
       
         lvSql = "SELECT id, name, age FROM tbl_users "; 
       
         ResultSet rows =  lvStmt.executeQuery(lvSql);
       
         while (rows.next()) {
           
             System.out.println(
                     " id = " + rows.getInt("id") +
                     " name = " + rows.getString("name") +
                     " age = "+rows.getInt("age")
             );   
        }
       

       
        // 8. Close db connection
       
        lvConn.close();
     }
   

}


12. Run program

Result:

How to setup Java and PostgreSQL JDBC on Windows 7 - codingtrabla tutorial 9







No comments:

Post a Comment