Wednesday, February 23, 2011

Generate PDF

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PDFServlet extends HttpServlet {
 public void init(ServletConfig config) throws ServletException {
  super.init(config);
 }
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doPost(request, response);
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String query = null;
  response.setContentType("application/pdf"); // Code 1
   //PrintWriter pw=response.getWriter();
  Document document = new Document();
  try {
   PdfWriter.getInstance(document, response.getOutputStream()); // Code
                   // 2
   document.open();
   try {
    int rcount = 0;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(
      "jdbc:oracle:thin:@localhost:1521", "hr", "tiger");
    query = "SELECT * FROM REGIONS";
    Statement state = conn.createStatement(
      ResultSet.CONCUR_UPDATABLE,
      ResultSet.TYPE_SCROLL_SENSITIVE);
    ResultSet rs = state.executeQuery(query);
    ResultSetMetaData mdata = rs.getMetaData();
    int colcount = mdata.getColumnCount();
    PdfPTable table = new PdfPTable(1);
    while (rs.next()) {
     rcount++;
    }
    rs.beforeFirst();
    rs.next();
    table.addCell(mdata.getColumnName(colcount));
    rs.beforeFirst();
    for (int i = 1; i <= rcount; i++) {
     for (int j = 1; j <= colcount-1; j++) {
    
      table.addCell(rs.getString("region_name"));
     }
    }
    document.add(table);
    document.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  } catch (DocumentException e) {
   e.printStackTrace();
  }
 }
}

Saturday, February 19, 2011

when click a radio button display related text boxes

<html>
<head>
<script language='javascript'>
function display(){   
     if(document.frmRadio.selType[0].checked){
       document.getElementById('div1').style.display = 'block';
       document.getElementById('div2').style.display = 'block' ;
     }
     else{
       document.getElementById('div2').style.display =  'none';
       document.getElementById('div1').style.display = 'block';
     }       
}
</script>
</head>
<body>
<form name='frmRadio'>
<tr><td><input type=radio name='selType' onclick='display()'> Display dropdown & textbox</td></tr>
<tr><td><input type=radio name='selType' onclick='display()'> Display Textbox Only</td></tr>
<tr height='30'><td>&nbsp;</td></tr>
<div id='div1' style=display:none>
<tr>
  <td><input type='text' name='txtName'></td>
</tr>
</div>
<div id='div2' style=display:none>
<tr>
  <td>
      <select name='selOption'>
         <option value='A'>A</option>
         <option value='B'>B</option>
      </select>
  </td>
</tr>
</div>
</form>
</body>
</html>

Wednesday, February 16, 2011

bat file of project

start firefox "http://localhost:9090/nani/query.html"

Tuesday, February 15, 2011

Swing table with file data

import java.awt.Color;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class InsertFileDataToJTable extends AbstractTableModel {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    Vector<String> data;
    Vector<String> columns;
    int i = 1;

    public InsertFileDataToJTable() {
        String line;
        data = new Vector<String>();
        columns = new Vector<String>();
        try {
            FileReader fr1 = new FileReader("D:\\abc.txt");
            BufferedReader br = new BufferedReader(fr1);
            StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
            /*while (st1.hasMoreTokens())
                columns.addElement(st1.nextToken());*/
            int f = 4,l=f+4;
            while (i < f) {
                br.readLine();
                i++;
            }
            while (f <= l) {
                if ((line = br.readLine()) != null) {
                    StringTokenizer st2 = new StringTokenizer(line, " ");
                    while (st2.hasMoreTokens())
                        data.addElement(st2.nextToken());
                    f++;
                }
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    protected String[] columnNames = new String[] {"ID", "NAME", "ADD"};
    public int getRowCount() {
        return data.size() / getColumnCount();
    }
    public String getColumnName(int col) { return columnNames[col]; }
    public int getColumnCount() {
        return columns.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        String name=(String) data.elementAt((rowIndex * getColumnCount())
                + columnIndex);
        return name;
    }

    public static void main(String s[]) {
        InsertFileDataToJTable model = new InsertFileDataToJTable();
        JTable table = new JTable();
        table.setModel(model);
        table.setBackground(Color.orange);
        JScrollPane scrollpane = new JScrollPane(table);
        JPanel panel = new JPanel();
        panel.add(scrollpane);
        JFrame frame = new JFrame();
        frame.add(panel, "Center");
        frame.pack();
        frame.setVisible(true);
      
    }
}