A simple JDBC application sample code
By: Ramlak
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();
}
}
}
Archived Comments
1. I need simple inventory program with database connectivity in eclipse
View Tutorial By: deep at 2016-02-17 11:16:25
2. this code runs permanently!!! created by diko(Turkey)
public void mysql() {
View Tutorial By: ali at 2013-08-12 07:56:01
3. sir i need some simple example of jdbc i cannot understand this..
View Tutorial By: Velkumar.s at 2013-05-15 07:08:37
4. This is very simple code and very much understandable. Exclusively try, catch block is easy to progr
View Tutorial By: Prem Anand at 2013-05-04 11:21:16
5. simple java program
View Tutorial By: dhaval at 2013-04-18 09:48:31
6. Need code fore Data Insert query.........
View Tutorial By: jfdsf at 2013-03-25 13:00:38
7. semicolon(*;) is missed in first line..con .close() method is not possible for this program
View Tutorial By: Reddymalla Babu Sircilla at 2013-02-22 09:01:29
8. wt about line of 35 {e.printStackTrace();} i don't know plz tell me abt yhis line
Re
View Tutorial By: Reddymalla Babu Sircilla at 2013-02-22 08:38:30
9. It is very help full to me and very simple understandable program Method then comments are great he
View Tutorial By: Reddymalla Babu Sircilla at 2013-02-22 08:28:22
10. one closing bracket ')' is required at while loop in printing statement.
View Tutorial By: varsha Tambe at 2013-01-07 09:34:37
11. my question is how do this program connect to my sql 5.5?
and eclipse and netbeans?
View Tutorial By: ravisskit at 2012-10-08 12:00:18
12. Thanks sir i am understand to jdbc but who will run this program on MS-dos. please give me provide t
View Tutorial By: Ranjeet singh at 2012-05-04 07:40:29
13. Sir, I need to use MS Access as a database in java. My doubt is, whether I need to connect the datab
View Tutorial By: John Moses at 2012-03-31 00:20:38
14. nice program for data retrieving...can i get simple code for manipulating the data as well...
View Tutorial By: prem at 2012-02-13 10:16:22
15. Your program contains only data retrieval command.Use of Data Manipulation Command may enhance your
View Tutorial By: Govindarajan at 2012-02-08 01:02:01
16. Thank for simple and nice code. Pls,
1. line 8 "Change the connection string according
View Tutorial By: Sherif at 2011-12-26 14:12:12
17. Sir. Please give an example which is small and understand by java program. Please i want it .am not
View Tutorial By: mogesie gedamu at 2011-12-19 12:55:33
18. sir i need some simple example of jdbc i cannot understand this
View Tutorial By: sumit at 2011-11-26 18:02:14
19. sir, am doing my mca project in java based on network security, i need some java codings ,help me.
View Tutorial By: saravanapriya at 2011-11-24 03:25:06
20. excellent and superb , as a beginner i came to understand clearly
thanks alot
View Tutorial By: shantha at 2011-09-30 04:10:33
21. I NEED THE SAMPLE PROGRAM FOR DATA BASE CONNECTIVITY USING JAVA SCRIPT,MS ACCESS.PLS HELP ME
View Tutorial By: SUN at 2011-09-02 01:48:30
22. Sir. Please give an example which is small and understand by java beginer like me. Please i want it
View Tutorial By: Rafik at 2011-08-17 19:45:04
23. In line No. 23, change stmd to stmt.
ResultSet rs = stmd.executeQuery("select movien
View Tutorial By: Rishi Raj at 2011-07-08 13:50:11
24. nice one to understand the basic steps of jdbc....
View Tutorial By: trishna at 2011-06-10 00:04:21
25. nice one to understand the basic steps of jdbc....
View Tutorial By: trishna at 2011-06-09 23:43:03
26. nice one to understand the basic steps of jdbs....
View Tutorial By: trishna at 2011-06-09 23:42:51
27. Thanks it helped to understand but error occurred
View Tutorial By: Maya at 2010-11-17 22:08:10
28. if u want to work with jdbc or hibernate it's better to use shine enterprise pattern it's easier and
View Tutorial By: omid at 2010-07-02 03:07:17
29. Simple and effective example
View Tutorial By: Anand at 2009-02-05 00:18:10
30. Thnx Ramlak,but be ware about syntax errors.
View Tutorial By: Saber at 2008-11-03 07:45:23
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
TEXT datatype SPLIT in MSSQL - to solve the 8000 limit set by varchar
What is Referential Integrity in databases?
Handling CSV in Stored Procedures
java.lang.NoClassDefFoundError and java.lang.NoSuchMethodError
Calling a Stored Procedure from JDBC in Java
setSavepoint and releaseSavepoint Example in Java
PreparedStatement Example in Java
Creating Database Tables Using ANT
Using the DriverManager Class vs Using a DataSource Object for a connection
Stored Procedures example in SQL