Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ara; import net.proteanit.sql.DbUtils; import java.sql.DriverManager; import java.sql.SQLException; import java.awt.*; import javax.swing.*; import java.sql.Connection; import java.sql.*; import java.sql.Driver; import net.proteanit.sql.DbUtils; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Chapter; import com.itextpdf.text.Font; import com.itextpdf.text.List; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.Anchor; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.Chunk; import com.itextpdf.text.FontFactory; import com.itextpdf.text.PageSize; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Rectangle; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileOutputStream; import com.itextpdf.text.Element; import java.io.IOException; import java.util.Date; import java.util.Random; /** * * @author Anand */ public class Confirmsec extends javax.swing.JFrame { Connection connection = null; ResultSet rs = null; PreparedStatement ps = null; /** * Creates new form Confirmsec */ public Confirmsec() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); preres = new javax.swing.JTextArea(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); tx1 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel1.setText("Confirm Section"); preres.setEditable(false); preres.setColumns(20); preres.setRows(5); jScrollPane1.setViewportView(preres); jButton1.setText("Correct Data Continue"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Abort Operation"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); tx1.setEditable(false); jLabel2.setText("Welcome"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(44, 44, 44).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 163, Short.MAX_VALUE) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(tx1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addGap(101, 101, 101).addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton2))) .addGap(20, 20, 20))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(34, 34, 34) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(tx1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1).addComponent(jButton2)) .addContainerGap(39, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed home1 home = new home1(); home.setVisible(true); }//GEN-LAST:event_jButton2ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed Random t = new Random(); c = t.nextInt(10000); String sql = "UPDATE uimsdetails set verid=? WHERE uid=?"; String jdbcClassName = "com.mysql.jdbc.Driver"; try { Class.forName(jdbcClassName); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/5sem", "root", ""); ps = connection.prepareStatement(sql); ps.setInt(1, c); ps.setString(2, tx1.getText()); int i = ps.executeUpdate(); if (i == 0) { JOptionPane.showMessageDialog(null, "Document Created."); } } catch (SQLException | ClassNotFoundException e) { JOptionPane.showMessageDialog(null, e); System.out.println(e); } try { myfunction(); } catch (Exception e) { System.out.println(e); } }//GEN-LAST:event_jButton1ActionPerformed /** * @param args the command line arguments */ public static final String DEST = "C:\\Users\\Anand\\Documents\\NetBeansProjects\\ARA\\Bonafide.pdf"; int c; public static void main(String args[]) throws IOException, DocumentException { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Confirmsec.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Confirmsec.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Confirmsec.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Confirmsec.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Confirmsec().setVisible(true); } }); } public void myfunction() throws IOException, DocumentException { String a = preres.getText(); File file = new File(DEST); file.getParentFile().mkdirs(); Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(DEST)); document.open(); Image image = Image.getInstance("logo.jpg"); document.add(image); Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD); Font paragraphFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL); Font small = FontFactory.getFont(FontFactory.HELVETICA, 5, Font.BOLD); String date = new Date().toString(); Paragraph paragraph1 = new Paragraph(date); Paragraph paragraph2 = new Paragraph("To Whom it May Concern", chapterFont); paragraph2.setAlignment(Element.ALIGN_CENTER); paragraph1.setAlignment(Element.ALIGN_RIGHT); paragraph2.setSpacingBefore(20f); paragraph2.setSpacingAfter(10f); Paragraph paragraph3 = new Paragraph(a, paragraphFont); paragraph3.setSpacingAfter(20f); Paragraph paragraph4 = new Paragraph("Registrar"); paragraph4.setAlignment(Element.ALIGN_RIGHT); paragraph4.setSpacingAfter(100f); Paragraph paragraph5 = new Paragraph( "This is a digitally signed document. Use Ref No. " + c + " to verify.", small); document.add(paragraph1); document.add(paragraph2); document.add(paragraph3); document.add(paragraph4); document.add(paragraph5); document.close(); JOptionPane.showMessageDialog(null, "Document Generated."); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; public static javax.swing.JTextArea preres; public static javax.swing.JTextField tx1; // End of variables declaration//GEN-END:variables }