Thursday, May 13, 2021

19 - net sf jasperreports engine JRException Error retrieving field valu...

18 - Generate PDF with Jasperreport and Spring Boot SOLVED


In this tutorial, I show you how to generate a PDF with Jasperreport and Spring Boot .
application.properties:
   
 spring.datasource.url=jdbc:mysql://localhost:3306/YOURDATABASE?autoReconnect=true
spring.datasource.username=YOURUSERNAME
spring.datasource.password=YOURPASSWORD
spring.jpa.generate-ddl=true
	
   
  
POM.xml:
   



		<dependency>
    		<groupId>net.sf.jasperreports</groupId>
    		<artifactId>jasperreports</artifactId>
    		<version>6.16.0</version>
		</dependency>
		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency> 
		
   
  
Main Java Class:
  
  package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServiceJpasql001Application {

	public static void main(String[] args) {
		SpringApplication.run(ServiceJpasql001Application.class, args);
	}

}

  
Student Class:
  
  
  package com.example.demo.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student{
 
	@Id
    @Column(name="stid")
	private int stid;
	
    @Column(name="name")
	private String name;
    
    @Column(name="programme")
	private String Programme;

	public int getStid() {
		return stid;
	}

	public void setStid(int stid) {
		this.stid = stid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProgramme() {
		return Programme;
	}

	public void setProgramme(String programme) {
		Programme = programme;
	}

	public Student(int stid, String name, String programme) {
		super();
		this.stid = stid;
		this.name = name;
		Programme = programme;
	}

	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	  
}
  
  
StudentRepository :
  
  
  package com.example.demo.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.demo.model.Student;

public interface StudentRepository extends JpaRepository {
  	List findByName(String name);
}
  
  
StudentService:
  
  
  package com.example.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;

@Service
public class StudentService {

	@Autowired
	private StudentRepository studentRepository;
	
	public Student addStudent(Student student) {
		
		return studentRepository.save(student);
	}
	
	
  public void deleteStudent(Student student) {
		
		 studentRepository.delete(student);
	}
  
	public List getStudent() {
		
		return studentRepository.findAll();
	}
	
    public List getStudentByName(String name) {
		
		return studentRepository.findByName(name);
	}
}
  
  
StudentController:
  
  
  package com.example.demo.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.Student;
import com.example.demo.service.StudentService;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;

@RestController
public class StudentController {

	
	@Autowired
	StudentService studentService;
	
	@RequestMapping("/all")
	public List getStudent() {		
		return studentService.getStudent();
	}
	
	@RequestMapping("/studentByName/{name}")
	public List getStudentByName(@PathVariable String name) {		
		return studentService.getStudentByName(name);
	}
	
	@RequestMapping("/save")
	public  Student  saveStudent(@RequestBody Student student) {		
		return studentService.addStudent(student);
	}
	
	@RequestMapping("/delete")
	public  void  deleteStudent(@RequestBody Student student) {		
		 studentService.deleteStudent(student);
	}
	
	
	@RequestMapping("/pdf")
	
	  public void getReportsinPDF(HttpServletResponse response) throws JRException, IOException {
	    	
		 //Compiled report
		InputStream jasperStream = (InputStream) this.getClass().getResourceAsStream("/StudentList.jasper");
	      
	    //Adding attribute names
	    Map params = new HashMap<>();
	    params.put("stid","stid");
	    params.put("name","name");
	    params.put("programme","programme");
	    
	   // Fetching the student from the data database.
        final JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(studentService.getStudent());
        
	    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
	    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, source);

	    response.setContentType("application/x-pdf");
	    response.setHeader("Content-disposition", "inline; filename=StudentList.pdf");

	    final ServletOutputStream outStream = response.getOutputStream();
	    JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
	  }
}

  
  

17 - Unable to locate Attribute with the the given name XXX on this Man...

Dev Tips#64 Connect Jaspersoft Studio to a JDBC Database Data Adapter li...

Sunday, May 2, 2021

blender#010 Introduction to collection layer in Blender

blender#009 Introduction to scene in Blender for beginner​

blender#008 How to assign a material to an object in Blender

blender#007 How to split your workspace into many windows in Blender fo...

blender#006 What can you do in Blender

blender#005 How to change workspaces in Blender for beginner

blender#004 Add and delete objects in Blender

blender#003 Introduction to Modes in Blender