Monday, September 29, 2014

SQL ORA-00979: not a GROUP BY expression

This exception occurs when the SELECT clause contains aggregate functions such as MAX, MIN, SUM, AVG, and COUNT, and there has no GROUP BY clause or the GROUP BY clause does not include all the items in the SELECT clause except the aggregate functions.

For example, the following queries throw such an exception.

1. SELECT NAME, DESCRIPTION, MAX(PRICE) PRICE FROM FRUIT WHERE PRICE < 1.33;

In this case, the query has an aggregate function MAX(PRICE) in the SELECT clause but does not have a GROUP BY clause.

2. SELECT NAME, DESCRIPTION, MAX(PRICE) PRICE FROM FRUIT WHERE PRICE < 1.33 GROUP BY NAME;

In this case, though the GROUP BY clause is there but it dose not contain all the non-aggregate items. The DESCRIPTION is missing from the GROUP BY clause.

To Fix this problem, you may add all the items in the SELECT clause to the GROUP BY clause . However, the GROUP BY clause must have all the non-aggregate items (both NAME and DESCRIPTION in the above queries).

------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Tuesday, September 23, 2014

DynamicReports: How to set the height/width/dimension of a sub-report?

Lets say that you have a subreport which displays a message. The length of the message can be from 0 to 20 lines. Following this message subreport, you have another subreport which displays some data and you would like it to be always displayed at the bottom one third of the page regardless the length of the message.

One way to make the second data subreport to be always displayed at a certain height of the page is to set the height of the first message subreport above it. The way to do it is to put the subreport in a vertical list or a horizontal list and set the height/width/dimension of that vertical list or horizontal list.


import java.util.ArrayList;
import java.util.List;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import static net.sf.dynamicreports.report.builder.DynamicReports.cmp;
import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.exp;
import static net.sf.dynamicreports.report.builder.DynamicReports.stl;
import net.sf.dynamicreports.report.builder.component.SubreportBuilder;
import net.sf.dynamicreports.report.builder.datatype.DataTypes;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class ReportDimensionDemo {
    public static void main(String[] args){
        new ReportDimensionDemo();
    }
 
    public ReportDimensionDemo() {
        JasperReportBuilder report = DynamicReports.report();
        report.setDataSource(createDataSource());
     
        SubreportBuilder messageSubreport = cmp.subreport(buildMessageSubreport())
                .setDataSource(exp.subDatasourceBeanCollection("messageData"));
        SubreportBuilder dataSubreport = cmp.subreport(buildDataSubreport())
                .setDataSource(exp.subDatasourceBeanCollection("dataData"));
     
        report.detail(cmp.verticalList(messageSubreport).setFixedHeight(300),
               // .setFixedWidth(200)
                //.setFixedDimension(200, 300)
                dataSubreport );
        try {
            report.show(false);
        }catch(DRException de){
            de.printStackTrace(System.out);
        }
    }
 
    private JasperReportBuilder buildMessageSubreport() {
        JasperReportBuilder report = DynamicReports.report();
        report.columns(col.column("message", DataTypes.stringType()));
     
        return report;
    }
 
    private JasperReportBuilder buildDataSubreport() {
        JasperReportBuilder report = DynamicReports.report();
        report.columns(col.column("Name", "name", DataTypes.stringType())
                    .setStyle(stl.style(stl.pen1Point())),
                col.column("Price", "price", DataTypes.doubleType())
                        .setStyle(stl.style(stl.pen1Point()))
                        .setPattern("$##0.00"));
     
        return report;
    }
 
    private JRDataSource createDataSource() {
        List<ReportData> dataSource = new ArrayList<ReportData>();
        ReportData reportData = new ReportData();
        List<MessageData> messageList = new ArrayList<MessageData>();
        MessageData messageData = new MessageData();
        List<DataData> dataList = new ArrayList<DataData>();
        DataData dataData = new DataData();
     
        messageData.setMessage("The most useful report in the universe.");
        messageList.add(messageData);
        reportData.setMessageData(messageList);
     
        dataData.setName("Apple");
        dataData.setPrice(1.68);
        dataList.add(dataData);
        dataData = new DataData();
        dataData.setName("Papaya");
        dataData.setPrice(3.14);
        dataList.add(dataData);
        reportData.setDataData(dataList);
     
        dataSource.add(reportData);
        return new JRBeanCollectionDataSource(dataSource);
    }
 
    public class ReportData {
        private List<MessageData> messageData;
        private List<DataData> dataData;
     
        public List<MessageData> getMessageData() {return messageData;}
        public void setMessageData(List<MessageData> m) {messageData=m;}
        public List<DataData> getDataData() {return dataData;}
        public void setDataData(List<DataData> d) {dataData=d;}
    }
 
    public class MessageData{
        private String message;
     
        public String getMessage() {return message;}
        public void setMessage(String m) {message=m;}
    }
 
    public class DataData {
        private String name;
        private Double price;
     
        public String getName() {return name;}
        public void setName(String n) {name=n;}
        public Double getPrice() {return price;}
        public void setPrice(Double p) {price=p;}
    }
}
   
-----------------------------------------------------------------------------------------------------------  

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Thursday, September 4, 2014

How to get current working directory in Java - resolved

The current working directory is the root directory of your java project. There are several ways to retrieve the current working directory.

1. System.getProperty("user.dir"))

           public class CurrentDirectoryTest {
                      public static void main(String[] args){
                              System.out.println("Current Directory is: "+System.getProperty("user.dir"));
                      }
           }

2. Paths.get("").toAbsolutePath().toString()
   
          public class CurrentDirectoryTest {
                      public static void main(String[] args){
                              System.out.println("Current Directory is: "+Paths.get("").toAbsolutePath().toString());
                      }
           }

3. (new File("")).getAbsolutePath()

           public class CurrentDirectoryTest {
                      public static void main(String[] args){
                              System.out.println("Current Directory is: "+(new File("")).getAbsolutePath());
                      }
           }

---------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Wednesday, September 3, 2014

NetBeans contains files with errors - resolved

If you have a class X in project B extends a class Y in project A, error badges may occur in project B after you modified class Y and rebuilt project A. When you point the cursor at the error badge, it says "Contains files with errors". When you open the java file of class X and point the cursor at the error line, it says "Class X cannot convert to class Y".

To fix this problem


  1. Right-click on project B and select "Porperties".
  2. Click "Libraries" on the left under Categories.
  3. Select the library or the jar of project A in the right pane and click the "Remove" button to remove it.
  4. Click "OK" to close the window.
  5. Restart NetBeans.
  6. Repeat steps 1 and 2.
  7. Click the "Add Library" or "Add JAR/Folder" button to add the library or the jar of project A back to project B.         
-----------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.