Giter VIP home page Giter VIP logo

spring-jdbctemplate's Introduction

Spring-JdbcTemplate

Welcome to the Spring-JdbcTemplate- wiki!

  • create database query
  • create pojo class
  • create jdbcTemaplate Execute query:
  • create application Context.xml with database connection:
  • create main java class
create table employee(id number(10),name varchar(100),salary number(10));
employee java:
public class Employee{
private int id;
private String name;
Private float salary;
getter setter methos
} 

private JdbcTemplate JdbcTemplate:

public int Save(Employee e){
String Query="insert into employee vales(
"+e.getId()+","+e.getName+",salary=+e.getsalary()+"'";
return jdbcTemplate.update(query);

public int deletQuery(Employeee){
String query="delete from employee where id="+e.getId()+"";
return jdbcTemplate.update(delete);
}

application contect.xml:

<bean id="ds" class="org.spring.framework.jdbc.datasource.DriverManagerDataSource">
<property name="driverclass" value="oracle.jdbc.driver.oracleDriver">
<property name="url" value="localjost:1522:xe:/>
<property name="username" value="system"/>
<property name="password" value="oracle"/>
</bean>

<bean id="jdbctemplate" class="org.spring.jdbc.core.JdbcTemplate">
<property name="datasource" red="ds"></property>
</bean>

<bean id="edao" class="com.javatpoint.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate">
</bean>

</beans>

test.java:

Applicationcontext ctx=new ClassPathXmlApplcationContext("applicationContext.xml");

EmployeeDao dao=(EmployeeDao).getBean("dao");
int status=dao.saveEmployee(new Employee(102,"amit",35000));
system.out.printlnt(status);

preparedstatement:

boolean saveEmployeeByPrearedStatement(final Employeee e)
{
String query="insert into employee values(?,?,?)";
return jdbcTemplate.execute(query,new PreparedStatementCallBack<Boolean>(){

public Boolean doInPreparedStatement(PreparedStatement ps){

ps.setInt(1,e.getId());
ps.setString(2,e.getName());
ps.setFloat(3,e.getSalary());

return ps.execute();
}}

prepared statement:

ps.setId(1,e.getId());
ps.setString(2,e.getName());
ps.setFloat(3,e.getsalary());

fetching record from JDBCTemplate:
template.query
template.
->easily fetch records from database using query method

public list<Employee> getAllEmployee(){
return template.query("select * from employee");
List<Employee> list=new ArrayList<Employee>();
while(rs.next()){
Employee e=new Employee();
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setSalary(rs.getInt(3));
list.add(e);
}
return list;
}
  
  

main class:

employee dao=(EmployeeDao)ctx.getBean("edao");
dao.saveEmployeeByPreparedStatement(new Employee(108,"Amit",35000));

Jdbc data source:

JdbcDaoImpl:

JdbcTemplate jdbcTemplate=new JdbcTemplate();

DataSource:

setter getter

JdbcTemplate

getter setter

spring.xml:

<context:annotation-config/>
<bean id="datasource" class="org.apache.common.dcp.basicdatasource"/>
<property name="driverClassName" value="org.class.apache.derby."/>
<property name="url" value=""/>
<property name="initialSize" value="2"/>
<property name="maxAccess" value="5"/>\
</bean>

JDBCDemo.java:

main()
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("Spring.xml");
JdbcDaoImpl dao=ctx.getBean("jdbcDaoImpl",jdbcdaoimpl.class);
}

`

https://www.youtube.com/watch?v=0LYqy0wvS20

spring-jdbctemplate's People

Contributors

sandysanthosh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.