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