8 Nisan 2019 Pazartesi

JDBC ResultSet Arayüzü Sütun Indeksi İle Getter Metodları

Giriş
İndeks kullanan getter metodları sütun ismi (column name) kullanan metodlara göre daha performanslı. Açıklaması şöyle.
High-load performance testing showed that Hibernate’s approach of reading values from ResultSet by name to be its most limiting factor in scaling through-put.
İmzası şöyle
Object getObject(int columnIndex) throws SQLException;
public <T> T getObject(int columnIndex, Class<T> type) throws SQLException;
Object getObject(int columnIndex, java.util.Map<String,Class<?>> map) throws SQLException;
getArray metodu
İmzası şöyle
Array getArray(int columnIndex) throws SQLException;
getBigDecimal metodu
İmzası şöyle
BigDecimal getBigDecimal(int columnIndex) throws SQLException;
getBinaryStream metodu
İmzası şöyle
java.io.InputStream getBinaryStream(int columnIndex) throws SQLException;
Bu metod ile veri tabanından BINARY, VARBINARY, LONGVARBINARY gibi JDBC tipine sahip sütunlar okunabilir. Ancak bu tür sütunların kullanıldığını görmedim. Bunlar yerine daha çok BLOB kullanılıyor. getBlob () yerine, stream tabanlı metodların kullanılmasının çok fazla veri çekerken daha iyi olabileceği yazılı. Şöyle yaparız.
InputStream is = rs.getBinaryStream (1);
...
is.close ();
getBlob metodu
ResultSet blob'a getBlob (index) veya getBlob ("ColumnName") şeklinde erişime izin verir. Şöyle yaparız.
Blob b= rs.getBlob (1);
byte byteArray[]= b.getBytes(0,(int)b.length()); 
FileOutputStream fout = new FileOutputStream("d:\\image.jpg");  
fout.write (byteArray);  
fout.close ();
getDate metodu
java.sql.Date getDate(int columnIndex, Calendar cal) throws SQLException;
getObject metodu
Metodun içi şuna benzer.
public Object getObject(int index) throws SQLException {
  checkValidRow();
  checkValidIndex(index);
  return currentRow[index - 1];
}
Örnek
Açıklaması şöyle. Bu yöntem örneğin SQL sonucunu bir terminalde göstermek için kullanılabilir.
This approach does not use any container classes, it transmits the data as is. Then the client side code converts the data into the objects.
Şöyle yaparız
@Repository
public class JdbcTableRepository {

  @Autowired
  DataSource ds;
 
  public List loadAs2DArray(String sql) throws SQLException {

    try (Connection con = ds.getConnection(); 
         Statement st = con.createStatement(); 
         ResultSet rs = st.executeQuery(sql)) {
      ResultSetMetaData md = rs.getMetaData();
      int colCount = md.getColumnCount();

      List rows = new LinkedList<>();
      List row = new LinkedList<>();
      for (int c = 1; c <= colCount; c++) {
        row.add(md.getColumnLabel(c));
      }
      rows.add(row);
      while (rs.next()) {
        row = new LinkedList<>();
        for (int c = 1; c <= colCount; c++) {
          row.add(rs.getObject(c));
        }
        rows.add(row);
      }
      return rows;
    }
  }
}
getTime metodu
İmzası şöyle
java.sql.Time getTime(int columnIndex, Calendar cal) throws SQLException;
getTimeStamp metodu
İmzası şöyle
java.sql.Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException;
getURL metodu
İmzası şöyle
java.net.URL getURL(int columnIndex) throws SQLException;





Hiç yorum yok:

Yorum Gönder