Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
47 views4 pages

Codes Used

The document discusses the 5 steps to connect a Java application to a database using JDBC, which are registering the driver class, creating a connection, creating a statement, executing queries, and closing the connection. It also provides two simple example programs showing how to connect to MySQL and Oracle databases using JDBC. The document describes how Tomcat is the official reference implementation for servlet and JSP technologies and provides an example of a basic JSP page.

Uploaded by

Vaseem Akram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views4 pages

Codes Used

The document discusses the 5 steps to connect a Java application to a database using JDBC, which are registering the driver class, creating a connection, creating a statement, executing queries, and closing the connection. It also provides two simple example programs showing how to connect to MySQL and Oracle databases using JDBC. The document describes how Tomcat is the official reference implementation for servlet and JSP technologies and provides an example of a basic JSP page.

Uploaded by

Vaseem Akram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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>

You might also like