کانال بله, جهت پشتیبانی و اطلاع رسانی کانال بله, جهت پشتیبانی و اطلاع رسانی
عضویت

آموزش PreparedStatement-Java Spring

PreparedStatement‌

مثال PreparedStatement در قالب Jdbc اسپرینگ

می¬توان با استفاده از قالب Jdbc در اسپرینگ، با کمک متد execute() در کلاس قالب Jdbc ، query های پارامتری(parameterized) را اجرا کرد. برای استفاده از query پارامتری، نمونه PreparedStatementCallback را در متد execute پاس می¬دهیم.


متد واسط PreparedStatementCallback

public T execute(String sql,PreparedStatementCallback< T >);    

واسط PreparedStatementCallback

تنها یک متد doInPreparedStatement را دارد. نحو این متد در ادامه آمده است.

public T doInPreparedStatement(PreparedStatement ps)throws SQLException, DataAccessException     

مثال کاربردPreparedStatement در اسپرینگ

فرض میکنیم جدول زیر را درون پایگاه داده Oracle10g ایجاد کرده اید.

  • create table employee(
  • id number(10),
  • name varchar2(100),
  • salary number(10)
  • );

Employee.java

این کلاس شامل سه خصیصه به همراه سازنده ها و setter و getter ها است.

package com.javatpoint;  
  
public class Employee {  
private int id;  
private String name;  
private float salary;  
//no-arg and parameterized constructors  
//getters and setters  
}  

EmployeeDao.java

این شامل یک خصیصه قالب Jdbc و یک متد saveEmployeeByPreparedStatement است. برای فهم کد این متد، باید مفهوم کلاس ناشناس (annonymous) را متوجه شوید.

package com.javatpoint;  
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 EmployeeDao {  
private JdbcTemplate jdbcTemplate;  
  
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {  
    this.jdbcTemplate = jdbcTemplate;  
}  
  
public Boolean saveEmployeeByPreparedStatement(final Employee e){  
    String query="insert into employee values(?,?,?)";  
    return jdbcTemplate.execute(query,new PreparedStatementCallback< Boolean >(){  
    @Override  
    public Boolean doInPreparedStatement(PreparedStatement ps)  
            throws SQLException, DataAccessException {  
              
        ps.setInt(1,e.getId());  
        ps.setString(2,e.getName());  
        ps.setFloat(3,e.getSalary());  
              
        return ps.execute();  
              
    }  
    });  
}  
  
  
}

applicationContext.xml

از DriverManagerDataSource برای نگهداری اطلاعات درباره پایگاه داده، از جمله نام کلاس driver، اتصال URL، نام کاربری و رمز عبور استفاده می شود. یک خصیصه به نام datasource در کلاس قالب Jdbc از نوع DriverManagerDataSource وجود دارد. بنابراین برای خصیصه datasource باید مرجع شی DriverManagerDataSource در کلاس قالب Jdbc را آماده کنیم. در اینجا از شی قالب Jdbc در کلاس EmployeeDao استفاده می کنیم. بنابراین از متد setter برای پاس دادن آن استفاده می کنیم. شما همچنین می توانید از سازنده استفاده کنید.

< ?xml version="1.0" encoding="UTF-8" ? >

< beans 3. xmlns="http://www.springframework.org/schema/beans"
         4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         5. xmlns:p="http://www.springframework.org/schema/p"
         6. xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.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="oracle"/>

< /bean>  
  
< bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
< property name="dataSource" ref="ds">< /property>  
< /bean>  
  
< bean id="edao" class="com.javatpoint.EmployeeDao">  
< property name="jdbcTemplate" ref="jdbcTemplate">< /property>  
< /bean>  
  
  
< /beans>  

Test.java

این کلاس bean را از فایل applicationContext.xml می گیرد و متد saveEmployeeByPreparedStatement() را فراخوانی میکند.

package com.javatpoint;   
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class Test {  
  
public static void main(String[] args) {  
    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");  
          
    EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");  
    dao.saveEmployeeByPreparedStatement(new Employee(108,"Amit",35000));  
}  
}  
1398/12/20 2016 523
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

نظرات خود را ثبت کنید...