public static JasperDesign createJasperDesign() throws JRException {

    // JasperDesign
    JasperDesign jasperDesign = new JasperDesign();
    jasperDesign.setName("ReqIF_Report");
    jasperDesign.setPageWidth(595);
    jasperDesign.setPageHeight(842);
    jasperDesign.setColumnWidth(515);
    jasperDesign.setColumnSpacing(0);
    jasperDesign.setLeftMargin(40);
    jasperDesign.setRightMargin(40);
    jasperDesign.setTopMargin(50);
    jasperDesign.setBottomMargin(50);

    // Fonts
    JRDesignStyle normalStyle = createNormalStyle(9);
    jasperDesign.addStyle(normalStyle);

    JRDesignStyle boldStyle = createBoldStyle(9);
    jasperDesign.addStyle(boldStyle);

    JRDesignStyle italicStyle = createItalicStyle(9);
    jasperDesign.addStyle(italicStyle);

    // Parameters
    JRDesignParameter parameter = new JRDesignParameter();
    parameter.setName("ReportTitle");
    parameter.setValueClass(java.lang.String.class);
    jasperDesign.addParameter(parameter);

    // Query
    JRDesignQuery query = new JRDesignQuery();
    query.setText("");
    jasperDesign.setQuery(query);
    return jasperDesign;
  }
  public static JasperDesign getJasperDesign() throws JRException {

    // JasperDesign
    JasperDesign jasperDesign = new JasperDesign();
    jasperDesign.setName("ReqIF_Report");
    jasperDesign.setPageWidth(595);
    jasperDesign.setPageHeight(842);
    jasperDesign.setColumnWidth(515);
    jasperDesign.setColumnSpacing(0);
    jasperDesign.setLeftMargin(40);
    jasperDesign.setRightMargin(40);
    jasperDesign.setTopMargin(50);
    jasperDesign.setBottomMargin(50);

    // Fonts
    JRDesignStyle normalStyle = createNormalStyle(9);
    jasperDesign.addStyle(normalStyle);

    JRDesignStyle boldStyle = createBoldStyle(9);
    jasperDesign.addStyle(boldStyle);

    JRDesignStyle italicStyle = createItalicStyle(9);
    jasperDesign.addStyle(italicStyle);

    // Parameters
    JRDesignParameter parameter = new JRDesignParameter();
    parameter.setName("ReportTitle");
    parameter.setValueClass(java.lang.String.class);
    jasperDesign.addParameter(parameter);

    // Query
    JRDesignQuery query = new JRDesignQuery();
    query.setText("");
    jasperDesign.setQuery(query);

    // Fields
    JRDesignField field = createField("Id");
    jasperDesign.addField(field);

    field = createField("FirstName");
    jasperDesign.addField(field);

    field = createField("LastName");
    jasperDesign.addField(field);

    field = createField("City");
    jasperDesign.addField(field);

    field = createField("Street");
    jasperDesign.addField(field);
    // Bands

    JRDesignBand band = new JRDesignBand();
    band.setHeight(20);

    JRDesignTextField textField = createTextField(0, 4);

    band.addElement(textField);
    JRDesignLine line = createLine(0, 19);
    band.addElement(line);

    band = new JRDesignBand();
    band.setHeight(20);
    line = createLine(0, -1);
    band.addElement(line);

    JRDesignStaticText staticText = new JRDesignStaticText();

    //		band.addElement(staticText);

    //		band.addElement(textField);

    // Page header
    band = new JRDesignBand();
    band.setHeight(20);
    JRDesignFrame frame = new JRDesignFrame();
    frame.setX(0);
    frame.setY(5);
    frame.setWidth(515);
    frame.setHeight(15);
    frame.setForecolor(new Color(0x33, 0x33, 0x33));
    frame.setBackcolor(new Color(0x33, 0x33, 0x33));
    frame.setMode(ModeEnum.OPAQUE);
    band.addElement(frame);

    staticText = createStaticText("ID", 0, 0);
    staticText.setStyle(createBoldStyle(9));
    frame.addElement(staticText);

    staticText = createStaticText("Name", 111, 0);
    staticText.setStyle(createBoldStyle(9));
    frame.addElement(staticText);

    staticText = createStaticText("Street", 260, 0);
    staticText.setStyle(createBoldStyle(9));
    frame.addElement(staticText);

    jasperDesign.setPageHeader(band);

    // Column header
    band = new JRDesignBand();
    jasperDesign.setColumnHeader(band);

    // Detail
    band = new JRDesignBand();
    band.setHeight(20);

    textField = new JRDesignTextField();
    textField.setX(0);
    textField.setY(4);
    textField.setWidth(50);
    textField.setHeight(15);
    textField.setHorizontalAlignment(HorizontalAlignEnum.RIGHT);
    textField.setStyle(normalStyle);

    JRDesignExpression expression = new JRDesignExpression();
    expression.setText("$F{Id}");
    textField.setExpression(expression);
    band.addElement(textField);

    textField = new JRDesignTextField();
    textField.setStretchWithOverflow(true);
    textField.setX(55);
    textField.setY(4);
    textField.setWidth(200);
    textField.setHeight(15);
    textField.setPositionType(PositionTypeEnum.FLOAT);
    textField.setStyle(normalStyle);
    band.addElement(textField);
    textField = new JRDesignTextField();
    textField.setStretchWithOverflow(true);
    textField.setX(260);
    textField.setY(4);
    textField.setWidth(255);
    textField.setHeight(15);
    textField.setPositionType(PositionTypeEnum.FLOAT);
    textField.setStyle(normalStyle);
    band.addElement(textField);

    line = new JRDesignLine();
    line.setX(0);
    line.setY(19);
    line.setWidth(515);
    line.setHeight(0);
    line.setForecolor(new Color(0x80, 0x80, 0x80));
    line.setPositionType(PositionTypeEnum.FLOAT);
    band.addElement(line);

    ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);

    // Column footer
    band = new JRDesignBand();
    // jasperDesign.setColumnFooter(band);

    // Page footer
    band = new JRDesignBand();
    // jasperDesign.setPageFooter(band);

    // Summary
    band = new JRDesignBand();
    // jasperDesign.setSummary(band);

    return jasperDesign;
  }
Beispiel #3
0
  private void create_bottonActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_create_bottonActionPerformed
    // TODO add your handling code here:
    try {
      /*
          Connection conn =  DriverManager.getConnection("jdbc:sqlite:C:/Documents and Settings/ishant0/bonafide.db");

         JasperDesign jasperDesign = JRXmlLoader.load("E:\\PD\\pd\\pratice\\Bonafide\\src\\bonafide\\report1.jrxml");
         String name = Second.getcdt();
         //System.out.print(name);
         String sql = "select * from firstInfo order by date DESC LIMIT 1";


         /*java.util.Map parameters = new java.util.HashMap();
         parameters.put("name", name);
         String abc = "Hey now please work properly!!";
         parameters.put("abc", abc);
          *


         JRDesignQuery newQuery = new JRDesignQuery();
         newQuery.setText(sql);
         jasperDesign.setQuery(newQuery);
         JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
         JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
         JasperViewer.viewReport(jasperPrint);

      }
      catch (Exception ex)
      {
          JOptionPane.showMessageDialog(null, ex);
      }
       */

      // Commented on 18-june-2013
      /*
        ps = con.prepareStatement("select * from abc"); //Table abc contains formate for certificates..............
        /* now use type table with typeName and formate datafield.....
         *

        rs = ps.executeQuery();
        rs.next();
        String s = rs.getString(1);

            rs.close();
            ps.close();



        st = con.createStatement();
        ResultSet rs2= st.executeQuery("select * from firstInfo order by date desc limit 1");
        rs2.next();
        String name = rs2.getString("name");
        String rollnum = rs2.getString("rollnum");



        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(java.util.regex.Pattern.quote("name??"));
        String data[] = pattern.split(s);
        String o1 = data[0] +" "+name+" "+data[1];

        pattern = java.util.regex.Pattern.compile(java.util.regex.Pattern.quote("roll??"));
        data = pattern.split(o1);
        String op = data[0] +" "+rollnum+" "+data[1];
        //System.out.println(op);

      */

      // NWE CODE:
      // Step-1  Fetching formate from type table.
      try {
        con = Connect.getConnection();

        ps = con.prepareStatement("select formate from type where typeName = 'Student Bonafide'");
        rs = ps.executeQuery();
        if (rs.next()) {
          formate = rs.getString("formate");
        } else {
          javax.swing.JOptionPane.showMessageDialog(
              null, "Internal error formate type or Certificate type not found.");
        }

        Connect.closeConnection(con, ps, rs);
        System.out.println("Step 1 completed!!!");
      } catch (Exception e) {
        javax.swing.JOptionPane.showMessageDialog(null, "Closing rs or ps for step 1" + e);
      }

      // Step-2 Fetching data which is last entered in firstInfo table.
      try {
        con = Connect.getConnection();

        ps = con.prepareStatement("select * from firstInfo order by date desc limit 1");
        java.sql.ResultSet rs2 = ps.executeQuery();
        rs2.next();
        name = rs2.getString("name");
        rollnum = rs2.getString("rollnum");
        father_name = rs2.getString("father_name");
        course = rs2.getString("course");
        semester = rs2.getString("sem");

        // System.out.println(name+rollnum+father_name);
        Connect.closeConnection(con, ps, rs2);
        System.out.println("Step 2 completed!!!");
      } catch (Exception e) {
        javax.swing.JOptionPane.showMessageDialog(null, "Problem in step 2" + e);
      }

      // Step-3 Merging data with formate
      try {
        to_deliver = Connect.splitJoin(formate, "{name}", name);
        to_deliver = Connect.splitJoin(to_deliver, "{roll}", rollnum);
        to_deliver = Connect.splitJoin(to_deliver, "(father}", father_name);
        to_deliver = Connect.splitJoin(to_deliver, "{course}", course);
        to_deliver = Connect.splitJoin(to_deliver, "{semester}", semester);
        System.out.println(to_deliver);
        System.out.println("Step 3 completed!!!");
      } catch (Exception e) {
        javax.swing.JOptionPane.showMessageDialog(null, "Problem while merging!" + e);
      }

      // Sending this above result op to toDeliver table:
      try {
        con = Connect.getConnection();
        ps = con.prepareStatement("insert into toDeliver (op) values(?)");
        ps.setString(1, to_deliver);
        ps.executeUpdate();
        Connect.closeConnection(con, ps, null);
      } catch (Exception e) {
        javax.swing.JOptionPane.showMessageDialog(null, "Problem in delivering data" + e);
      }

      // Now code for jasper reports:
      Connection conn =
          DriverManager.getConnection("jdbc:sqlite:C:/Documents and Settings/ishant0/bonafide.db");
      JasperDesign jasperDesign =
          JRXmlLoader.load("E:\\PD\\pd\\pratice\\Bonafide\\src\\bonafide\\report1.jrxml");
      String sql = "select * from toDeliver order by date DESC LIMIT 1";

      JRDesignQuery newQuery = new JRDesignQuery();
      newQuery.setText(sql);
      jasperDesign.setQuery(newQuery);
      JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
      JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
      JasperViewer.viewReport(jasperPrint);

      // con.close();

    } catch (Exception e) {
      javax.swing.JOptionPane.showMessageDialog(null, e);
    }
  } // GEN-LAST:event_create_bottonActionPerformed