Wednesday, 24 January 2018

Advancedjava-Slip23



Slip23

Q1. Write a JSP script to accept the details of Student (RNo, SName, Gender, Computer_ Knowledge , Class)  and display it on the browser. Use appropriate controls for accepting data.



student.html

<form method='post' action='student.jsp'>
<table>
<tr><td><b>Roll No:</b></td><td><input type='text' name='rno'></td></tr>
<tr><td><b>Name:</b></td><td><input type='text' name='name'></td></tr>
<tr>
            <td><b>Gender:</b></td>
            <td>
            <select name='gender'>
            <option value=''>---</option>
            <option value='Male'>Male</option>
            <option value='Female'>Female</option>
            </select>
            </td>
</tr>
<tr>
            <td><b>Computer Knowledge:</b></td>
            <td>
            <select name='ck'>
            <option value=''>---</option>
            <option value='Beginner'>Beginner</option>
            <option value='Intermediate'>Intermediate</option>
            <option value='Expert'>Expert</option>
            </select>
            </td>
</tr>
<tr>
            <td><b>Class:</b></td>
            <td>
            <select name='class'>
            <option value=''>---</option>
            <option value='FY'>FY</option>
            <option value='SY'>SY</option>
            <option value='TY'>TY</option>
            </select>
            </td>
</tr>
<tr>
            <td><input type='submit'></td>
            <td><input type='reset'></td>
</tr>
</table>
</form>

student.jsp
<table border=1>
<tr><td><b>Roll No:</b></td><td><%=request.getParameter("rno")%></td></tr>
<tr><td><b>Name:</b></td><td><%=request.getParameter("name")%></td></tr>
<tr><td><b>Gender:</b></td><td><%=request.getParameter("gender")%></td></tr>
<tr><td><b>Computer Knowledge:</b></td><td><%=request.getParameter("ck")%></td></tr>
<tr><td><b>Class:</b></td><td><%=request.getParameter("class")%></td></tr>
</table>

 

Q2. Write a Java Program to Read, Update and Delete any record from “Elements” table. The table has following fields (Atomic_weight , Name (primary key), Chemical_Symbol). The input should be provided through Command Line Arguments along with the appropriate data.
The operations are: R : Read, U: Update, D: Delete.
The syntax for Read: R
The syntax for Delete: D name                                                                                           
Assume Elements table is already created.



import java.sql.*;
import java.io.*;
import javax.sql.*;

class Elements{
            public static void main(String args[]){
                        try{
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:lab.dsn");

                                    Statement s=null;
                                    PreparedStatement ps=null;
                                    ResultSet rs=null;

                                    char ch = args[0].charAt(0);

                                    String name="",symbol="";
                                    int weight=0;
                                   
                                    switch(ch)
                                    {
                                    case 'R':
                                                s = con.createStatement();
                                                rs = s.executeQuery("select * from elements");
                                                while(rs.next()){
                                                            System.out.println(rs.getInt(1)+"\t"+
                                                                        rs.getString(2)+"\t"+
                                                                        rs.getString(3));
                                                }
                                                break;
                                    case 'D':
                                                name = args[1];
                                                ps = con.prepareStatement("delete from elements where name=?");
                                                ps.setString(1,name);
                                                if(ps.executeUpdate()==1)
                                                            System.out.println("Element "+name+" deleted successfully.");
                                                else
                                                            System.out.println("Element "+name+" not found.");                                               
                                                break;
                                    case 'U':
                                                weight = Integer.parseInt(args[1]);
                                                name = args[2];
                                                symbol = args[3];

                                                ps = con.prepareStatement("update elements set atomic_weight=?,chemical_symbol=? where name=?");

                                                ps.setInt(1,weight);
                                                ps.setString(2,symbol);
                                                ps.setString(3,name);

                                                if(ps.executeUpdate()==1)
                                                            System.out.println("Element "+name+" updated successfully.");
                                                else
                                                            System.out.println("Element "+name+" not found.");                                               
                                    }

                                    con.close();                                         
                        }
                        catch(Exception e){
                                    System.out.println(e);
                        }
            }
}