Inserting Data Into Datbase s9
Inserting Data Into Datbase s9
Inserting Data Into Datbase s9
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.27</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.2</version>
</dependency>
*****************************************end
=============>
Database Setup.
Spring JDBC Configuration
JdbcTemplate and DataSource Setup
-----
create database springjdbc;
show databases;
---
use springjdbc;
create table student(id int primary key,
name varchar(100) not null,
city varchar(200));
show tables;
---
insert into student(id,name,city) values(1,'raja', "mumbai");
desc student;
-----------------------------------***********
com.spring.entity ---> Student class --id, name, city ; //default constructor, para
constructor, getter & setter , tostring() ... like pojo
object store in App class
config.xml
-----
<beans ..........>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="ds"
>
//postgrey jdbc driver class name
<property name = "driveClassName" value = "com.mysql.jdbc.Driver />
<property name = "url" value = "jdbc:msql://Localhost:3306/springjdbc " />
<property name = "username" value = "postgrey" />
<property name = "password" value = "sb@1..5" />
//jdbc : is protocol, which technology we are using
//mysql : is subprotocol
// //Localhost:3306 : ip address ,which system db working, if db working on another
sysytem then we use ip address of that system
//database name:
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
//insert query
String query ="insert into student(id,name,city)values(?,?,?)";
studentDaoImpl----
public int update(Student student) {
//update query
String query ="update student set name=?,city=? where id=?";
int result =
this.jdbcTemplate.update(query,student.getName(),student.getCity(),student.getId())
;
return result;
}
---app.java
Student student = new Student();
student.setId(3);
student.setName("no name");
student.setCity("hif");
int result = bean.update(student);
System.out.println("updated student :" +result);
}
-----------
delete operation using spring jdbc
==========>
studentDao -- public int delete(Student student);
studentDaoImpl----
public int delete(Student student) {
//delete query
String query ="delete from student where name=?;
int result = this.jdbcTemplate.update(query,student.getName());
syso("record is delete");
return result;
}
---app.java
Student student = new Student();
student.setName("no name");
int result = bean.delete(student);
System.out.println("deleted student :" +result);
}
**************************************************************
perform retrive/select operation using spring jdbc | selecting data using spring
jdbc
========================>
jdbc template methods
public T queryForObjecct(String sql, RowMapper<T>rowMapper, Object args)
public List<T> queryForObjecct(String sql, RowMapper<T>rowMapper)
@Bean("ds")
public DriverMangerDataSource getDataSource(){
DriverMangerDataSource ds = new DriverMangerDataSource();
ds.setDriverclasName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://Localhost:3306/springjdbc");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
@Bean("jdbcTemplate")
public JdbcTemplate getTemplate(){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(ds/ getDataSource());
return jdbcTemplate;
}
@Bean("studentDao")
public Studentdao getStudentdao(){
StudentDaoImpl studentDao = new StudentDaoImpl();
studentDao.setJdbctemplate(jdbcTemplate or getTemplate());
return studentDao;
}
}
*********************
use autowire in JdbcTemplate and on StudentDaoImple add @Component("studentdao")
**************************************