Advertisement
Guest User

Untitled

a guest
Dec 12th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. //CLASE JUEGO
  2. package Modelo;
  3.  
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. public class Juego {
  8.  
  9. Conectar c;
  10.  
  11. public Juego() {
  12. c = new Conectar();
  13. }
  14.  
  15. public int agregar(int id, int stock, String nombre, String precio, String categoria){
  16. try{
  17. Statement agregar=c.crearSentencia();
  18. return agregar.executeUpdate("insert into juego (ID,Stock,Nombre,Categoria,Precio) values ("+id+","+stock+",'"+nombre+"','"+precio+"','"+categoria+"')");
  19. }catch(SQLException ex){
  20. return -1;
  21. }
  22. }
  23.  
  24. public int modificarPrecio(int id, String nuevoPrecio){
  25. try{
  26. Statement s=c.crearSentencia();
  27. return s.executeUpdate("UPDATE juego SET precio='"+nuevoPrecio+"'WHERE id='"+id+"'");
  28. }catch(SQLException ex){
  29. return -1;
  30. }
  31. }
  32. }
  33.  
  34. //CLASE TIENDA
  35. package Modelo;
  36.  
  37. import java.util.ArrayList;
  38. import java.sql.SQLException;
  39. import java.sql.Statement;
  40. import javax.swing.table.DefaultTableModel;
  41.  
  42. public class Tienda {
  43. Conectar c;
  44. public ArrayList <Juego> lista = new ArrayList();
  45.  
  46.  
  47. public ArrayList<Juego> getLista() {
  48. return lista;
  49. }
  50.  
  51. public void setLista(ArrayList<Juego> lista) {
  52. this.lista = lista;
  53. }
  54.  
  55. public Tienda() {
  56. c = new Conectar();
  57. }
  58.  
  59. public int cliente(int rut, int telefono, String nombre, String direccion, String ciudad){
  60. try{
  61. Statement agregar=c.crearSentencia();
  62. return agregar.executeUpdate("insert into cliente (Rut,Telefono,Nombre,Direccion,Ciudad) values ("+rut+","+telefono+",'"+nombre+"','"+direccion+"','"+ciudad+"')");
  63. }catch(SQLException ex){
  64. return -1;
  65. }
  66. }
  67.  
  68. public int venta (int id, String valor, int cantidad){
  69. try{
  70. Statement s=c.crearSentencia();
  71. return s.executeUpdate("insert into venta (ID_juego,Valor_total,Cantidad) values ("+id+",'"+valor+"','"+cantidad+"')");
  72. }catch(SQLException ex){
  73. return -1;
  74. }
  75. }
  76.  
  77. public int ventaResta (int id, int nuevoStock){
  78. try{
  79. Statement s=c.crearSentencia();
  80. return s.executeUpdate("UPDATE juego SET stock=stock - "+nuevoStock+" WHERE ID="+id+"");
  81. }catch(SQLException ex){
  82. return -1;
  83. }
  84. }
  85. }
  86.  
  87. //CLASE CONECTAR
  88. package Modelo;
  89. import java.sql.Connection;
  90. import java.sql.DriverManager;
  91. import java.sql.SQLException;
  92. import java.sql.Statement;
  93.  
  94. public class Conectar {
  95. private Connection conn;
  96.  
  97. private String registrar(){
  98. try{
  99. Class.forName("com.mysql.jdbc.Driver");
  100. return "Exito";
  101. }catch(ClassNotFoundException e){
  102. return "no registrado";
  103. }
  104. }
  105.  
  106. private String obtenerConexion(){
  107. try{
  108. conn=DriverManager.getConnection("jdbc:mysql://localhost/prueba","root","");
  109. return "Exito";
  110. }catch (SQLException e){
  111. return "no conectado";
  112. }
  113. }
  114.  
  115. public Statement crearSentencia(){
  116. try{
  117. Statement s = conn.createStatement();
  118. return s;
  119. }catch(SQLException e){
  120. return null;
  121. }
  122. }
  123.  
  124. public Conectar() {
  125. registrar();
  126. obtenerConexion();
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement