blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

JDBC - simple application


JDBC singular connection using DriverManager - Stand alone Java application

package com.prodapps;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
 public static void main(String[] args) throws SQLException {
  Connection conn = null;
  Statement stmt = null;
  try {
   Class.forName("oracle.jdbc.OracleDriver");
   conn = DriverManager
   .getConnection("jdbc:oracle:thin:@localhost:1521:XE",
   "SYSTEM", "password");
   stmt = conn.createStatement();
   ResultSet rs = stmt
   .executeQuery("select * from products where id=1");
   while (rs.next()) {
    System.out.println((rs.getInt("id")));
    System.out.println((rs.getString("title")));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } finally {
   if (stmt != null)
   stmt.close();
   if (conn != null)
   conn.close();
  }
 }
}


Output:
1
ipad

Continue reading: JDBC web application - connections using DriverManager

No comments:

Post a Comment