View RSS Feed

JDBC

JDBC Introduction

Rate this Entry
by , 04-27-2012 at 07:09 PM (931 Views)
The JDBC API is a Java API that can access any kind of tabular data, especially data stored in a Relational Database.
JDBC helps you to write Java applications that manage these three programming activities:
1. Connect to a data source, like a database
2. Send queries and update statements to the database
3. Retrieve and process the results received from the database in answer to your query
The following simple code fragment gives a simple example of these three steps:
Java Code:
public void connectToAndQueryDatabase(String username, String password) {

    Connection con = DriverManager.getConnection(
                         "jdbc:myDriver:myDatabase",
                         username,
                         password);

    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");

    while (rs.next()) {
        int x = rs.getInt("a");
        String s = rs.getString("b");
        float f = rs.getFloat("c");
    }
}

Submit "JDBC Introduction" to Facebook Submit "JDBC Introduction" to Digg Submit "JDBC Introduction" to del.icio.us Submit "JDBC Introduction" to StumbleUpon Submit "JDBC Introduction" to Google

Categories
Tutorial

Comments