-
-
Save flbulgarelli/f2219952bcacb33ea35a71a4e5478399 to your computer and use it in GitHub Desktop.
Ejemplo JDBC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>utn.dds.persistencia</groupId> | |
<artifactId>que-me-pongo</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>Que Me Pongo</name> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<!-- drivers jdbc --> | |
<!-- postgre --> | |
<dependency> | |
<groupId>org.postgresql</groupId> | |
<artifactId>postgresql</artifactId> | |
<version>42.2.23</version> | |
</dependency> | |
<!-- hsqldb --> | |
<dependency> | |
<groupId>org.hsqldb</groupId> | |
<artifactId>hsqldb</artifactId> | |
<version>2.4.0</version> | |
<scope>test</scope> | |
</dependency> | |
<!-- dependency test --> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.11</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.2</version> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>com.github.johnpoth</groupId> | |
<artifactId>jshell-maven-plugin</artifactId> | |
<version>1.3</version> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ddl ---> create table, create view, .... | |
create table materiales ( | |
codigo bigint primary key, | |
nombre varchar(255) not null, | |
marca varchar(255), | |
precio int | |
) | |
create table prendas ( | |
id_prenda bigint primary key, | |
nombre character varying(255) not null, | |
color character varying(255), | |
cod_material bigint references materiales(codigo) | |
) | |
--dml ---> insert - update - delete - select | |
insert into materiales (codigo, nombre, marca, precio) values (3, 'lana', 'ovejita', 150); | |
insert into materiales (codigo, nombre, marca, precio) values (4, 'cuero', 'vaca feliz', 190); | |
insert into materiales (codigo, nombre, marca, precio) values (5, 'algodon', 'superhuavechito', 100); | |
insert into prendas (id_prenda, nombre, color, cod_material) values (4, 'sueter', 'verde', 3) | |
update materiales set precio = 120 where nombre = 'algodon' | |
select nombre, precio from materiales where precio > 100 | |
select codigo from materiales where nombre = 'algodon' | |
select * from prendas where cod_material in (select codigo from materiales where nombre = 'algodon') | |
select p.nombre as prenda, m.nombre as material, m.precio from prendas p, materiales m where p.cod_material = m.codigo and m.nombre = 'algodon' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.* | |
Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost/que_me_pongo_2_development", "que_me_pongo_2", "que_me_pongo_2" | |
PreparedStatement statement = connection.prepareStatement("select * from prendas") | |
ResultSet result = statement.executeQuery() | |
result.next() | |
result.getString("descripcion") | |
result.close() | |
statement.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment