Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7cbf224

Browse files
Spring Acceso a datos con JdbcTemplate
Usando la clase JdbcTemplate para facilitar el trabajo de acceso a datos.
1 parent 593acfe commit 7cbf224

File tree

9 files changed

+337
-0
lines changed

9 files changed

+337
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
10+
<config-files>
11+
<config-file>src/main/resources/springXMLConfig.xml</config-file>
12+
<config-file>src/main/resources/beans.xml</config-file>
13+
</config-files>
14+
<config-file-groups/>
15+
</spring-data>
16+
</project-shared-configuration>

‎tutorial_jdbc_template/nbactions.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>process-classes</goal>
10+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
11+
</goals>
12+
<properties>
13+
<exec.args>-classpath %classpath carmelo.spring.jdbc.SpringJDBC</exec.args>
14+
<exec.executable>java</exec.executable>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>process-classes</goal>
24+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
25+
</goals>
26+
<properties>
27+
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath carmelo.spring.jdbc.SpringJDBC</exec.args>
28+
<exec.executable>java</exec.executable>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>-classpath %classpath carmelo.spring.jdbc.SpringJDBC</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
</actions>

‎tutorial_jdbc_template/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>carmelo.spring</groupId>
5+
<artifactId>tutorial_jdbc_template</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.springframework</groupId>
12+
<artifactId>spring-context</artifactId>
13+
<version>4.3.6.RELEASE</version>
14+
</dependency>
15+
<dependency>
16+
<groupId>org.springframework</groupId>
17+
<artifactId>spring-jdbc</artifactId>
18+
<version>4.3.6.RELEASE</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.hsqldb</groupId>
22+
<artifactId>hsqldb</artifactId>
23+
<version>2.3.3</version>
24+
<scope>runtime</scope>
25+
</dependency>
26+
</dependencies>
27+
28+
<properties>
29+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
30+
<maven.compiler.source>1.8</maven.compiler.source>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
</properties>
33+
34+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package carmelo.spring.jdbc;
2+
3+
import javax.sql.DataSource;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
8+
9+
@Configuration
10+
@ComponentScan(basePackages = "carmelo.spring.jdbc.dao")
11+
public class SpringConfiguration {
12+
13+
@Bean
14+
public DataSource dataSource() {
15+
DriverManagerDataSource dataSource = new DriverManagerDataSource();
16+
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
17+
dataSource.setUrl("jdbc:hsqldb:hsql://localhost/");
18+
dataSource.setUsername("SA");
19+
dataSource.setPassword("");
20+
return dataSource;
21+
}
22+
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package carmelo.spring.jdbc;
2+
3+
import java.sql.SQLException;
4+
import carmelo.spring.jdbc.dao.ProductDao;
5+
import carmelo.spring.jdbc.model.Product;
6+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
7+
import org.springframework.context.support.AbstractApplicationContext;
8+
9+
public class SpringJDBC {
10+
11+
public static void main(String[] args) throws SQLException {
12+
13+
AbstractApplicationContext ctx
14+
= new AnnotationConfigApplicationContext(SpringConfiguration.class);
15+
16+
ProductDao product = ctx.getBean(ProductDao.class);
17+
18+
System.out.println("Cantidad de productos: " + product.cantidad());
19+
System.out.println("Producto con ID = 25: " + product.buscar(25));
20+
21+
// crear un nuevo producto
22+
Product p0 = new Product(111, "Computer", 800.25);
23+
24+
// insertar nuevo producto
25+
product.insertar(p0);
26+
27+
// bucar el producto con ID = 35
28+
Product p1 = product.buscar(35);
29+
p1.setName("Server Computer");
30+
p1.setPrice(1001.25);
31+
32+
// actualizar el producto con ID = 35
33+
product.actualizar(p1);
34+
35+
// listar todos los productos
36+
product.todos().forEach(System.out::println);
37+
38+
// eliminar el producto con ID = 111
39+
product.eliminar(p0.getId());
40+
41+
ctx.close();
42+
}
43+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package carmelo.spring.jdbc.dao;
2+
3+
import carmelo.spring.jdbc.model.Product;
4+
import java.util.List;
5+
6+
public interface ProductDao {
7+
8+
List<Product> todos();
9+
10+
Integer cantidad();
11+
12+
Product buscar(Integer id);
13+
14+
void insertar(Product product);
15+
16+
void actualizar(Product product);
17+
18+
void eliminar(Integer id);
19+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package carmelo.spring.jdbc.dao;
2+
3+
import carmelo.spring.jdbc.model.Product;
4+
import java.sql.ResultSet;
5+
import java.sql.SQLException;
6+
import java.util.List;
7+
import javax.sql.DataSource;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.dao.DuplicateKeyException;
10+
import org.springframework.jdbc.core.JdbcTemplate;
11+
import org.springframework.jdbc.core.RowMapper;
12+
import org.springframework.stereotype.Repository;
13+
14+
@Repository
15+
public class ProductDaoImpl implements ProductDao {
16+
17+
private JdbcTemplate jdbcTemplate;
18+
19+
@Autowired
20+
public void setDataSource(DataSource dataSource) {
21+
this.jdbcTemplate = new JdbcTemplate(dataSource);
22+
}
23+
24+
@Override
25+
public List<Product> todos() {
26+
27+
String sql = "select * from product";
28+
29+
return this.jdbcTemplate.query(sql, new RowMapper<Product>() {
30+
@Override
31+
public Product mapRow(ResultSet rs, int i) throws SQLException {
32+
Product p = new Product();
33+
p.setId(rs.getInt("ID"));
34+
p.setName(rs.getString("NAME"));
35+
p.setPrice(rs.getDouble("PRICE"));
36+
return p;
37+
}
38+
});
39+
}
40+
41+
@Override
42+
public Integer cantidad() {
43+
String sql = "select count(*) from product";
44+
return this.jdbcTemplate.queryForObject(sql, Integer.class);
45+
}
46+
47+
@Override
48+
public Product buscar(Integer id) {
49+
String sql = "select * from product where ID = ?";
50+
return this.jdbcTemplate.queryForObject(sql, new ProductRowMapper(), id);
51+
}
52+
53+
@Override
54+
public void insertar(Product product) {
55+
String sql = "insert into product (ID, NAME, PRICE) values (?, ?, ?)";
56+
this.jdbcTemplate.update(sql,
57+
product.getId(),
58+
product.getName(),
59+
product.getPrice());
60+
}
61+
62+
@Override
63+
public void actualizar(Product product) {
64+
String sql = "update product set NAME = ?, PRICE = ? where ID = ?";
65+
this.jdbcTemplate.update(sql,
66+
product.getName(),
67+
product.getPrice(),
68+
product.getId());
69+
}
70+
71+
@Override
72+
public void eliminar(Integer id) {
73+
String sql = "delete from product where ID = ?";
74+
this.jdbcTemplate.update(sql, id);
75+
}
76+
77+
class ProductRowMapper implements RowMapper<Product> {
78+
79+
@Override
80+
public Product mapRow(ResultSet rs, int i) throws SQLException {
81+
Product p = new Product();
82+
p.setId(rs.getInt("ID"));
83+
p.setName(rs.getString("NAME"));
84+
p.setPrice(rs.getDouble("PRICE"));
85+
return p;
86+
}
87+
88+
}
89+
90+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package carmelo.spring.jdbc.model;
2+
3+
public class Product {
4+
5+
private Integer id;
6+
private String name;
7+
private Double price;
8+
9+
public Product() {
10+
}
11+
12+
public Product(Integer id, String name, Double price) {
13+
this.id = id;
14+
this.name = name;
15+
this.price = price;
16+
}
17+
18+
public Integer getId() {
19+
return id;
20+
}
21+
22+
public void setId(Integer id) {
23+
this.id = id;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public Double getPrice() {
35+
return price;
36+
}
37+
38+
public void setPrice(Double price) {
39+
this.price = price;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "Product{" + "id=" + id + ", name=" + name + ", price=" + price + '}';
45+
}
46+
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!-- <?xml version="1.0" encoding="UTF-8"?> -->
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
6+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
7+
8+
<!-- <context:component-scan base-package="carmelo.spring.introduccion"/> -->
9+
10+
<bean id="saludaService"
11+
init-method="initHello"
12+
destroy-method="destroyHello"
13+
class="carmelo.spring.introduccion.HelloServiceImpl"/>
14+
15+
<bean id="one"
16+
class="carmelo.spring.introduccion.ClientService"
17+
factory-method="createInstance"/>
18+
19+
</beans>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /