Spring Ders15 - Database ( Veritabanı ) - ResultSetExtractor
ResultSetExtractor arayüzünin (interface) query metodu ile veritabanından bilgiler çekilebilir.
query metodunun signatürü (imzası) aşağıdaki gibidir.
Bununla ilgili bir örnek yapalım.
Main.java
package test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); BilgiIslem bilgici = (BilgiIslem) context.getBean("bilgiislem"); List <Personel> list = bilgici.getPersoneller(); System.out.println("ID\tAD\tMAAS\n"); for(Personel p:list){ System.out.println(p.getId()+"\t"+p.getAd()+"\t"+p.getMaas()); } } }
Personel.java
package test; public class Personel { private int id; private int maas; private String ad; public Personel() { } public Personel(int id, String ad, int maas) { this.id = id; this.ad = ad; this.maas = maas; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getMaas() { return maas; } public void setMaas(int maas) { this.maas = maas; } public String getAd() { return ad; } public void setAd(String ad) { this.ad = ad; } }
BilgiIslem.java
package test; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.ResultSetExtractor; public class BilgiIslem { private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public List<Personel> getPersoneller() { return jdbcTemplate.query("select * from bilgiislem", new ResultSetExtractor<List<Personel>>() { @Override public List<Personel> extractData(ResultSet rs) throws SQLException, DataAccessException { List<Personel> list = new ArrayList<Personel>(); while (rs.next()) { Personel p = new Personel(); p.setId(rs.getInt(1)); p.setAd(rs.getString(2)); p.setMaas(rs.getInt(3)); list.add(p); } return list; } }); } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="system" /> <property name="password" value="****" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="ds"></property> </bean> <bean id="bilgiislem" class="test.BilgiIslem"> <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean> </beans>
test.sql
create table bilgiislem( id int, ad varchar(255), maas int );
Yorumlar
Yorum Gönder