Click the above link for example
Advance Java
Arithmetic Operation using AWT Frame with Event Delegation
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
// author : www.raviroza.com
// date : 27-Jan-2023, 8.25 am
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ArithmaticOperation
extends Frame
implements ActionListener
{
Label l1,l2,lblResult;
TextField txtNo1, txtNo2;
Button btnAdd, btnSub, btnMul, btnDiv;
public ArithmaticOperation() {
// TODO Auto-generated constructor stub
l1 = new Label("enter number 1 : ");
l2 = new Label("enter number 2 : ");
lblResult = new Label("0");
txtNo1 = new TextField(3);
txtNo2 = new TextField(3);
btnAdd = new Button("+");
btnSub = new Button("-");
btnMul = new Button("*");
btnDiv = new Button("/");
setLayout(new FlowLayout());
add(l1); add(txtNo1);
add(l2); add(txtNo2);
add(lblResult);
add(btnAdd); add(btnSub);
add(btnMul); add(btnDiv);
btnAdd.addActionListener(this);
btnSub.addActionListener(this);
btnMul.addActionListener(this);
btnDiv.addActionListener(this);
setSize(500, 500);
//validate();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int a = Integer.parseInt(txtNo1.getText());
int b = Integer.parseInt(txtNo2.getText());
int c = 0;
if(e.getSource() == btnAdd)
{
c = a + b;
}
else if(e.getSource() == btnSub)
{
c = a - b;
}
else if(e.getSource() == btnMul)
{
c = a * b;
}
else if(e.getSource() == btnDiv)
{
c = a / b;
}
lblResult.setText ( Integer.toString(c));
}
public static void main(String[] args)
{
new ArithmaticOperation();
}
}
Dynamic Login 👤 Example using Servlet in Java
DynamicLogin.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Static Login</title>
<!-- www.raviroza.com -->
</head>
<body>
<h1>Static Login Information</h1>
<hr>
<form action="dlogin" method="post">
<p>
Username <input type="text" name="txtuser">
</p>
<p>
Password <input type="password" name="txtpass">
</p>
<p>
<input type="submit" value="Login" />
</p>
</form>
</body>
</html>
DynamicLoginServlet.java
package RROExamples;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/dlogin")
public class DynamicLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//create table tbluser (username varchar2(30), password varchar2(30));
//response.getWriter().append("Served at: ").append(request.getContextPath());
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String user = request.getParameter("txtuser");
String pass = request.getParameter("txtpass");
String QRY = "select * from tbluser where ";
QRY += " username = '"+ user +"' and ";
QRY += " password = '"+ pass +"' ";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","hjd");
if(conn.createStatement().executeQuery(QRY).next())
{
pw.print("Welcome, Master : "+user);
response.sendRedirect("home.html");
}
else
{
pw.print("Sorry, My Lord ");
pw.println("<br><a href='signup.html'> Sign Up Here </a> ");
}
}
catch(Exception e)
{
pw.print("Error : "+e.toString());
}
}
}
JDBC Callable Statement Theory
- CallableStatement interface is used to call the stored procedures or functions.
- We can have business logic on the database by the use of stored procedures and functions that will make the performance better because they are precompiled.
- For example we need to retrieve the age of the student based on birth date, one may create a function that receives date as INPUT and returns age of the student as the OUTPUT.
JDBC Prepared Statement Example
- The main feature of a PreparedStatement object is that, unlike a Statement object, it is given a SQL statement when it is created.
- The advantage to this is that in most cases, this SQL statement is sent to the DBMS right away, where it is compiled.
- As a result, the PreparedStatement object contains not just a SQL statement, but a SQL statement that has been precompiled.
- This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement SQL statement without having to compile it first.
JDBC with MS Access Database in Java 8
- Since JDK 8, Java does not support ODBC driver for JDBC connectivity.
- MS Access does not provide driver for JDK 8, therefore third party driver which is “UCanAccess” can be used to connect with MS Access database.
- Download UCanAccess Driver.
- Following is the way to connect with MSAccess database.
- Please create the following table in Access : Student (Sno number, Sname Text(50), Scity(50))
import java.sql.*;
public class Main {
public static void main(String[] args) throws Exception
{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn =
DriverManager.getConnection("jdbc:ucanaccess://C:/raviroza/ravi.mdb");
String Q = "Insert into Student values (101,'raviroza.com','world wide web')";
if(conn.createStatement().executeUpdate(Q)>0)
{
System.out.println("Recored added");
}
}
}
What is JDBC Prepared Statement?
- Sometimes it is more convenient to use a PreparedStatement object for sending SQL statements to the database. This special type of statement is derived from the more general class, Statement, that you already know.
- If you want to execute a Statement object many times, it usually reduces execution time to use a PreparedStatement object instead.