terça-feira, 17 de abril de 2012

17.04.2012 - JDBC - DAO Struture

Classe BD

package br.com.ipa.dao;

import java.sql.*;

public class BD {

   private static final String url = 
           "jdbc:derby://localhost:1527/ipa;user=admin;password=123";

   private static Connection getConnection() throws Exception {
      return DriverManager.getConnection(url);
   }

   public synchronized static int execute(String sql) throws Exception {
      Connection conn = BD.getConnection();
      Statement stmt = conn.createStatement();
      try {
         return stmt.executeUpdate(sql);
      } finally {
         stmt.close();
         conn.close();
      }
   }

   public synchronized static RecordSet executeQuery(String sql) throws Exception {
      Connection conn = BD.getConnection();
      Statement stmt = conn.createStatement();
      try {
         return new RecordSet(stmt.executeQuery(sql));
      } finally {
         stmt.close();
         conn.close();
      }
   }

   public static void main(String[] args) throws Exception {
      System.out.println(BD.getConnection());
   }
}

Classe RecordSet

package br.com.ipa.dao;

import java.sql.*;
import com.sun.rowset.CachedRowSetImpl;

public class RecordSet extends CachedRowSetImpl {

   public RecordSet() throws SQLException {
      super();
   }

   public RecordSet(ResultSet rs) throws SQLException {
      super();
      try {
         populate(rs);
      } finally {
         rs.close();
      }
   }
}

Nenhum comentário:

Postar um comentário