1. Download JDBC jar file from Postgresql.org .
2. place this file at PostgreSQL lib location.
cp -r postgresql-9.1-903.jdbc3.jar /opt/PostgreSQL/9.1/lib/
Execution of Java Sample program.
a. create table and insert sample lines.
create table av_test(id int).
insert into av_test values(1),(2),(3);
b.create sample JavaTest.java program
=================================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JavaTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5222/postgres", "postgres", "postgres");
PreparedStatement ps = con.prepareStatement("SELECT id FROM PUBLIC.AV_TEST");
System.out.println("Statement output " + ps.toString());
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.println("Done!!!" +rs.getString("id"));
}
}
}
==============================
c.Execution.
[root@localhost java_test]# javac JavaTest.java
[root@localhost java_test]# java -cp :/opt/PostgreSQL/9.1/lib/postgresql-9.1-903.jdbc3.jar JavaTest
Statement output SELECT id FROM PUBLIC.AV_TEST
Done!!!1
Done!!!2
Done!!!3
Done!!!4
[root@localhost java_test]#
No comments:
Post a Comment