Spring Ders14 - Database ( Veritabanı ) - PreparedStatement
PreparedStatement ( Hazırlanmış Bildiri) ile JdbcTemplate şablonu üzerinde parametrelendirmiş sorguyu execute() metodu ile çalıştırabiliriz.
Aşağıda bu metodun signatürü (imzası) verilmiştir.
Main.java
package test; 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"); Personel p1 = new Personel(1, "enes", 9999); bilgici.kaydet(p1); } }
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.PreparedStatement; import java.sql.SQLException; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCallback; public class BilgiIslem { private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public boolean kaydet(Personel p) { String sorgu = "insert into bilgiislem values(?,?,?)"; return jdbcTemplate.execute(sorgu, new PreparedStatementCallback<Boolean>() { @Override public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setInt(1, p.getId()); ps.setString(2, p.getAd()); ps.setInt(3, p.getMaas()); return ps.execute(); } }); } }
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