//STEP 1. Import required packages
import java.sql.* ;
public class Main{
// JDBC driver name and database URL
static final String JDBC_DRIVER
= "com.mysql.jdbc.Driver" ; 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
) { try {
//STEP 2: Register JDBC driver
Class .forName ( "com.mysql.jdbc.Driver" ) ;
//STEP 3: Open a connection
System .
out .
println ( "Connecting to database..." ) ;
//STEP 4: Execute a query to create statment with
// required arguments for RS example.
System .
out .
println ( "Creating statement..." ) ; stmt = conn.createStatement (
sql = "SELECT id, first, last, age FROM Employees" ;
// Move cursor to the last row.
System .
out .
println ( "Moving cursor to the last..." ) ; rs.last ( ) ;
//STEP 5: Extract data from result set
System .
out .
println ( "Displaying record..." ) ; //Retrieve by column name
int id = rs.getInt ( "id" ) ;
int age = rs.getInt ( "age" ) ;
String first
= rs.
getString ( "first" ) ; String last
= rs.
getString ( "last" ) ;
//Display values
System .
out .
print ( "ID: " + id
) ; System .
out .
print ( ", Age: " + age
) ; System .
out .
print ( ", First: " + first
) ; System .
out .
println ( ", Last: " + last
) ;
// Move cursor to the first row.
System .
out .
println ( "Moving cursor to the first row..." ) ; rs.first ( ) ;
//STEP 6: Extract data from result set
System .
out .
println ( "Displaying record..." ) ; //Retrieve by column name
id = rs.getInt ( "id" ) ;
age = rs.getInt ( "age" ) ;
first = rs.getString ( "first" ) ;
last = rs.getString ( "last" ) ;
//Display values
System .
out .
print ( "ID: " + id
) ; System .
out .
print ( ", Age: " + age
) ; System .
out .
print ( ", First: " + first
) ; System .
out .
println ( ", Last: " + last
) ; // Move cursor to the first row.
System .
out .
println ( "Moving cursor to the next row..." ) ; rs.next ( ) ;
//STEP 7: Extract data from result set
System .
out .
println ( "Displaying record..." ) ; id = rs.getInt ( "id" ) ;
age = rs.getInt ( "age" ) ;
first = rs.getString ( "first" ) ;
last = rs.getString ( "last" ) ;
//Display values
System .
out .
print ( "ID: " + id
) ; System .
out .
print ( ", Age: " + age
) ; System .
out .
print ( ", First: " + first
) ; System .
out .
println ( ", Last: " + last
) ;
//STEP 8: Clean-up environment
rs.close ( ) ;
stmt.close ( ) ;
conn.close ( ) ;
//Handle errors for JDBC
se.printStackTrace ( ) ;
//Handle errors for Class.forName
e.printStackTrace ( ) ;
} finally {
//finally block used to close resources
try {
if ( stmt!= null )
stmt.close ( ) ;
} // nothing we can do
try {
if ( conn!= null )
conn.close ( ) ;
se.printStackTrace ( ) ;
} //end finally try
} //end try
System .
out .
println ( "Goodbye!" ) ; } //end main
} //end JDBCExample
Ly9TVEVQIDEuIEltcG9ydCByZXF1aXJlZCBwYWNrYWdlcwppbXBvcnQgamF2YS5zcWwuKjsKCnB1YmxpYyBjbGFzcyBNYWluewogICAvLyBKREJDIGRyaXZlciBuYW1lIGFuZCBkYXRhYmFzZSBVUkwKICAgc3RhdGljIGZpbmFsIFN0cmluZyBKREJDX0RSSVZFUiA9ICJjb20ubXlzcWwuamRiYy5Ecml2ZXIiOyAgCiAgIHN0YXRpYyBmaW5hbCBTdHJpbmcgREJfVVJMID0gImpkYmM6bXlzcWw6Ly9sb2NhbGhvc3QvRU1QIjsKCiAgIC8vICBEYXRhYmFzZSBjcmVkZW50aWFscwogICBzdGF0aWMgZmluYWwgU3RyaW5nIFVTRVIgPSAidXNlcm5hbWUiOwogICBzdGF0aWMgZmluYWwgU3RyaW5nIFBBU1MgPSAicGFzc3dvcmQiOwogICAKcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICBDb25uZWN0aW9uIGNvbm4gPSBudWxsOwogICBTdGF0ZW1lbnQgc3RtdCA9IG51bGw7CiAgIHRyeXsKICAgICAgLy9TVEVQIDI6IFJlZ2lzdGVyIEpEQkMgZHJpdmVyCiAgICAgIENsYXNzLmZvck5hbWUoImNvbS5teXNxbC5qZGJjLkRyaXZlciIpOwoKICAgICAgLy9TVEVQIDM6IE9wZW4gYSBjb25uZWN0aW9uCiAgICAgIFN5c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