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 593acfe

Browse files
Spring Acceso a datos con la API JDBC
Acceder a datos con Spring y el uso de la API JDBC a bajo nivel.
1 parent ebbc159 commit 593acfe

File tree

18 files changed

+423
-0
lines changed

18 files changed

+423
-0
lines changed

‎tutorial_jdbc/nb-configuration.xml

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/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/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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</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+
</dependency>
25+
</dependencies>
26+
27+
<properties>
28+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
29+
<maven.compiler.source>1.8</maven.compiler.source>
30+
<maven.compiler.target>1.8</maven.compiler.target>
31+
</properties>
32+
33+
</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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package carmelo.spring.jdbc;
2+
3+
import java.sql.SQLException;
4+
import carmelo.spring.jdbc.dao.ProductDao;
5+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6+
import org.springframework.context.support.AbstractApplicationContext;
7+
8+
public class SpringJDBC {
9+
10+
public static void main(String[] args) throws SQLException {
11+
12+
AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class);
13+
14+
ProductDao product = ctx.getBean(ProductDao.class);
15+
product.todos().forEach(System.out::println);
16+
17+
ctx.close();
18+
}
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package carmelo.spring.jdbc.dao;
2+
3+
import carmelo.spring.jdbc.model.Product;
4+
import java.sql.Connection;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import javax.sql.DataSource;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.stereotype.Repository;
13+
14+
@Repository
15+
public class ProductDaoImpl implements ProductDao {
16+
17+
private DataSource dataSource;
18+
19+
@Autowired
20+
public void setDataSource(DataSource dataSource) {
21+
this.dataSource = dataSource;
22+
}
23+
24+
@Override
25+
public List<Product> todos() {
26+
27+
List<Product> products = new ArrayList<>();
28+
Connection conn = null;
29+
30+
try {
31+
conn = dataSource.getConnection();
32+
33+
Statement stmt = conn.createStatement();
34+
ResultSet rs = stmt.executeQuery("select * from product");
35+
36+
while(rs.next()) {
37+
Product p = new Product();
38+
p.setId(rs.getInt("ID"));
39+
p.setName(rs.getString("NAME"));
40+
p.setPrice(rs.getDouble("PRICE"));
41+
42+
products.add(p);
43+
}
44+
45+
rs.close();
46+
stmt.close();
47+
} catch (SQLException e) {
48+
throw new RuntimeException(e);
49+
} finally {
50+
if (conn != null) {
51+
try {
52+
conn.close();
53+
} catch (SQLException e) {
54+
}
55+
}
56+
}
57+
58+
return products;
59+
}
60+
61+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 Integer getId() {
10+
return id;
11+
}
12+
13+
public void setId(Integer id) {
14+
this.id = id;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public Double getPrice() {
26+
return price;
27+
}
28+
29+
public void setPrice(Double price) {
30+
this.price = price;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "Product{" + "id=" + id + ", name=" + name + ", price=" + price + '}';
36+
}
37+
38+
}
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>
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>

0 commit comments

Comments
(0)

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