viernes, 18 de noviembre de 2011



package proreportes;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.*;

/**
*
* @author Ingeniero
*/
public class reportePrueba {
private Connection conn;
private final String login="root";
private final String password="";
private String url="jdbc:mysql://127.0.0.1/ejemplo";

public reportePrueba(){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,login,password);
}
catch(ClassNotFoundException ex){

}
catch(SQLException ex){

}
}
public void runReporte(){
try{
String master=System.getProperty("user.dir")+"/src/proreportes/reporte1.jasper";
System.out.println("master "+master);
if(master==null){
System.out.println("no encuentro el reporte");
System.exit(3);
}
JasperReport masterReport=null;
try{
masterReport=(JasperReport)JRLoader.loadObject(master);
}
catch(JRException e){
System.out.println("error cargando el reporte"+e.getMessage());
System.exit(3);
}
Map parametro=new HashMap();
String par="";
par=form1.para;
parametro.put("p1", par);
JasperPrint jasperPrint=JasperFillManager.fillReport(masterReport, parametro,conn);
JasperViewer jviewer=new JasperViewer(jasperPrint,false);
jviewer.setVisible(true);

}
catch(Exception j){
System.out.println("error "+j.getMessage());
}
}

public void cerrar(){
try{
conn.close();
}
catch(SQLException ex){
ex.printStackTrace();

}
}

}

viernes, 2 de septiembre de 2011

try {
int res=0;
String a=jTextField1.getText();
String b=jPasswordField1.getText();
String c=jComboBox1.getSelectedItem().toString();

if (a.equals("")||
b.equals("")||
c.equals(""))

JOptionPane.showMessageDialog(null,"no se aceptan campos vacios");
else{

res=st.executeUpdate(" INSERT INTO usuarios VALUES(null,'"
+ a + "','"
+ b + "','"
+ c + "') ");
}
if (res==1)
JOptionPane.showMessageDialog(null,"Guardó Correctamente");



}
catch(SQLException c){
JOptionPane.showMessageDialog(null,"Error"+c.getMessage());
}

try{
conn=Enlace(conn);
st=conn.createStatement();
//Se hace la consulta SQL
rs=st.executeQuery("Select * from usuarios");
//Se llena el Combo con los registros
rs.next();
// el (2) representa la columna de la tabla
jTextField1.setText(rs.getString(1));
jPasswordField1.setText(rs.getString(2));
jComboBox1.setSelectedItem(rs.getString(3));




}
catch(SQLException c){
JOptionPane.showMessageDialog(null,"Error"+c.getMessage());
}