Making database connections to PostgreSQL using Java
As this book is intended to give you the basics, we will start with simple Java code that retrieves data from the database. For the Java code, we should have Java installed on the machine, and we need a PosgreSQL java driver to connect it to the database.
Here is the sample code:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; public class JavaSelect { public static void main(String[] args) throws ClassNotFoundException, SQLException{ Class.forName("org.postgresql.Driver"); Connection con = DriverManager.getConnection ("jdbc:postgresql://ip:port/dbname","user","password"); Statement stmt = con.createStatement(); //Queries ResultSet rs = stmt.executeQuery("select * from table"); while (rs.next()) { System.out.println(rs.getString...