0% found this document useful (0 votes)
9 views9 pages

J2ee CH - 3 Program - 26 To 39

The document contains various JSP program examples demonstrating basic operations such as addition, conditional statements, expression tags, and database connectivity with both MS-Access and MySQL. It includes code snippets for loops, includes, forwards, and scriptlets. Each section illustrates different JSP functionalities and their implementations in web applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

J2ee CH - 3 Program - 26 To 39

The document contains various JSP program examples demonstrating basic operations such as addition, conditional statements, expression tags, and database connectivity with both MS-Access and MySQL. It includes code snippets for loops, includes, forwards, and scriptlets. Each section illustrates different JSP functionalities and their implementations in web applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

JSP Programs

(25)Addition

<html>

<body>

<h1>Addition Of Two Value

<hr>

<%!int a,b,ans;%>

<%

a=10;
b=20;
ans=a+b;

%>

<%="Sum is"+ans%>

</h1>

</body>

</html>

-----------------------------------------

(26) if_else

<html>

<body>

<h1>Positive Or Nagative

<hr>

<%!int a=8;%>

<%
if(a>0)
{
%>

<%="a is Positive"%>

<%
}
else
{
%>

<%="a is Nagative"%>

<%
}
%>

</h1>

</body>
</html>

---------------------------------------------
(27) Expression tag - welcome message

<html>

<body bgcolor="pink">

<h1>Welcome To Jsp Page!

<hr>

<%="Welcome To Hns College"%>

<hr>

<%="Good Morning Student"%>

</h1>

</body>

</html>

-----------------------------------------------------

(28) include tag


<h1>
<%="Good morning"%>
<jsp:include page="ty.html"/>
<%="thank u"%>
</h1>

(28) forward tage


<h1>
<%="Good morning"%>
<jsp:forward page="ty.html"/>
<%="thank u"%>
</h1>

(28) ty.html file code


<html>
<body>
<hr color="red">
<h1>Welcome to hns</h1>
<hr color="red">
</body>
</html>

------------------------------------------
(29) employee.jsp
<h1>
<%="emp name"+request.getParameter("enm")%>
<%="salary"+request.getParameter("sal")%>
</h1>

(29) jspforward.jsp
<%="hello"%>
<jsp:forward page="employee.jsp">
<jsp:param name="enm" value="om"/>
<jsp:param name="sal" value="5000"/>
</jsp:forward>

(29) jspinclude.jsp
<%="hello"%>
<jsp:include page="employee.jsp">
<jsp:param name="enm" value="om"/>
<jsp:param name="sal" value="5000"/>
</jsp:include>
--------------------------------------------------
(30) if program
<%! int i=1; %>
<%

if(i>0)
{
out.println("True");

}
%>
-------------------------------------------------
(31) datedemo

<h1> First Scriptlet of JSP </h1>

<%@include file = "second.jsp" %>


<%@ page import = "java.util.Date" %>

<%
for(int i=1; i<=5; i++)
{
%>

<%= " Current Date is = " + new Date() %>

<%
out.println("<b>GOOD ByE... " + i + "<b><BR>");

}
%>

(31) second.jsp

<h1> First Scriptlet of JSP </h1>


<%
for(int i=1; i<=5; i++)
{
%>
<U> HELLO FRIENDS </U><BR>

<%@ include file = "LineDraw.jsp" %>

<I> GOOD TO SEE YOU </I> <BR>


<%
}
%>

(31) LineDraw.jsp
<HR height = 300 width = 200>

----------------------------------------------

(32) Expression.jsp
<%! int a = 1; %>
<%= a %>
<% a++; %>
<br>
------

<H1> Expression Test </H1>

<%! int i = 0; %>


<%

i++;

%>

<h2> .... HELLO WORLD... <h2>


<%= "This JSP page accessed :: " + i + " Times" %>

-----------------------------------------------------------
(33) for loop
<%! int i; %>
<%
for(i=1;i<=5;i++)
{
%>
<%@ include file="simple.jsp" %>
<%
}
%>

(33) simple.jsp

<h1>Simple JSP Pogram</h1>


<%
out.println("<hr> Hello JSP <br>");
%>

---------------------------------------------------------
(34) while loop
<%! int i=1; %>
<%

while(i<=5)
{
out.println(i+"<br>");
i++;

}
%>
----------------------------------------------------------

(35) pyramid.jsp
<%! int i,j,k; %>
<%

for(i=1;i<5;i++)
{
for(j=1;j<=i;j++)
{
out.println("*");
}
out.println("<br>");
}
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
out.println("*");
}
out.println("<br>");
}
%>
------------------------------------------------
(36) connectivity program JSP with MS-ACCESS

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

<html>

<body>

<h1> Hello Students


<%!

Connection cn;

ResultSet rs;

Statement st;

String q;

%>

<%

try

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

cn=DriverManager.getConnection("jdbc:odbc:mydsn");

st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
out.println("connection done");

q="select * from student";

rs=st.executeQuery(q);

out.println("<table border='1' bordercolor='red'>");

while(rs.next())

out.println("<tr>");

out.println("<td>"+rs.getInt(1));

out.println("<td>"+rs.getString(2));

out.println("<td>"+rs.getString(3));

}
st.close();

cn.close();

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

%>

</h1>

</body>

</html>

---------------------------------------------------------------
(37) Connectivity JSP with MYSQL

<h1>Hello Students

<%!

Connection cn;

ResultSet rs;

Statement st;

String q;

%>

<%

try

Class.forName("com.mysql.jdbc.Driver");

cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hnsmysql","root","");
st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

out.println("connection done");

q="select * from student";

rs=st.executeQuery(q);

out.println("<table border='1' bordercolor='red'>");

while(rs.next())

{
out.println("<tr>");

out.println("<td>"+rs.getInt(1));

out.println("<td>"+rs.getString(2));

st.close();

cn.close();

catch(Exception e) {}

%>

</h1>

</body>

</html>

------------------------------------------

(38) just understanding of all database and its driver (no need to write)

import java.sql.*;

class select_mysql
{
public static void main(String args[])
{
try
{
Connection cn;
Statement st;
ResultSet rs;

//for odbc access database:- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


//for odbc access database:-cn=DriverManager.getConnection("jdbc:odbc:mydsn");

//for sql server database:-


Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//for sql server:-
cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=hnssql
;integratedSecurity=true;");

//for oracle database:- Class.forName("oracle.jdbc.driver.OracleDriver");


//for oracle database:- String url =
"jdbc:oracle:this:user/password@(description=(address_list)=(address=(protocol=tcp)
(host=localhost)(post=1521)))(source_route=yes)(connection_data)=(sid=Earnest)";
//Connection con = DriverManager.getConnection(url,"scott","tiger");

Class.forName("com.mysql.jdbc.Driver");
cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hns","root","");

System.out.println("Connection done");

st=cn.createStatement();
rs = st.executeQuery("select * from student");

while(rs.next())

{
int rno = rs.getInt(1);
String name = rs.getString(2);
String city=rs.getString(3);

System.out.println("rno: " + rno);


System.out.println("name: " + name);
System.out.println("city name is : " + city);
}
cn.close();
rs.close();

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

--------------------------------------------------------

You might also like