Предположим, что отчет содержит имя вашего файла отчета (* .jrxml).
Эта программа заполнит отчет данными из базы данных mysql.
Я надеюсь, что это решит вашу проблему.
public void generate(String report) { // report will contain the name of your file
try {
InputStream design = getClass().getResourceAsStream(report);
JasperDesign jasperdesing = JRXmlLoader.load(design);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperdesing);
Connection con = ConnectDB.connect();// connect function in ConnectDB calss is used to connect to the database
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
JasperViewer.viewReport(jasperPrint, false);
} catch (Exception e) {
System.out.println("Exception " + e);
}
}
public class ConnectDB {
public static Connection con = null;
public static Connection connect(){
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:///dbname","username","password");
}catch(Exception e){
System.out.println();
}
return con;
}
}