Sie sind auf Seite 1von 11

//conexion a base de datos 'bferreteria' funciona perfecto. Gracias a D-S.

Gracias
a todos.
import java.awt.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;

public class sqlbase1 extends Formulario1 //sqlbase1


{
Conectar3 conn2 = new Conectar3();
DefaultTableModel modelo = new DefaultTableModel();

public static void main(String[] args)


{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
new sqlbase1();
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
//******** ****** ******

public sqlbase1()
{
modelo.addColumn("Cod");
modelo.addColumn("Cant");
modelo.addColumn("Detalle");
modelo.addColumn("V/u");
modelo.addColumn("Subt");
modelo.addColumn("iva(16%)");
modelo.addColumn("Total");

this.tbproductos.setModel(modelo);
JScrollPane1.setViewportView(tbproductos);

//***** *********** *******

bconsultar.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
bconsultarActionPerformed(evt);
}
});;
baceptar.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
baceptarActionPerformed(evt);
}
});;

/* JMenuItem1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
JMenuItem1ActionPerformed(evt);
}
});;

beliminartodos.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
beliminartodosActionPerformed(evt);
}
});;*/

bgrabar.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
bgrabarActionPerformed(evt);
}
});;

tbproductos.setComponentPopupMenu(JPopupMenu1);
JScrollPane1.setViewportView(tbproductos);
} // de contructor
//************ ************ ************

private void bconsultarActionPerformed(java.awt.event.ActionEvent evt)


{
try
{
String sql = "Select * from productos where codigo = ?";
Connection conexion = conn2.getConnction();
PreparedStatement ps = conexion.prepareStatement(sql);
ps.setString(1,txtcodigo.getText());
ResultSet rs = ps.executeQuery();

if(rs.next())
{
txtproducto.setText(rs.getString(2));
txtvunitario.setText(rs.getString(3));
}
else
{
labelresultado.setText("No existe un registro con dicho codigo");
}
//conexion.close();
}//de try
catch(SQLException ex)
{
Logger.getLogger(sqlbase1.class.getName()).log(Level.SEVERE,null,ex);
}
}

private void baceptarActionPerformed(java.awt.event.ActionEvent evt)


{
String cad1 = txtvunitario.getText();
String cad2 = txtcantidad.getText();
double vunit = Double.parseDouble(cad1);
int cant = Integer.parseInt(cad2);
double iva; double subt; double total; double suma = 0; double gtotal = 0;
labelresultado.setText("");
total = cant*vunit;
iva = total*(0.16);
subt = total-iva;

txtsubtotal.setText(String.valueOf(subt)); //t1.setText(String.valueOf(res));
txtiva.setText(String.valueOf(iva));
txttotal.setText(String.valueOf(total));

String []datos = new String[7];

datos[0] = txtcodigo.getText();
txtcodigo.setText(null);
datos[1] = txtcantidad.getText();
txtcantidad.setText(null);
datos[2] = txtproducto.getText();
txtproducto.setText(null);
datos[3] = txtvunitario.getText();
txtvunitario.setText(null);
datos[4] = txtsubtotal.getText();
txtsubtotal.setText(null);
datos[5] = txtiva.getText();
txtiva.setText(null);
datos[6] = txttotal.getText();
txttotal.setText(null);
modelo.addRow(datos);
//*** *** ***

for(int i = 0; i<tbproductos.getRowCount(); i++)


{
String precio = tbproductos.getValueAt(i,3).toString();
String cantidad = tbproductos.getValueAt(i,1).toString();
int c = Integer.parseInt(cantidad);
double p = Double.parseDouble(precio);
suma = c*p;
gtotal = gtotal+suma;

txtgrantotal.setText(String.valueOf(gtotal));
}
}

/* private void JMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)


{
int filaseleccionada = tbproductos.getSelectedRow();
if(filaseleccionada >= 0)
{
modelo.removeRow(filaseleccionada);
}
else
{
JOptionPane.showMessageDialog(null,"no selecciono ninguna fila");
}
}*/

/* private void beliminartodosActionPerformed(java.awt.event.ActionEvent evt)


{
int cantidadfilas = tbproductos.getRowCount();
for(int i = cantidadfilas-1; i>=0; i--)
{
modelo.removeRow(i);
}
labelresultado.setText("");
}*/

private void bgrabarActionPerformed(java.awt.event.ActionEvent evt)


{
for(int i=0; i<tbproductos.getRowCount(); i++)
{
String cad1 = tbproductos.getValueAt(i,0).toString();
txtcodigo.setText(String.valueOf(cad1));
try
{
String sql = "Select * from productosv where codigo = ?";
Connection conexion = conn2.getConnction();
PreparedStatement ps = conexion.prepareStatement(sql);
ps.setString(1,txtcodigo.getText());
ResultSet rs = ps.executeQuery();
if(rs.next())
{
txtcantidad.setText(rs.getString(3));
txttotal.setText(rs.getString(4));
}
//conexion.close();
}//de try
catch(SQLException ex)
{
Logger.getLogger(sqlbase1.class.getName()).log(Level.SEVERE,null,ex);

}
hacercalculos(i);
enviardatos();
} //de for

totalventas();

txtcodigo.setText("");
txtcantidad.setText("");
txttotal.setText("");
txtgrantotal.setText("");
}
//****** ***** *****

private void hacercalculos(int i)


{
String cad1 = tbproductos.getValueAt(i,1).toString();
String cad2 = tbproductos.getValueAt(i,6).toString();
int cantt = Integer.parseInt(cad1);
double totalt = Double.parseDouble(cad2);
String cad3 = txtcantidad.getText();
String cad4 = txttotal.getText();
int cantb = Integer.parseInt(cad3);
double totalb = Double.parseDouble(cad4);
int gcant = 0;
double gtotal = 0;
gcant = cantt + cantb;
gtotal = (totalt + totalb);

txtcantidad.setText(String.valueOf(gcant));
txttotal.setText(String.valueOf(gtotal));
}

private void enviardatos()


{
try
{
String sql = "Update productosv set cantidad = ?, total = ? where codigo
= ?";
Connection conexion = conn2.getConnction();
PreparedStatement ps;
ps = conexion.prepareStatement(sql);
ps.setString(1,txtcantidad.getText());
ps.setString(2,txttotal.getText());
ps.setString(3,txtcodigo.getText());

ps.executeUpdate();
//conexion.close();
}//de try
catch(Exception e)
{
System.out.print(e.getMessage());
}
}

private void totalventas()


{
try
{
String sql = "Select * from totalventas";
Connection conexion = conn2.getConnction();
Statement ps = conexion.prepareStatement(sql); //
ps.setString(1,txtcodigo.getText());
ps.executeQuery(sql);
ResultSet rs = ps.getResultSet();

if(rs.next())
{
txttotalventas.setText(rs.getString(2));
}
//conexion.close();
}//de try
catch(SQLException ex)
{
Logger.getLogger(sqlbase1.class.getName()).log(Level.SEVERE,null,ex);
}
//*** ***
String cad1 = txtgrantotal.getText();
String cad2 = txttotalventas.getText();
double gtotal = Double.parseDouble(cad1);
double totalv = Double.parseDouble(cad2);
totalv = (gtotal + totalv);
txttotalventas.setText(String.valueOf(totalv));

try
{
String sql = " Update totalventas set totalv = ?";
Connection conexion = conn2.getConnction();
PreparedStatement ps = conexion.prepareStatement(sql);
ps.setString(1,txttotalventas.getText());
ps.executeUpdate();
//conexion.close();
}
catch(Exception e)
{
System.out.print(e.getMessage());
}
labelresultado.setText("se grabaron los datos");
} //de void totalventas
}//de clase principal

Das könnte Ihnen auch gefallen