Database Connection
There are 5 steps to connect any java application with
the database in java using JDBC.
They are as follows:
1. Register the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
2. Creating connection
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_
name“ , "root", "admin");
3. Creating statement
Statement stmt=con.createStatement();
4. Executing queries
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
5. Closing connection
con.close();
Simple Program for JDBC Connection
import java.sql.*;
class mysqlCon
{
public static void main(String args[])
{
try
{ Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/db_name
“ , "root", "admin");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()) {
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
con.close();
}
catch(Exception e)
{ System.out.println(e);}
}
}
import java.sql.*;
class OracleCon
{
public static void main(String args[])
{
try
{ Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe",
"system","oracle");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()) {
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
con.close();
}
catch(Exception e)
{ System.out.println(e);}
}
}
Apache Tomcat Server
JSPs, like servlets, are server-side programs run inside a HTTP server. To support JSP/servlet, a Java-
capable HTTP server is required. Tomcat Server (@ http://tomcat.apache.org) is the official
reference implementation (RI) for Java servlet and JSP, provided free by Apache (@
http://www.apache.org) - an open-source software foundation.
<html>
<head><title>First JSP</title></head>
<body>
<%
doublenum = Math.random();
if (num> 0.95) {
%>
<h2>You'll have a luck day!</h2><p>(<%= num %>)</p>
<%
} else {
%>
<h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
<%
%>
<a href="<%= request.getRequestURI() %>"><h3>Try Again</h3></a>
</body>
</html>
Cascading Style Sheets (CSS)
<!DOCTYPE html>
<html>
<head>
<title>Internal CSS</title>
<style>
.main {
text-align:center;
}
.GFG {
color:#009900;
font-size:50px;
font-weight:bold;
}
.geeks {
font-style:bold;
font-size:20px;
}
</style>
</head>
<body>