【问题标题】:Issue in JFreeChart and HibernateJFreeChart 和 Hibernate 中的问题
【发布时间】:2020-07-12 06:06:13
【问题描述】:

有人可以帮我解决这个问题吗?我正在尝试使用休眠数据库创建图表,但在 for 循环“实际参数列表和形式参数列表长度不同”中出现错误。我尝试在线查找此错误,但没有得到任何解决方案。我决定从这里寻求帮助。任何帮助将不胜感激

这是我的实体类的代码:

class Student1
{
    private int rno,marks1,marks2,marks3;
    private String name;

public Student1() {  }

public Student1(int rno,String name,int marks1,int marks2,int marks3)
{
    this.rno = rno;
    this.name = name;
    this.marks1 = marks1;
    this.marks2 = marks2;
    this.marks3 = marks3;

 //getter and setters
}

方法图代码如下:

public static void chartsStudent()
{

    DefaultCategoryDataset d1 = new DefaultCategoryDataset();

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");

    SessionFactory sfact = cfg.buildSessionFactory();
    Session session = null;

    try
    {
        session = sfact.openSession();
        System.out.println("connected");
        session.beginTransaction();

        List<Student1> student1list = new ArrayList<>();
        student1list = session.createQuery("from Student1").list();
        
        for(Student1 b : student1list)
        {
            d1.setValue(b.getName(),b.getMarks1(),b.getMarks2(),b.getMarks3());
        }
        
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
    finally
    {
        if (session !=null)
        {
            session.close();
            System.out.println("disconnected");
        }
    }
JFreeChart chart = ChartFactory.createBarChart("Student Preformance",
"Subjects","Marks",d1,PlotOrientation.VERTICAL,true,true,false);
int width = 400;
int height = 400;
}

错误

【问题讨论】:

  • 根据DefaultCategoryDataset.setvalue()的api,这个函数没有String, int, int, int作为参数。
  • 那我应该使用什么函数而不是设置值,实际上我在这方面是全新的。@Ynjxsjmh

标签: java hibernate jfreechart


【解决方案1】:

如here 所述,添加或设置值的DefaultCategoryDataset 突变器都采用三个 参数,而不是四个。每个value 是感兴趣的Number,每个rowKey 是一个系列,每个columnKey 是系列中的一个元素。例如,

public void setValue(double value, Comparable rowKey, Comparable columnKey)

根据您的Student 班级,每个值是成绩/分数,每个系列 (rowKey) 是学生的姓名,每个元素 (columnKey) 是一个作业/测试。

d1.setValue(b.getMarks1(), b.getName(), "Test1");
d1.setValue(b.getMarks2(), b.getName(), "Test2");
…

或者,JDBCCategoryDataset 在一个步骤中从 SQL 查询构建 CategoryDataset; source 就是一个例子;更多可以找到here。

【讨论】:

  • 好吧,我的错误已经消失,图表也正在保存,但它没有显示任何想法为什么?@trashgod
  • @KomalPatil:我不确定;验证数据是否在数据库和您的List&lt;Student1&gt; 中;如果遇到困难,请发帖 minimal reproducible example 和 screenshot。
  • 好的,完成了。非常感谢您的帮助:)@trashgod
  • @KomalPatil:很高兴你把它整理好了;如果您想详细说明,请随时编辑您的问题或answer your own question。
  • 嘿,我想再问一个问题,我应该如何验证我的名字,rno,java swings 中的标记?@trashgod
猜你喜欢
  • 2011-09-03
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多