Friday, December 27, 2013

insert records using jsp




form.html

<html>
<form method="post" action="http://localhost:8080/examples/jsp/insert.jsp">
<table>
<tr><td>Name:</td><td><input type="text" name="name"></td></tr>
<tr><td>Address:</td><td><input type="text" name="address"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>


insert.jsp


<%@page import="java.sql.*"%><%
String name=request.getParameter("name");
String address=request.getParameter("address");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           Connection con = DriverManager.getConnection("jdbc:odbc:student");
           Statement st=con.createStatement();
           int i=st.executeUpdate("insert into user(name,address) values('"+name+"','"+address+"')");
           out.println("Data is inserted successfully");
%>




for more

visit:
http://docs.oracle.com/cd/A87860_01/doc/java.817/a83726/sampapp2.htm



















JDBC Example using jsp

Simple Query--SimpleQuery.jsp

This page executes a simple query of the scott.emp table, listing employees and their salaries in an HTML table (ordered by employee name).

<%@ page import="java.sql.*" %>

<!------------------------------------------------------------------
 * This is a basic JavaServer Page that does a JDBC query on the
 * emp table in schema scott and outputs the result in an html table.
 *
--------------------------------------------------------------------!>
<HTML>
  <HEAD>
    <TITLE>
       SimpleQuery JSP
    </TITLE>
  </HEAD>
 <BODY BGCOLOR=EOFFFO>
 <H1> Hello
  <%= (request.getRemoteUser() != null? ", " + request.getRemoteUser() : "") %>
 !  I am SimpleQuery JSP.
 </H1>
 <HR>
 <B> I will do a basic JDBC query to get employee data
     from EMP table in schema SCOTT..
 </B>

 <P>
<%
    try {
      // Use the following 2 files whening running inside Oracle 8i
      // Connection conn = new oracle.jdbc.driver.OracleDriver().
      //                     defaultConnection ();
      Connection  conn =
          DriverManager.getConnection((String)session.getValue("connStr"),
                                           "scott", "tiger");
      Statement stmt = conn.createStatement ();
      ResultSet rset = stmt.executeQuery ("SELECT ename, sal " +
                           "FROM scott.emp ORDER BY ename");
      if (rset.next()) {
%>
     <TABLE BORDER=1 BGCOLOR="C0C0C0">
     <TH WIDTH=200 BGCOLOR="white"> <I>Employee Name</I> </TH>
     <TH WIDTH=100 BGCOLOR="white"> <I>Salary</I> </TH>
     <TR> <TD ALIGN=CENTER> <%= rset.getString(1) %> </TD>
          <TD ALIGN=CENTER> $<%= rset.getDouble(2) %> </TD>
     </TR>

<%     while (rset.next()) {
%>

     <TR> <TD ALIGN=CENTER> <%= rset.getString(1) %> </TD>
          <TD ALIGN=CENTER> $<%= rset.getDouble(2) %> </TD>
     </TR>

<% }
%>
      </TABLE>
<%  }
      else {
%>
     <P> Sorry, the query returned no rows! </P>

<%
      }
      rset.close();
      stmt.close();
    } catch (SQLException e) {
      out.println("<P>" + "There was an error doing the query:");
      out.println ("<PRE>" + e + "</PRE> \n <P>");
    }
%>

 </BODY>
</HTML>

Thursday, December 26, 2013

inserting image

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;



public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showImageDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }      
      });  
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();      
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);
   }

   private void showImageDemo(){
      headerLabel.setText("Control in action: Image");

      controlPanel.add(new ImageComponent("logo.jpg"));
      mainFrame.setVisible(true);
   }

   class ImageComponent extends Component {

      BufferedImage img;

      public void paint(Graphics g) {
         g.drawImage(img, 0, 0, null);
      }

      public ImageComponent(String path) {
         try {
            img = ImageIO.read(new File(path));
         } catch (IOException e) {
            e.printStackTrace();
         }
      }

      public Dimension getPreferredSize() {
         if (img == null) {
            return new Dimension(100,100);
         } else {
            return new Dimension(img.getWidth(), img.getHeight());
         }
      }
   }
}

Wednesday, December 25, 2013

// LineNumberReader/PrintWriter

// LineNumberReader/PrintWriter
import java.io.*;
class Line
{
  public static void main(String[] args)
  {
    String x;
    try
    {
      LineNumberReader r = new LineNumberReader(new FileReader("ionote.txt"));
      PrintWriter w = new PrintWriter(new FileWriter("aman.txt"));
      while((x=r.readLine())!=null) // while not eof
      {
        w.println(r.getLineNumber()+". "+x);
        System.out.println(x);
        Thread.sleep(10);
      }
      w.close();
    }catch(Exception ex){}  
  }
}

file reader/writer

// FileReader/Writer
import java.io.*;
class RW
{
  public static void main(String[] args)
  {
    int x;
    try
    {
      FileReader r = new FileReader("RW.java");
      FileWriter w = new FileWriter("aman.txt");
      while((x=r.read())!=-1) // while not eof
      {
        w.write(x);
        System.out.print((char)x);
        Thread.sleep(10);
      }
      w.close();
    }catch(Exception ex){}  
  }
}

sample notepad

import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Notepad extends Frame implements ActionListener
{
  TextArea ta = new TextArea();
  MenuBar mb = new MenuBar();
  Menu f1 = new Menu("File");
  Menu f2 = new Menu("Edit");
  Menu f3 = new Menu("Help");
  MenuItem a1 = new MenuItem("New");
  MenuItem a2 = new MenuItem("Open");
  MenuItem a3 = new MenuItem("Save");
  MenuItem a4 = new MenuItem("Save As");
  MenuItem a5 = new MenuItem("Exit");
  MenuItem a6 = new MenuItem("Cut");
  MenuItem a7 = new MenuItem("Copy");
  MenuItem a8 = new MenuItem("Paste");
  MenuItem a9 = new MenuItem("Select All");
  MenuItem a10 = new MenuItem("Help Topic.");
  String x,data,fname;
  Notepad()
  {
    setTitle("Untitled - Notepad");
    setFont(new Font("Arial",0,14));
   // add item in file menu
    f1.add(a1);
    f1.add(a2);
    f1.addSeparator();
    f1.add(a3);
    f1.add(a4);
    f1.addSeparator();
    f1.add(a5);
   // add item in edit menu
    f2.add(a6);
    f2.add(a7);
    f2.add(a8);
    f2.addSeparator();
    f2.add(a9);
    f3.add(a10); // add item in Help Topic
   // add menu in menubar
    mb.add(f1);
    mb.add(f2);
    mb.add(f3);
    setMenuBar(mb); // set menu bar in Frame.
    add(ta); // add textarea in center, bcos default layout in frame in BorderLayout
    setBounds(5,5,790,560);
    setVisible(true);
    setResizable(false);
    a1.addActionListener(this);
    a2.addActionListener(this);
    a3.addActionListener(this);
    a4.addActionListener(this);
    a5.addActionListener(this);
    a6.addActionListener(this);
    a7.addActionListener(this);
    a8.addActionListener(this);
    a9.addActionListener(this);
    a10.addActionListener(this);
    addWindowListener(new Win());
  }
  class Win extends WindowAdapter
  {
    public void windowClosing(WindowEvent e)
    {
       dispose();
    }
  }
  public void actionPerformed(ActionEvent e)
  {
    Object o = e.getSource(); // o hold the event source object.
    if(o==a1) // for new
    {
      setTitle("Untitled - Notepad");
      ta.setText("");
    }
    if(o==a2) // for open
    {
      FileDialog fd = new FileDialog(this,"Open File",FileDialog.LOAD);
      fd.setVisible(true);
      fname = fd.getDirectory()+fd.getFile();
      setTitle(fname); // display selected file name on title
      try
      {
        BufferedReader r = new BufferedReader(new FileReader(fname));
        data="";
         while((x=r.readLine())!=null) // while not eof
          data+=x+"\r\n";
        ta.setText(data);
      }catch(Exception ex)
{

}  
    }
    if(o==a3) // for Save
    {
      if(getTitle().equals("Untitled - Notepad"))
      {
        FileDialog fd = new FileDialog(this,"Save File",FileDialog.SAVE);
        fd.setVisible(true);
        fname = fd.getDirectory()+fd.getFile();
        setTitle(fname); // display selected file name on title
      }
      try
      {
        PrintWriter w = new PrintWriter(new FileWriter(fname));
        w.print(ta.getText());
        w.close();
      }catch(Exception ex){}  
    }
    if(o==a4) // for Save As
    {
      FileDialog fd = new FileDialog(this,"Save As File",FileDialog.SAVE);
      fd.setVisible(true);
      fname = fd.getDirectory()+fd.getFile();
      setTitle(fname); // display selected file name on title
      try
      {
        PrintWriter w = new PrintWriter(new FileWriter(fname));
        w.print(ta.getText());
        w.close();
      }catch(Exception ex){}  
    }
    if(o==a5) // for exit
      dispose();
  }
  public static void main(String aa[])
  {
     new Notepad();
  }
}
// javac Notepad.java
// java Notepad

Thursday, November 28, 2013

Awt Example Text Area

//MyAwt.java

import java.awt.*;
import java.awt.event.*;


public class MyAwt extends Frame implements ActionListener{
 
    Button btnSave = new Button();
    Button btnClear = new Button();
    TextField txt1 = new TextField();
    TextField txt2 = new TextField();

    TextArea txtar= new TextArea();

    Label lbl1 = new Label();
    Label lbl2 = new Label();
 
 
    public MyAwt()
    {
        this.setLayout(null);
        this.add(lbl1,null);
        lbl1.setText("Name");
        lbl1.setBounds(20,70 , 50, 20);

        this.add(lbl2,null);
        lbl2.setText("Number");
        lbl2.setBounds(20,110 , 50, 20);

        this.add(txt1,null);
        txt1.setBounds(80,70 , 200, 20);

        this.add(txt2,null);
        txt2.setBounds(80,110 , 200, 20);

        this.add(btnSave,null);
        btnSave.setLabel("SUBMIT");
        btnSave.setBounds(70,150 , 60, 30);
        btnSave.addActionListener(this);

        this.add(btnClear,null);
        btnClear.setLabel("CLEAR");
        btnClear.setBounds(140,150 , 60, 30);
        btnClear.addActionListener(this);

        this.add(txtar,null);
        txtar.setBounds(20,190 , 360, 100);
        txtar.setEnabled(false);
    }
 
 
    public void actionPerformed(ActionEvent e)
    {
        Object o = e.getSource();
        if(o.equals(btnSave))
        {
            txtar.setEnabled(true);
            txtar.setText("Name : "+txt1.getText()+"\nNumber : "+txt2.getText());
            txtar.setEditable(false);
        }
        if(o.equals(btnClear))
        {
            txt1.setText("");
            txt2.setText("");
            txtar.setText("");
            txtar.setEnabled(false);
        }
    }
 

    //main
    public static void main(String[] args) {
        MyAwt t = new MyAwt();
        t.setSize(400,320);
        TWindow tw = new TWindow();
        t.addWindowListener(tw);
        t.setTitle("My AWT");
        t.setVisible(true);
    }
}

//TWindow.java

import java.awt.*;
import java.awt.event.*;

public class TWindow extends WindowAdapter{
      public void windowClosing(WindowEvent e) 
    { 
        System.exit(0); 
    } 
}

Wednesday, November 27, 2013

awt lower to upper

//Import Statements
import java.awt.*;
import java.awt.event.*;
//Class method
class ChangeCase extends Frame implements ActionListener
{
    //Variable declaration
    Checkbox ch1,ch2,ch3,ch4,ch5;
    CheckboxGroup chg;
    Button bu;
    TextArea tax;
    public ChangeCase(String title)//Main method
    {
    super(title);
    chg=new CheckboxGroup();//Object creation
    ch1=new Checkbox("SentenceCase",chg,false);
    ch2=new Checkbox("lowercase",chg,false);
    ch3=new Checkbox("UPPERCASE",chg,false);
    ch4=new Checkbox("TitleCase",chg,false);
    ch5=new Checkbox("tOGGLEcASE",chg,false);
    bu=new Button("Apply");
    tax=new TextArea();
    setLayout(new GridLayout(7,1));
    add(ch1);
    add(ch2);
    add(ch3);
    add(ch4);
    add(ch5);
    add(bu);
    add(tax);
    bu.addActionListener(this);
//  addWindowListener(new Mywindow());
    setSize(400,400);
    setVisible(true);
    }
    // © http://students3k.com – Karhtikh venkat
    public void actionPerformed(ActionEvent e)
    {
 
        if(e.getSource()==bu)//Decision making statement
        {
                int i;
                StringBuffer str=new StringBuffer(ta.getText());
                String s;
                if(ch1.getState()==true)
                {
                str.insert(0,Character.toUpperCase(str.charAt(0)));
                str.deleteCharAt(1);
                for(i=0;i<str.length();i++)//For statement
                    {
                    if(str.charAt(i)=='.')
                    {
                    str.insert(i+2,Character.toUpperCase(str.charAt(i+2)));
                    str.deleteCharAt(i+3);
                    }
                }
                tax.setText(new String(str));
                }
                else if(ch2.getState()==true)
                {
                s=tax.getText();
                s=s.toLowerCase();
                tax.setText(s);
                }
                else if(ch3.getState()==true)
                {
                s=tax.getText();
                s=s.toUpperCase();
                tax.setText(s);
                }
                else if(ch4.getState()==true)
                {
                s=tax.getText();
                s=s.toLowerCase();
                str=new StringBuffer(s);
        // © http://students3k.com – Karhtikh venkat
    str.insert(0,Character.toUpperCase(str.charAt(0)));
                str.deleteCharAt(1);
                for(i=0;i<str.length();i++)
                {
                if(Character.isSpace(str.charAt(i))==true)
                    {
                    str.insert(i+1,Character.toUpperCase(str.charAt(i+1)));
                    str.deleteCharAt(i+2);
                    }
                }
                tax.setText(new String(str));
                }
                else
                {
                s=tax.getText();
                s=s.toUpperCase();
                str=new StringBuffer(s);
                str.insert(0,Character.toLowerCase(str.charAt(0)));
                str.deleteCharAt(1);
                for(i=0;i<str.length();i++)
                {
                if(Character.isSpace(str.charAt(i))==true)
                    {
                    str.insert(i+1,Character.toLowerCase(str.charAt(i+1)));
                    str.deleteCharAt(i+2);
                    }
                }
                tax.setText(new String(str));
                }
 
        }
        // © http://students3k.com – Karhtikh venkat
    }
    public static void main(String a[])
    {
    ChangeCase n=new ChangeCase("ChangeCase");
    }
}