Está en la página 1de 20

Codigo Fuente

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

/*

* ingresoproductos.java

* Created on 28-abr-2013, 15:36:32

*/

package clases;

import java.sql.*;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JFormattedTextField;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import javax.swing.table.DefaultTableModel;

/**

* @author Usuario

*/

public class ingresoproductos extends javax.swing.JFrame {

/** Creates new form ingresoproductos */

public ingresoproductos() {

initComponents();

mostrardatos("");

void mostrardatos(String valor){

DefaultTableModel modelo= new DefaultTableModel();

modelo.addColumn("CODIGO");

modelo.addColumn("DESCRIPCION");

modelo.addColumn("PRECIO UNITARIO");

tbproductos.setModel(modelo);

String sql="";

if(valor.equals(""))

sql="SELECT * FROM productos";

else{

sql="SELECT * FROM productos WHERE codpro='"+valor+"'";

String []datos = new String [3];

try {
Statement st = cn.createStatement();

ResultSet rs = st.executeQuery(sql);

while(rs.next()){

datos[0]=rs.getString(1);

datos[1]=rs.getString(2);

datos[2]=rs.getString(3);

modelo.addRow(datos);

tbproductos.setModel(modelo);

} catch (SQLException ex) {

Logger.getLogger(ingresoproductos.class.getName()).log(Level.SEVERE, null, ex);

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jPopupMenu1 = new javax.swing.JPopupMenu();

jMenuItem1 = new javax.swing.JMenuItem();

jMenuItem2 = new javax.swing.JMenuItem();

jPanel1 = new javax.swing.JPanel();

jLabel1 = new javax.swing.JLabel();

txtcod = new javax.swing.JTextField();


jLabel2 = new javax.swing.JLabel();

txtdes = new javax.swing.JTextField();

jLabel3 = new javax.swing.JLabel();

txtpre = new javax.swing.JTextField();

btngrabar = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

jButton7 = new javax.swing.JButton();

jLabel7 = new javax.swing.JLabel();

jLabel8 = new javax.swing.JLabel();

jLabel9 = new javax.swing.JLabel();

jLabel10 = new javax.swing.JLabel();

jScrollPane1 = new javax.swing.JScrollPane();

tbproductos = new javax.swing.JTable();

txtbuscar = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jLabel4 = new javax.swing.JLabel();

jLabel5 = new javax.swing.JLabel();

jButton5 = new javax.swing.JButton();

jButton6 = new javax.swing.JButton();

jLabel6 = new javax.swing.JLabel();

jButton4 = new javax.swing.JButton();

jTextField1 = new javax.swing.JTextField();

jMenuBar1 = new javax.swing.JMenuBar();

jMenu1 = new javax.swing.JMenu();

jMenuItem3 = new javax.swing.JMenuItem();

jMenu2 = new javax.swing.JMenu();

jMenuItem5 = new javax.swing.JMenuItem();

jMenuItem1.setText("modificar");

jMenuItem1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {


jMenuItem1ActionPerformed(evt);

});

jPopupMenu1.add(jMenuItem1);

jMenuItem2.setText("eliminar");

jMenuItem2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jMenuItem2ActionPerformed(evt);

});

jPopupMenu1.add(jMenuItem2);

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

setTitle("Ingreso Productos");

setBackground(new java.awt.Color(204, 204, 255));

setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

setLocation(new java.awt.Point(0, 0));

setResizable(false);

setSize(new java.awt.Dimension(0, 0));

getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalle Producto"));

jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

jLabel1.setText("Codigo");

jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, -1, -1));

txtcod.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

txtcodActionPerformed(evt);
}

});

txtcod.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyTyped(java.awt.event.KeyEvent evt) {

txtcodKeyTyped(evt);

});

jPanel1.add(txtcod, new org.netbeans.lib.awtextra.AbsoluteConstraints(88, 31, 147, -1));

jLabel2.setText("Descripcion");

jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 110, -1, 30));

jPanel1.add(txtdes, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 110, 147, -1));

jLabel3.setText("Precio");

jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 80, -1, -1));

txtpre.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

txtpreActionPerformed(evt);

});

txtpre.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyTyped(java.awt.event.KeyEvent evt) {

txtpreKeyTyped(evt);

});

jPanel1.add(txtpre, new org.netbeans.lib.awtextra.AbsoluteConstraints(88, 71, 147, -1));

btngrabar.setText("Grabar");

btngrabar.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {


btngrabarActionPerformed(evt);

});

jPanel1.add(btngrabar, new org.netbeans.lib.awtextra.AbsoluteConstraints(369, 81, -1, -


1));

jButton3.setText("actualizar");

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt);

});

jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(264, 81, -1, -1));

jButton7.setText("Limpiar");

jButton7.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton7ActionPerformed(evt);

});

jPanel1.add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(458, 81, -1, -1));

jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/clases/mysql-
logo.png"))); // NOI18N

jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(627, 31, 99, -1));

jLabel8.setIcon(new
javax.swing.ImageIcon("C:\\Users\\Julioguitars\\Desktop\\Java_Insert_Update_Delete_Display
\\Project
Code\\Java_Insert_Update_Delete_Display_Tutorial\\src\\java_insert_update_delete_display
_tutorial\\icons\\delete.png")); // NOI18N

jPanel1.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 20, -1, 50));


jLabel9.setIcon(new
javax.swing.ImageIcon("C:\\Users\\Julioguitars\\Desktop\\Java_Insert_Update_Delete_Display
\\Project
Code\\Java_Insert_Update_Delete_Display_Tutorial\\src\\java_insert_update_delete_display
_tutorial\\icons\\refresh.png")); // NOI18N

jPanel1.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 28, 40, -1));

jLabel10.setIcon(new
javax.swing.ImageIcon("C:\\Users\\Julioguitars\\Desktop\\Java_Insert_Update_Delete_Display
\\Project
Code\\Java_Insert_Update_Delete_Display_Tutorial\\src\\java_insert_update_delete_display
_tutorial\\icons\\add.png")); // NOI18N

jPanel1.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 30, -1, -1));

getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 13,


802, -1));

tbproductos.setModel(new javax.swing.table.DefaultTableModel(

new Object [][] {

},

new String [] {

));

tbproductos.setComponentPopupMenu(jPopupMenu1);

jScrollPane1.setViewportView(tbproductos);

getContentPane().add(jScrollPane1, new
org.netbeans.lib.awtextra.AbsoluteConstraints(12, 192, 412, 320));

txtbuscar.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

txtbuscarActionPerformed(evt);

}
});

getContentPane().add(txtbuscar, new org.netbeans.lib.awtextra.AbsoluteConstraints(440,


230, 135, -1));

jButton1.setText("buscar por codigo");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

});

getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(440,


190, -1, -1));

jLabel4.setText("Lenin Gutierrez");

getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(470,


420, -1, -1));

jLabel5.setText("Julio Perez");

getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(480,


380, -1, -1));

jButton5.setText("Modificar");

jButton5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton5ActionPerformed(evt);

});

getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(470,


270, -1, -1));

jButton6.setText("Salir");

jButton6.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {


jButton6ActionPerformed(evt);

});

getContentPane().add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(630,


450, 160, 51));

jLabel6.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/clases/banderin.png"))); // NOI18N

getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(610,


180, 200, 260));

jButton4.setText("Eliminar");

jButton4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton4ActionPerformed(evt);

});

getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(470,


310, -1, -1));

jTextField1.setText("Lista de Datos de los Clientes o ususarios");

jTextField1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jTextField1ActionPerformed(evt);

});

getContentPane().add(jTextField1, new
org.netbeans.lib.awtextra.AbsoluteConstraints(110, 160, 240, -1));

jMenu1.setText("File");

jMenu1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jMenu1ActionPerformed(evt);
}

});

jMenuItem3.setIcon(new
javax.swing.ImageIcon("C:\\Users\\Julioguitars\\Desktop\\iconfinder_Cancel_131742.png"));
// NOI18N

jMenuItem3.setText("Salir");

jMenuItem3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jMenuItem3ActionPerformed(evt);

});

jMenu1.add(jMenuItem3);

jMenuBar1.add(jMenu1);

jMenu2.setText("Informacion");

jMenuItem5.setIcon(new
javax.swing.ImageIcon("C:\\Users\\Julioguitars\\Desktop\\informacion.png")); // NOI18N

jMenuItem5.setText("Acerca de");

jMenuItem5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jMenuItem5ActionPerformed(evt);

});

jMenu2.add(jMenuItem5);

jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);
pack();

setLocationRelativeTo(null);

}// </editor-fold>

private void btngrabarActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

try {

PreparedStatement pst = cn.prepareStatement("INSERT INTO


productos(codpro,despro,prepro) VALUES (?,?,?)");

pst.setString(1, txtcod.getText());

pst.setString(2, txtdes.getText());

pst.setString(3, txtpre.getText());

pst.executeUpdate();

mostrardatos("");

} catch (Exception e) {

System.out.print(e.getMessage());

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

mostrardatos(txtbuscar.getText());

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int fila= tbproductos.getSelectedRow();

if(fila>=0){

txtcod.setText(tbproductos.getValueAt(fila, 0).toString());
txtdes.setText(tbproductos.getValueAt(fila, 1).toString());

txtpre.setText(tbproductos.getValueAt(fila, 2).toString());

else{

JOptionPane.showMessageDialog(null,"no seleciono fila");

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

try {

PreparedStatement pst = cn.prepareStatement("UPDATE productos SET


despro='"+txtdes.getText()+"',prepro='"+txtpre.getText()+"' WHERE
codpro='"+txtcod.getText()+"'");

pst.executeUpdate();

mostrardatos("");

} catch (Exception e) {

System.out.print(e.getMessage());

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int fila = tbproductos.getSelectedRow();

String cod="";

cod=tbproductos.getValueAt(fila, 0).toString();

try {

PreparedStatement pst = cn.prepareStatement("DELETE FROM productos WHERE


codpro='"+cod+"'");

pst.executeUpdate();

mostrardatos("");
} catch (Exception e) {

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {

int fila= tbproductos.getSelectedRow();

if(fila>=0){

txtcod.setText(tbproductos.getValueAt(fila, 0).toString());

txtdes.setText(tbproductos.getValueAt(fila, 1).toString());

txtpre.setText(tbproductos.getValueAt(fila, 2).toString());

else{

JOptionPane.showMessageDialog(null,"no seleciono fila");

private void txtbuscarActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {

dispose();

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

int fila = tbproductos.getSelectedRow();

String cod="";

cod=tbproductos.getValueAt(fila, 0).toString();

try {
PreparedStatement pst = cn.prepareStatement("DELETE FROM productos WHERE
codpro='"+cod+"'");

pst.executeUpdate();

mostrardatos("");

} catch (Exception e) {

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {

txtcod.setText("");

txtpre.setText("");

txtdes.setText("");

private void txtcodActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void txtcodKeyTyped(java.awt.event.KeyEvent evt) {

char txtcod=evt.getKeyChar();

if (Character.isLetter(txtcod)) {

getToolkit().beep();

evt.consume();

JOptionPane.showMessageDialog(rootPane,"Ingresar solo numeros");

private void txtpreActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}
private void txtpreKeyTyped(java.awt.event.KeyEvent evt) {

char txtpre=evt.getKeyChar();

if (Character.isLetter(txtpre)) {

getToolkit().beep();

evt.consume();

JOptionPane.showMessageDialog(rootPane,"Ingresar solo numeros");

// TODO add your handling code here:

private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {

dispose(); // TODO add your handling code here:

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {

dispose(); // TODO add your handling code here:

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {

JOptionPane.showMessageDialog(this, "UTA Ingenieria en Telecomunicaciones"); // TODO


add your handling code here:

/**

* @param args the command line arguments

*/
public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see


http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(ingresoproductos.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(ingresoproductos.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(ingresoproductos.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(ingresoproductos.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);

//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new ingresoproductos().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton btngrabar;

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton3;

private javax.swing.JButton jButton4;

private javax.swing.JButton jButton5;

private javax.swing.JButton jButton6;

private javax.swing.JButton jButton7;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel10;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JLabel jLabel5;

private javax.swing.JLabel jLabel6;

private javax.swing.JLabel jLabel7;

private javax.swing.JLabel jLabel8;

private javax.swing.JLabel jLabel9;

private javax.swing.JMenu jMenu1;

private javax.swing.JMenu jMenu2;

private javax.swing.JMenuBar jMenuBar1;

private javax.swing.JMenuItem jMenuItem1;

private javax.swing.JMenuItem jMenuItem2;

private javax.swing.JMenuItem jMenuItem3;


private javax.swing.JMenuItem jMenuItem5;

private javax.swing.JPanel jPanel1;

private javax.swing.JPopupMenu jPopupMenu1;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTextField jTextField1;

private javax.swing.JTable tbproductos;

private javax.swing.JTextField txtbuscar;

private javax.swing.JTextField txtcod;

private javax.swing.JTextField txtdes;

private javax.swing.JTextField txtpre;

// End of variables declaration

conectar cc= new conectar();

Connection cn= cc.conexion();