- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to connect to PostgreSQL database using a JDBC program?
PostgreSQL is an open source relational database management system (DBMS) developed by a worldwide team of volunteers. PostgreSQL is not controlled by any corporation or other private entity and the source code is available free of charge.
PostgreSQL runs on all major operating systems, including Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows. It supports text, images, sounds, and video, and includes programming interfaces for C / C++, Java, Perl, Python, Ruby, Tcl and Open Database Connectivity (ODBC).
Download the latest version of postgresql- from postgresql-jdbc repository.
Add downloaded jar file postgresql-(VERSION).jdbc.jar in your class path.
Example
Following JDBC program establishes connection to PostgreSQL database and creates a table in it.
import java.sql.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class PostgreSQLJDBC { public static void main( String args[] ) { Connection c = null; Statement stmt = null; try { Class.forName("org.postgresql.Driver"); c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "manisha", "123"); System.out.println("Connection established successfully"); stmt = c.createStatement(); String sql = "CREATE TABLE COMPANY " + "(ID INT PRIMARY KEY NOT NULL," + " NAME TEXT NOT NULL, " + " AGE INT NOT NULL, " + " ADDRESS CHAR(50), " + " SALARY REAL)"; stmt.executeUpdate(sql); stmt.close(); c.close(); } catch ( Exception e ) { System.err.println( e.getClass().getName()+": "+ e.getMessage() ); System.exit(0); } System.out.println("Table created successfully"); } }
Output
Connection established successfully Table created successfully
- Related Articles
- How to connect to Derby database using a JDBC program?
- How to connect to HSQLDB database using a JDBC program?
- How to connect to a MongoDB database using a JDBC program?
- How to connect to an SQLite database using a JDBC program?
- How to insert Timestamp value in a database using JDBC program?
- How to drop a database using JDBC API?
- How to insert images in Database using JDBC?
- How to connect Database in Python?
- How to insert a Python tuple in a PostgreSql database?
- How to create a database in MySQL using a JDBC API?
- How to create a table in a database using JDBC API?
- How to drop a table from a database using JDBC API?
- How to create a function in a database using JDBC API?
- How to connect hibernate with MySQL Database?
- How to get all table names from a database using JDBC?
