A simple JDBC application sample code

By: Ramlak Printer Friendly Format    


The basic process for a single data retrieval operation using JDBC would be as follows.

  • a JDBC driver would be loaded;
  • a database Connection object would be created from using the DriverManager (using the database driver loaded in the first step);
  • a Statement object would be created using the Connection object;
  • a SQL Select statement would be executed using the Statement object, and a ResultSet would be returned;
  • the ResultSet would be used to step through (or iterate through) the rows returned and examine the data.

The following JDBC code sample demonstrates this sequence of calls.

JDBCSample.java

import java.sql.*;
 
public class JDBCSample {
 
public static void main( String args[]) {
 
String connectionURL = "jdbc:postgresql://localhost:5432/movies;user=java;password=samples";
// Change the connection string according to your db, ip, username and password
 
try {
 
    // Load the Driver class.
    Class.forName("org.postgresql.Driver");
    // If you are using any other database then load the right driver here.
 
    //Create the connection using the static getConnection method
    Connection con = DriverManager.getConnection (connectionURL);
 
    //Create a Statement class to execute the SQL statement
    Statement stmt = con.createStatement();
 
    //Execute the SQL statement and get the results in a Resultset
    ResultSet rs = stmt.executeQuery("select moviename, releasedate from movies");
 
    // Iterate through the ResultSet, displaying two values
    // for each row using the getString method
 
    while (rs.next())
        System.out.println("Name= " + rs.getString("moviename") + " Date= " + rs.getString("releasedate"));
}
catch (SQLException e) {
    e.printStackTrace();
}
catch (Exception e) {
    e.printStackTrace();
}
finally {
    // Close the connection
    con.close();
}
}
}


Most Viewed Articles (in JDBC )

Latest Articles (in JDBC)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Thnx Ramlak,but be ware about syntax errors.
View Tutorial          By: Saber at 2008-11-03 07:45:23

2. Simple and effective example
View Tutorial          By: Anand at 2009-02-05 00:18:10

3. if u want to work with jdbc or hibernate it's bett
View Tutorial          By: omid at 2010-07-02 03:07:17

4. Thanks it helped to understand but error occurred
View Tutorial          By: Maya at 2010-11-17 22:08:10

5. nice one to understand the basic steps of jdbs....
View Tutorial          By: trishna at 2011-06-09 23:42:51

6. nice one to understand the basic steps of jdbc....
View Tutorial          By: trishna at 2011-06-09 23:43:03

7. nice one to understand the basic steps of jdbc....
View Tutorial          By: trishna at 2011-06-10 00:04:21

8. In line No. 23, change stmd to stmt.
Re

View Tutorial          By: Rishi Raj at 2011-07-08 13:50:11

9. Sir. Please give an example which is small and und
View Tutorial          By: Rafik at 2011-08-17 19:45:04

10. I NEED THE SAMPLE PROGRAM FOR DATA BASE CONNECTIVI
View Tutorial          By: SUN at 2011-09-02 01:48:30

11. excellent and superb , as a beginner i came to und
View Tutorial          By: shantha at 2011-09-30 04:10:33

12. sir, am doing my mca project in java based on netw
View Tutorial          By: saravanapriya at 2011-11-24 03:25:06

13. sir i need some simple example of jdbc i cannot un
View Tutorial          By: sumit at 2011-11-26 18:02:14

14. Sir. Please give an example which is small and und
View Tutorial          By: mogesie gedamu at 2011-12-19 12:55:33

15. Thank for simple and nice code. Pls,
1. li

View Tutorial          By: Sherif at 2011-12-26 14:12:12

16. Your program contains only data retrieval command.
View Tutorial          By: Govindarajan at 2012-02-08 01:02:01

17. nice program for data retrieving...can i get simpl
View Tutorial          By: prem at 2012-02-13 10:16:22

18. Sir, I need to use MS Access as a database in java
View Tutorial          By: John Moses at 2012-03-31 00:20:38

19. Thanks sir i am understand to jdbc but who will ru
View Tutorial          By: Ranjeet singh at 2012-05-04 07:40:29

20. my question is how do this program connect to my
View Tutorial          By: ravisskit at 2012-10-08 12:00:18

21. one closing bracket ')' is required at while loop
View Tutorial          By: varsha Tambe at 2013-01-07 09:34:37

22. It is very help full to me and very simple underst
View Tutorial          By: Reddymalla Babu Sircilla at 2013-02-22 08:28:22

23. wt about line of 35 {e.printStackTrace();} i don'
View Tutorial          By: Reddymalla Babu Sircilla at 2013-02-22 08:38:30

24. semicolon(*;) is missed in first line..con .close(
View Tutorial          By: Reddymalla Babu Sircilla at 2013-02-22 09:01:29

25. Need code fore Data Insert query.........
View Tutorial          By: jfdsf at 2013-03-25 13:00:38

26. simple java program
View Tutorial          By: dhaval at 2013-04-18 09:48:31

27. This is very simple code and very much understanda
View Tutorial          By: Prem Anand at 2013-05-04 11:21:16

28. sir i need some simple example of jdbc i cannot un
View Tutorial          By: Velkumar.s at 2013-05-15 07:08:37

29. this code runs permanently!!! created by diko(Turk
View Tutorial          By: ali at 2013-08-12 07:56:01

30. I need simple inventory program with database conn
View Tutorial          By: deep at 2016-02-17 11:16:25