0% found this document useful (0 votes)
15 views3 pages

JDBC Connection and Data Retrieval Example

This Java code connects to a MySQL database, executes a SQL query to retrieve employee data from a table, and iterates through the result set to display the records. It registers the JDBC driver, opens a database connection, creates a statement, executes a query to retrieve data, extracts data from the result set by moving the cursor, and closes the resources.

Uploaded by

Shobha Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

JDBC Connection and Data Retrieval Example

This Java code connects to a MySQL database, executes a SQL query to retrieve employee data from a table, and iterates through the result set to display the records. It registers the JDBC driver, opens a database connection, creates a statement, executes a query to retrieve data, extracts data from the result set by moving the cursor, and closes the resources.

Uploaded by

Shobha Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

import [Link].

*;

public class JDBCExample {


// JDBC driver name and database URL
static final String JDBC_DRIVER = "[Link]";
static final String DB_URL = "jdbc:mysql://localhost/EMP";

// Database credentials
static final String USER = "username";
static final String PASS = "password";

public static void main(String[] args) {


Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
[Link]("[Link]");

//STEP 3: Open a connection


[Link]("Connecting to database...");
conn = [Link](DB_URL,USER,PASS);

//STEP 4: Execute a query to create statment with


// required arguments for RS example.
[Link]("Creating statement...");
stmt = [Link](
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = [Link](sql);

// Move cursor to the last row.


[Link]("Moving cursor to the last...");
[Link]();

//STEP 5: Extract data from result set


[Link]("Displaying record...");
//Retrieve by column name
int id = [Link]("id");
int age = [Link]("age");
String first = [Link]("first");
String last = [Link]("last");

//Display values
[Link]("ID: " + id);
[Link](", Age: " + age);
[Link](", First: " + first);
[Link](", Last: " + last);

// Move cursor to the first row.


[Link]("Moving cursor to the first row...");
[Link]();
//STEP 6: Extract data from result set
[Link]("Displaying record...");
//Retrieve by column name
id = [Link]("id");
age = [Link]("age");
first = [Link]("first");
last = [Link]("last");

//Display values
[Link]("ID: " + id);
[Link](", Age: " + age);
[Link](", First: " + first);
[Link](", Last: " + last);
// Move cursor to the first row.

[Link]("Moving cursor to the next row...");


[Link]();

//STEP 7: Extract data from result set


[Link]("Displaying record...");
id = [Link]("id");
age = [Link]("age");
first = [Link]("first");
last = [Link]("last");

//Display values
[Link]("ID: " + id);
[Link](", Age: " + age);
[Link](", First: " + first);
[Link](", Last: " + last);

//STEP 8: Clean-up environment


[Link]();
[Link]();
[Link]();
}catch(SQLException se){
//Handle errors for JDBC
[Link]();
}catch(Exception e){
//Handle errors for [Link]
[Link]();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
[Link]();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
[Link]();
}catch(SQLException se){
[Link]();
}//end finally try
}//end try
[Link]("Goodbye!");
}//end main
}//end JDBCExample

You might also like