Guest User

Untitled

a guest
Aug 20th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. public class Camiseta {
  2.  
  3. private int id;
  4. private int numero;
  5. private Marca marca;
  6.  
  7. public int getId() {
  8. return id;
  9. }
  10.  
  11. public void setId(int id) {
  12. this.id = id;
  13. }
  14.  
  15. public int getNumero() {
  16. return numero;
  17. }
  18.  
  19. public void setNumero(int numero) {
  20. this.numero = numero;
  21. }
  22.  
  23. public Marca getMarca() {
  24. return marca;
  25. }
  26.  
  27. public void setMarca(Marca marca) {
  28. this.marca = marca;
  29. }
  30.  
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. public class Equipo {
  39.  
  40. private int id;
  41. private String nombre;
  42.  
  43. public int getId() {
  44. return id;
  45. }
  46.  
  47. public void setId(int id) {
  48. this.id = id;
  49. }
  50.  
  51. public String getNombre() {
  52. return nombre;
  53. }
  54.  
  55. public void setNombre(String nombre) {
  56. this.nombre = nombre;
  57. }
  58.  
  59. }
  60.  
  61.  
  62.  
  63.  
  64. public class Jugador {
  65.  
  66. private int id;
  67. private String nombre;
  68. private Equipo equipo;
  69. private Camiseta camiseta;
  70.  
  71. public int getId() {
  72. return id;
  73. }
  74.  
  75. public void setId(int id) {
  76. this.id = id;
  77. }
  78.  
  79. public String getNombre() {
  80. return nombre;
  81. }
  82.  
  83. public void setNombre(String nombre) {
  84. this.nombre = nombre;
  85. }
  86.  
  87. public Equipo getEquipo() {
  88. return equipo;
  89. }
  90.  
  91. public void setEquipo(Equipo equipo) {
  92. this.equipo = equipo;
  93. }
  94.  
  95. public Camiseta getCamiseta() {
  96. return camiseta;
  97. }
  98.  
  99. public void setCamiseta(Camiseta camiseta) {
  100. this.camiseta = camiseta;
  101. }
  102.  
  103. }
  104.  
  105.  
  106.  
  107. public class Marca {
  108.  
  109. private int id;
  110. private String nombre;
  111.  
  112.  
  113.  
  114. public int getId() {
  115. return id;
  116. }
  117.  
  118. public void setId(int id) {
  119. this.id = id;
  120. }
  121.  
  122. public String getNombre() {
  123. return nombre;
  124. }
  125.  
  126. public void setNombre(String nombre) {
  127. this.nombre = nombre;
  128. }
  129.  
  130. }
  131.  
  132. import com.mitocode.beans.Marca;
  133.  
  134. public interface DAOMarca {
  135.  
  136. public void registrar(Marca marca) throws Exception;
  137. }
  138.  
  139.  
  140.  
  141. import java.sql.Connection;
  142. import java.sql.PreparedStatement;
  143.  
  144. import javax.sql.DataSource;
  145.  
  146. import org.springframework.beans.factory.annotation.Autowired;
  147. import org.springframework.stereotype.Repository;
  148.  
  149. import com.mitocode.beans.Marca;
  150.  
  151.  
  152. @Repository //estereotipo de una determinada capa
  153. public class DAOMarcaImpl implements DAOMarca {
  154.  
  155. @Autowired
  156. private DataSource dataSource;
  157.  
  158.  
  159. public void registrar(Marca marca) throws Exception {
  160. // TODO Auto-generated method stub
  161.  
  162. String sql = "INSERT INTO marca(marca_id, marca_nombre) VALUES (?,?)";
  163. Connection con = null;
  164. try {
  165. con = dataSource.getConnection();
  166. PreparedStatement ps = con.prepareStatement(sql);
  167. ps.setInt(1, marca.getId());
  168. ps.setString(2, marca.getNombre());
  169. ps.executeUpdate();
  170. ps.close();
  171. } catch (Exception e) {
  172. // TODO: handle exception
  173. }
  174. finally {
  175. if (con != null) {
  176. con.close();
  177. }
  178. }
  179. }
  180.  
  181. }
  182.  
  183. import com.mitocode.beans.Marca;
  184.  
  185. public interface ServiceMarca {
  186. public void registrar(Marca marca) throws Exception;
  187.  
  188. }
  189.  
  190.  
  191. import org.springframework.stereotype.Service;
  192.  
  193. import com.mitocode.beans.Marca;
  194. import com.mitocode.dao.DAOMarca;
  195.  
  196. @Service
  197. public class ServiceMarcaImpl implements ServiceMarca {
  198.  
  199. private DAOMarca daoMarca;
  200.  
  201. public void registrar(Marca marca) throws Exception {
  202. // TODO Auto-generated method stub
  203. try {
  204. daoMarca.registrar(marca);
  205.  
  206. } catch (Exception e) {
  207. // TODO: handle exception
  208. }
  209. finally {
  210.  
  211. }
  212. }
  213.  
  214. }
  215.  
  216. Marca marca = new Marca();
  217. marca.setId(666);
  218. marca.setNombre("Mi marca");
  219.  
  220.  
  221. ApplicationContext appContext = new ClassPathXmlApplicationContext("com/mitocode/xml/beans.xml");
  222.  
  223. ServiceMarca sm = (ServiceMarca) appContext.getBean("serviceMarcaImpl");
  224.  
  225.  
  226. try {
  227. sm.registrar(marca);
  228. } catch (Exception e) {
  229. // TODO: handle exception
  230. System.out.println("Error insercion: "+e);
  231. }
  232.  
  233. <?xml version="1.0" encoding="UTF-8"?>
  234. <beans xmlns="http://www.springframework.org/schema/beans"
  235. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  236. xmlns:context="http://www.springframework.org/schema/context"
  237. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  238. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
  239.  
  240. <context:component-scan base-package="com.mitocode"></context:component-scan>
  241.  
  242.  
  243. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  244. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  245. <property name="url" value="jdbc:mysql://localhost:3306/db_spring"></property>
  246. <property name="username" value="root"></property>
  247. <property name="password" value=""></property>
  248. </bean>
  249.  
  250. </beans>
  251.  
  252. <project xmlns="http://maven.apache.org/POM/4.0.0"
  253. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  254. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  255. <modelVersion>4.0.0</modelVersion>
  256.  
  257. <groupId>com.mitocode</groupId>
  258. <artifactId>springbd</artifactId>
  259. <version>0.0.1-SNAPSHOT</version>
  260. <packaging>jar</packaging>
  261.  
  262. <name>springbd</name>
  263. <url>http://maven.apache.org</url>
  264.  
  265. <properties>
  266. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  267. </properties>
  268.  
  269. <dependencies>
  270.  
  271. <dependency>
  272. <groupId>org.springframework</groupId>
  273. <artifactId>spring-context</artifactId>
  274. <version>4.2.0.RELEASE</version>
  275. </dependency>
  276.  
  277. <dependency>
  278. <groupId>org.springframework</groupId>
  279. <artifactId>spring-jdbc</artifactId>
  280. <version>4.2.0.RELEASE</version>
  281. </dependency>
  282.  
  283. <dependency>
  284. <groupId>mysql</groupId>
  285. <artifactId>mysql-connector-java</artifactId>
  286. <version>5.1.37</version>
  287. </dependency>
  288.  
  289. </dependencies>
  290. </project>
Add Comment
Please, Sign In to add comment