Monday, April 12, 2010

Java Database Connectivity (JDBC)

JDBC is Java Database Connectivity helps to write java applications using databases.

Steps to use JDBC in java applications as follows
  1. Load the JDBC driver.
  2. Define the connection URL.
  3. Establish the connection.
  4. Create a statement object.
  5. Execute a query or update.
  6. Process the results.
  7. Close the connection. 
try {
            Class.forName("driveClassName");
            Connection con = DriverManager.getConnection(
                    "jdbc:driveName:databaseName", "login", "password");

            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM Table");
            while (rs.next()) {
                int a = rs.getInt("column1");
                String b = rs.getString("column2");
                float c = rs.getFloat("column3");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            // close the connection
        }





No comments:

Post a Comment