import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import static java.awt.print.Printable.NO_SUCH_PAGE;
import static java.awt.print.Printable.PAGE_EXISTS;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class frmBuscarBC extends javax.swing.JFrame {
coneBD cn=new coneBD();
DefaultTableModel modelo=new DefaultTableModel();
public frmBuscarBC() {
initComponents();
String titu[]={"IDProducto","Nombre","Precio"};
modelo.setColumnIdentifiers(titu);
tblPro.setModel(modelo);
tblPro.getColumnModel().getColumn(1).setPreferredWidth(200);
DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
cellRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
tblPro.getColumnModel().getColumn(2).setCellRenderer(cellRenderer);
}
private void txtBuscarActionPerformed(java.awt.event.ActionEvent evt) {
String SQL="select * from productos where barcode like '"+txtBuscar.getText()+"%';";
String datos[]=new String[3];
int op=0;
try{
cn.Conectar();
cn.st=cn.conec.createStatement();
cn.rt=cn.st.executeQuery(SQL);
while (cn.rt.next()){
datos[0]=cn.rt.getString(1);
datos[1]=cn.rt.getString(2);
datos[2]=cn.rt.getString(3);
modelo.addRow(datos);
op=1;
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
if (op==0) {
JOptionPane.showMessageDialog(null, "No tenemos el producto "+txtBuscar.getText());
} else {
calTot();
}
txtBuscar.setText(null);
}
private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum){
if (pageNum > 0){
return Printable.NO_SUCH_PAGE;
}
Font fuente = new Font("courier",Font.PLAIN,10);
Graphics2D g2 = (Graphics2D) pg;
g2.setFont(fuente);
g2.drawString("minuvaMarket", 100, 100);
int n = modelo.getRowCount(); int i=0;
for(i=0;i<n;i++) {
g2.drawString(""+modelo.getValueAt(i, 0).toString(), 100, i*20+125);
g2.drawString(""+modelo.getValueAt(i, 1).toString(), 200, i*20+125);
g2.drawString(""+modelo.getValueAt(i, 2).toString(),350, i*20+125);
}
jPanel1.paint(g2);
return Printable.PAGE_EXISTS;
}
});
try {
job.print();
} catch(PrinterException e) {
e.printStackTrace();
}
}
private void calTot() {
int n = modelo.getRowCount();
int i; double S=0; double tax=0; double tot=0;
for(i=0;i<n;i++) {
S = S + Double.parseDouble(modelo.getValueAt(i, 2).toString());
}
S = redo(S);
lblSub.setText(S+"");
tax = 0.1*S;
tax = redo(tax);
lblTax.setText(tax+"");
tot = S+tax;
tot = redo(tot);
lblTot.setText(tot+"");
}
private double redo(double num){
return Math.rint(num*100)/100;
}
Clase de Conexion coneBD.java
package pktForm12;
import java.sql.*;
import javax.swing.JOptionPane;
public class coneBD {
public static PreparedStatement prest=null;
public static CallableStatement cllst=null;
public static Connection conec=null;
public static Statement st=null;
public static ResultSet rt=null;
public static String usu;
public static String contra;
public static String host;
public static String db;
public void Conectar(){
try{
usu="root"; contra=""; host="localhost"; db="ventas";
String url="jdbc:mysql://"+host+"/"+db;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conec=DriverManager.getConnection(url,usu,contra);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.toString());
}
}
}
Tener Agregado en Libraries el conector mysql
Para Un Software Optimo comunicarse a minuvasoft10@gmail.com - Miguel Nunez