Wednesday, 24 January 2018

Advancedjava-Slip24



Slip24

Q1. Write a JSP script to check whether given mail ID is valid or not. (Mail ID should contain one @ symbol and atleast one Dot(.) symbol) 

 

<%@ page language="java" %>

<html>
<head><title>Email Validation</title>
<script language = "Javascript">

function emailcheck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert("Invalid E-mail ID")
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert("Invalid E-mail ID")
return false
}

if (str.indexOf(" ")!=-1){
alert("Invalid E-mail ID")
return false
}
alert("valid E-mail ID")
return true
}

function ValidateEmail(){
var emailID=document.frm.txtEmail

if ((emailID.value==null)||(emailID.value=="")){
alert("Please Enter your Email Address")
emailID.focus()
return false
}
if (emailcheck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}
return true
}
</script>
</head>
<body>
<form name="frm" method="post" action="#" onSubmit="return ValidateEmail()">
<p>Enter an Email Address :
<input type="text" name="txtEmail">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>

Another way

email.jsp
<%
String email = request.getParameter("email");
int i = email.indexOf('@');
int j = email.lastIndexOf('@');
int k = email.indexOf('.');
if(i!=-1 && j!=-1 && i==j && k!=-1 && k>i)
out.print(email+" is valid email.");
else
out.print(email+" is invalid email.");
%>


email.html
<form method='post' action='email.jsp'>
Email:<input type='text' name='email'><br><br>
<input type='submit'><input type='reset'>
</form>


 
                                                                       
Q2. Write a java program to accept the details of college (CID, CName, Address, year) and store it into database (Use Swing and PreparedStatement interface)      


import java.sql.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class College extends JFrame{
            private JLabel lblID,lblName,lblAddr,lblYear;
            private JTextField txtID,txtName,txtAddr,txtYear;
            private JButton btnSave,btnClose;
            private JPanel panCenter,panSouth;

            private Connection con;
            private PreparedStatement ps;

            public College(){
                        lblID = new JLabel("College ID:");
                        lblName = new JLabel("College Name:");
                        lblAddr = new JLabel("Address:");
                        lblYear = new JLabel("Year:");

                        txtID = new JTextField();
                        txtName = new JTextField();
                        txtAddr = new JTextField();
                        txtYear = new JTextField();

                        panCenter = new JPanel();
                        panCenter.setLayout(new GridLayout(4,2));
                        panCenter.add(lblID);
                        panCenter.add(txtID);
                        panCenter.add(lblName);
                        panCenter.add(txtName);
                        panCenter.add(lblAddr);
                        panCenter.add(txtAddr);
                        panCenter.add(lblYear);
                        panCenter.add(txtYear);

                        btnSave = new JButton("Save");
                        btnClose = new JButton("Close");

                        panSouth = new JPanel();
                        panSouth.add(btnSave);
                        panSouth.add(btnClose);

                        setTitle("College Information");
                        setSize(400,200);
                        setLocation(100,100);
                        add(panCenter,"Center");
                        add(panSouth,"South");
                        setVisible(true);
                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                        try{
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:lab.dsn");
                        }
                        catch(Exception e){
                                    JOptionPane.showMessageDialog(null,e);
                                    dispose();
                        }

                        btnSave.addActionListener(new ActionListener(){
                                    public void actionPerformed(ActionEvent ae){
                                                try{
                                                            int cid = Integer.parseInt(txtID.getText());
                                                            String cname = txtName.getText();
                                                            String addr = txtAddr.getText();
                                                            int yr = Integer.parseInt(txtYear.getText());

                                                            ps = con.prepareStatement("insert into college values(?,?,?,?)");
                       
                                                            ps.setInt(1,cid);
                                                            ps.setString(2,cname);
                                                            ps.setString(3,addr);
                                                            ps.setInt(4,yr);

                                                            ps.executeUpdate();

                                                            txtID.setText("");
                                                            txtName.setText("");
                                                            txtAddr.setText("");
                                                            txtYear.setText("");

                                                            txtID.requestFocus();
                                                }
                                                catch(Exception e){   
                                                            JOptionPane.showMessageDialog(null,e);
                                                }
                                    }
                        });                                           

                        btnClose.addActionListener(new ActionListener(){
                                    public void actionPerformed(ActionEvent ae){
                                                dispose();
                                    }
                        });
            }

            public static void main(String args[]){
                        new College();
            }
}