Wednesday, 24 January 2018

Advancedjava-Slip28

Slip28

 Q1. Write a JSP program to check whether given number is Armstrong or not. (Use Include directive).

 armstrong.html


<html>
<body>
<form method=post action="armstrong.jsp">
Enter Number : <input type="text" name=t1><br>
<input type=submit>
</form>
</body>
</html>

 

 armstrong.jsp

<%@include file="armstrong.html"%>
<html>
<body>
<% int n,m,sum=0,rem; %>
<% n=Integer.parseInt(request.getParameter("t1"));
m=n;
while(n>0)
{
   rem=n%10;
   sum=sum+(rem*rem*rem);
   n=n/10;
}
if(sum==m)
out.print("the no. is armstrong "+sum);
else
out.print("the no. is not armstrong "+sum);
%>
</body>
</html>

 

Q2. Write a java program to display the selected employee details in JTable. (use database, combo box for employee selection )  Employee having fields eno, ename, sal, desg.


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

import java.util.*;

import java.sql.*;


class EmpView extends JFrame{

            private JTable tabRecord;

            private JLabel lblEmp;

            private JComboBox cmbEmp;

            private JPanel panNorth;

            private DefaultTableModel dtm;

            private Connection con;

            private PreparedStatement ps;

            private Statement s;

            private ResultSet rs;


            public EmpView(){

                        lblEmp = new JLabel("Employee:");

                        cmbEmp = new JComboBox();

                        panNorth = new JPanel();

                        panNorth.add(lblEmp);

                        panNorth.add(cmbEmp);

                        dtm = new DefaultTableModel();

                        dtm.addColumn("EmpNo");

                        dtm.addColumn("Name");

                        dtm.addColumn("Salary");

                        //dtm.addColumn("Designation");

                        tabRecord = new JTable(dtm);

                        setTitle("View Employee");

                        setSize(500,150);

                        add(panNorth,"North");

                        add(new JScrollPane(tabRecord),"Center");

                        setVisible(true);

                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                                  try{

                                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

             con=DriverManager.getConnection("jdbc:odbc:lab.dsn");

                                    s = con.createStatement();


                                    rs = s.executeQuery("select * from emp");

                                    cmbEmp.addItem("---");

                                    while(rs.next()){

                                                cmbEmp.addItem(rs.getString(2));

                                    }

                        }

                        catch(Exception e){

                                    JOptionPane.showMessageDialog(null,e);

                        }

          

                        cmbEmp.addItemListener(new ItemListener(){

                                    public void itemStateChanged(ItemEvent ie){

                                                try{

                                      String name = cmbEmp.getSelectedItem().toString();

                                         ps = con.prepareStatement("select * from emp where name=?");

                                                            ps.setString(1,);

                                                            rs = ps.executeQuery();

                                                            if(rs.next()){

                                                                        Vector rec = new Vector();

                                                                        rec.add(rs.getString(1));

                                                                        rec.add(rs.getString(2));

                                                                        rec.add(rs.getString(3));

                                                                       // rec.add(rs.getString(4));


                                                                        if(dtm.getRowCount()>0)

                                                                                    dtm.removeRow(0);

                                                                        dtm.addRow(rec);

                                                            }

                                                }

                                                catch(Exception e){

                                                            JOptionPane.showMessageDialog(null,e);

                                                }

                                    }

                        });

            }


            public static void main(String args[]){

                        new EmpView();

            }

}