Guest User

Untitled

a guest
Apr 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. package Conectar;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Driver;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import javax.swing.JOptionPane;
  11. import java.sql.CallableStatement;
  12. import java.sql.Types;
  13.  
  14.  
  15. public class Coneccion
  16. {
  17. static String login="";
  18. static String Password="";
  19. Connection conexion=null;
  20. String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
  21. CallableStatement cst;
  22. ResultSet r;
  23. Statement sentencia;
  24. Coneccion con;
  25.  
  26. public Coneccion()
  27. {
  28. try
  29. {
  30. Class.forName(driver);
  31. conexion=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; databaseName= NEGOCIO", "sa", "123");
  32. }
  33. catch(SQLException | ClassNotFoundException e)
  34. {
  35. JOptionPane.showMessageDialog(null, e);
  36. }
  37. }
  38.  
  39. public Connection getConnection()
  40. {
  41. return conexion;
  42. }
  43.  
  44. public void desconectar()
  45. {
  46. conexion=null;
  47. if(conexion!=null)
  48. {
  49. JOptionPane.showMessageDialog(null, "No se pudo cerra la conexion");
  50. }
  51. }
  52.  
  53. public ResultSet Listar (String Cad)
  54. {
  55. try
  56. {
  57. Class.forName(driver).newInstance();
  58. Connection cn= DriverManager.getConnection("jdbc:sqlserver://localhost:1433; databaseName= NEGOCIO", "sa", "123");
  59. PreparedStatement da= cn.prepareStatement(Cad);
  60. ResultSet tbl= da.executeQuery();
  61. return tbl;
  62. }
  63. catch(ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e)
  64. {
  65. javax.swing.JOptionPane.showMessageDialog(null, e);
  66. return null;
  67. }
  68. }
  69.  
  70.  
  71. public String Ejecutar (String Cad)
  72. {
  73. try
  74. {
  75. Class.forName(driver).newInstance();
  76. Connection cn= DriverManager.getConnection("jdbc:sqlserver://localhost:1433; databaseName= NEGOCIO", "sa", "123");
  77. PreparedStatement da= cn.prepareStatement(Cad);
  78.  
  79. ResultSet r = da.executeQuery();
  80. return "Registro Grabados con exito";
  81. }
  82. catch(ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e)
  83. {
  84. javax.swing.JOptionPane.showMessageDialog(null, e);
  85. return "Error: " + e.getMessage();
  86. }
  87.  
  88. }
  89.  
  90.  
  91.  
  92. public ResultSet TraerProducto(int id, String nombre, int m) {
  93. try
  94. {
  95. Coneccion obj= new Coneccion();
  96.  
  97. PreparedStatement pstm= con.getConnection().prepareStatement("{call TraerProducto ?, ?}");
  98. pstm.setInt(1, id);
  99. pstm.setString(2, nombre);
  100.  
  101. ResultSet consulta = pstm.executeQuery();
  102.  
  103. ResultSet r=obj.Listar(String.valueOf(consulta));
  104. }
  105. catch(Exception e)
  106. {
  107. JOptionPane.showMessageDialog(null, JOptionPane.WARNING_MESSAGE);
  108. }
  109. return r;
  110. }
  111. }
  112.  
  113. public void keyTyped(KeyEvent e)
  114. {
  115. if (textBuscarP.getText().length()>3)
  116. {
  117. String eleccion=String.valueOf(comboBoxFiltroP.getSelectedIndex());
  118.  
  119. if(eleccion=="CODIGO")
  120. {
  121. m=1;
  122. }
  123. else
  124. {
  125. m=0;
  126. }
  127.  
  128.  
  129. Coneccion objeto= new Coneccion();
  130. ResultSet r;
  131.  
  132.  
  133. if (m==1)
  134. {
  135. r = objeto.TraerProducto(Integer.parseInt(this.textBuscarP.getText()), "", m);
  136.  
  137. }
  138. else
  139. {
  140. r= objeto.TraerProducto(-10, this.textBuscarP.getText(), m);
  141. }
  142.  
  143. try
  144. {
  145. String titulos[]= {"ID","Nombre","Precio","Cantidad","Precio de Descuento", "Cantidad de Descuento","Codigo de Barra"};
  146. DefaultTableModel m1 = new DefaultTableModel(null, titulos);
  147. JTable p= new JTable (m1);
  148. String fila[]= new String[7];
  149. int c=1;
  150.  
  151. while (r.next())
  152. {
  153. fila[1]=r.getString(1);
  154. fila[2]=r.getString(2);
  155. fila[3]=r.getString(3);
  156. fila[4]=r.getString(4);
  157. fila[5]=r.getString(5);
  158. fila[6]=r.getString(6);
  159. m1.addRow(fila);
  160. c++;
  161. }
  162. TablaProducto.setModel(m1);
  163. TableRowSorter<TableModel> ordenar= new TableRowSorter<TableModel> (m1);
  164. TablaProducto.setRowSorter(ordenar);
  165. this.TablaProducto.setModel(m1);
  166. }
  167. catch(Exception e1)
  168. {
  169. JOptionPane.showMessageDialog(null, JOptionPane.WARNING_MESSAGE);
  170. }
  171. }
  172.  
  173. }
Add Comment
Please, Sign In to add comment