Wednesday, 24 January 2018

Advancedjava-Slip12



Slip12
Q1. Write a Multithreading program in java to display the number’s between 1 to 100 continuously in a TextField by clicking on button.      (use Runnable Interface).
import java.awt.*;
import java.awt.event.*;

public class thread7 extends Frame implements ActionListener,Runnable
{
    Button b1,b2;
   TextField t11,t12;
    int cnt;
   Thread t1 = new Thread(this,"t1");
    Thread t2=new Thread(this,"t2");
  
            public thread7()
   {
         setLayout(null);
         t11=new TextField();
         t12=new TextField();
         b1=new Button("Start");
         b2=new Button("Stop");
          t11.setBounds(50,50,100,100);
          t12.setBounds(160,50,100,100);
         b1.setBounds(50,170,100,30);
          b2.setBounds(160,170,100,30);
       
       add(t11);
        add(t12);
        b1.addActionListener(this);
         b2.addActionListener(this);
        add(b1);
        add(b2);
      
        setSize(400,400);
        setVisible(true);
         cnt=0;

                    addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        System.exit(0);
                    }
                });


    }
    public void actionPerformed(ActionEvent ae)
    {
           String str;
           str=ae.getActionCommand();
           if(str.equals("Start"))
          {
                   
                    t1.start();
      t2.start();
          }
         else if(str.equals("Stop"))
      {
            t1.stop();
            t2.stop();
      }
    }
     public void run()
    {
                 try
                 {
                  for(int i=1;i<=100;i++)
                   {
                  t11.setText(" "+i);
                  t1.sleep(150);
                  t12.setText(""+i);
                  t2.sleep(150);
                  }
                }
                catch(Exception e)
                {}
      }
    public static void main(String args[])
        {
            new thread7().show();     
        }
}


                       
Q2. Write a JSP program to accept the details of Account (ANo, Type, Bal) and store it into database and display it in tabular form. (Use PreparedStatement interface) 



account.html


<form method='post' action='add_account.jsp'>
<table>
<tr>
            <td><b>Account No:</b></td>
            <td><input type='text' name='ano'></td>
</tr>
<tr>
            <td><b>Account Type:</b></td>
            <td>
            <select name='type'>
            <option value=''>Select Type</option>
            <option value='Saving'>Saving</option>
            <option value='Current'>Current</option>
            </select>
            </td>
</tr>
<tr><td><b>Balance:</b></td><td><input type='text' name='bal'></td></tr>
<tr><td><input type='submit'></td><td><input type='reset'></td></tr>
</table>
</form>
<a href='view_accounts.jsp'>View Accounts</a>
                                   

add_account.jsp                               
<%@page import="java.sql.*" errorPage="err.jsp"%>

<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:lab.dsn");

int ano = Integer.parseInt(request.getParameter("ano"));
String type = request.getParameter("type");
float bal = Float.parseFloat(request.getParameter("bal"));
PreparedStatement ps = con.prepareStatement("insert into account values(?,?,?)");
ps.setInt(1,ano);
ps.setString(2,type);
ps.setFloat(3,bal);
ps.executeUpdate();
%>
Account created successfully.<br>
<a href='account.html'>Add Account</a> <a href='view_accounts.jsp'>View Accounts</a>

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

<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:lab.dsn");
Statement s = con.createStatement();
ResultSet rs = s.executeQuery("select * from account");
%>
<table border=1>
<tr><th>Account No</th><th>Type</th><th>Balance</th></tr>
<%
while(rs.next()){
%>
<tr><td><%=rs.getInt(1)%></td><td><%=rs.getString(2)%></td><td><%=rs.getFloat(3)%></td>
</tr>
<%
            }
%>
</table><br>
<a href='account.html'>Home</a>


err.jsp
<%@page isErrorPage="true"%>
Error:<%=exception%><br>
<a href='account.html'>Home</a>