CASE STUDY
Creating Table
CREATE TABLE `members` (
`id` int(10) unsigned NOT NULL auto_increment,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`uname` varchar(45) NOT NULL,
`pass` varchar(45) NOT NULL,
`regdate` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Example</title>
</head>
<body>
<form method="post" action="[Link]">
<h2>Login Here</h2>
<label for="Username">Username</label><br>
<input type="text" name="uname"/><br>
<label for="Password">Password</label><br>
<input type="password" name="password"/><br>
<input type="submit" value="Login"/>
<input type="reset" value="Reset"/>
<strong> Yet Not Registered!!</strong>
<a href="[Link]">Register Here</a>
</form>
</body>
</html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Registration</title>
</head>
<body>
<form method="post" action="[Link]">
<h2>Enter Information Here</h2>
<label for="firstName">First Name</label><br>
<input type="text" name="fname" /></br>
<label for="lastName">Last Name</label><br>
<input type="text" name="lname" /></br>
<label for="email">Email</label><br>
<input type="text" name="email" /></br>
<label for="username">Username</label><br>
<input type="text" name="uname" /></br>
<label for="password">Password</label><br>
<input type="password" name="pass" /></br>
<input type="submit" value="Submit" /></td>
<input type="reset" value="Reset" /></td>
<strong>Already registered!!</strong>
<a href="[Link]">Login Here</a>
</form>
</body>
</html>
[Link]
<%@ page import ="[Link].*" %>
<%
String fname = [Link]("fname");
String lname = [Link]("lname");
String email = [Link]("email");
String uname = [Link]("uname");
String password = [Link]("password");
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost:3306/dbname",
"root", "dbpass");
String sql=”INSERT INTO users(fname , lname , email, uname, password) values
(?,?,?,?,?)”;
PreparedStatement stmt = [Link](sql);
[Link](1,fname );
[Link](2, lname );
[Link](3, email );
[Link](4, uname );
[Link](5,password );
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]("[Link]");
} else {
[Link]("[Link]");
}
%>
[Link]
Registration is Successful.
Please Login Here <a href='[Link]'>Go to Login</a>
[Link]
<%@ page import ="[Link].*" %>
<%
String uname = [Link]("uname");
String pwd = [Link]("pass");
[Link]("[Link]");
Connection con = [Link]("jdbc:mysql://localhost:3306/dbname",
"root", "dbpass");
String sql = "SELECT * FROM Users where uname=”+uname+" and
password=”+password+”;
Statement stmt = [Link]();
ResultSet rs= [Link](sql);
if ([Link]()) {
[Link]("uname", uname);
[Link]("[Link]");
} else {
[Link]("Invalid password <a href='[Link]'>try again</a>");
}
%>
[Link]
<%
if (([Link]("userid") == null) || ([Link]("userid") == "")) {
%>
You are not logged in<br/>
<a href="[Link]">Please Login</a>
<%} else {
%>
Welcome <%=[Link]("userid")%>
<a href='[Link]'>Log out</a>
<%
}
%>
[Link]
<%
[Link]("userid", null);
[Link]();
[Link]("[Link]");
%>
DATABASE (Using Prepared Statement)
//STEP 1. Import required packages
import [Link].*;
public class JDBCExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "[Link]";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
// Database credentials
static final String USER = "root";
static final String PASS = " ";
public void dbConnection() {
try {
//STEP 2: Register JDBC driver
[Link](JDBC_DRIVER);
//STEP 3: Open a connection
[Link]("Connecting to database...");
conn = [Link](DB_URL, USER, PASS);
if (conn != null) {
[Link]("Connected");
}
} catch (SQLException e) {
[Link]();
}
}
public void insert() {
try {
String sql = "INSERT INTO Users (username, password, fullname, email) VALUES
(?, ?, ?, ?)";
PreparedStatement stmt = [Link](sql);
[Link](1, "bill");
[Link](2, "secretpass");
[Link](3, "Bill Gates");
[Link](4, "[Link]@[Link]");
int rowsInserted = [Link]();
if (rowsInserted > 0) {
[Link]("A new user was inserted successfully!");
}
} catch (Exception e) {
[Link]();
}
}
public void select() {
try {
String sql = "SELECT * FROM Users";
Statement stmt = [Link]();
ResultSet rs= [Link](sql);
while ([Link]()) {
String uname = [Link](2);
String password = [Link](3);
String fname = [Link](4);
String email = [Link](5);
[Link](uname+” ”+password+” ”+fname+” ”+email);
}
} catch (Exception e) {
[Link]();
}
}
public void update() {
try {
String sql = "UPDATE Users SET password=?, fullname=?, email=? WHERE
username=?";
PreparedStatement stmt = [Link](sql);
[Link](1, "123456789");
[Link](2, "William Henry Bill Gates");
[Link](3, "[Link]@[Link]");
[Link](4, "bill");
int rowsUpdated = [Link]();
if (rowsUpdated > 0) {
[Link]("An existing user was updated successfully!");
}
} catch (Exception e) {
[Link]();
}
}
public void delete() {
try {
String sql = "DELETE FROM Users WHERE username=?";
PreparedStatement stmt = [Link](sql);
[Link](1, "bill");
int rowsDeleted = [Link]();
if (rowsDeleted > 0) {
[Link]("A user was deleted successfully!");
}
} catch (Exception e) {
[Link]();
}
}
public void close() {
try {
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
}
}
public static void main(String[] args) {
JDBCExample jdbc = new JDBCExample();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
Java Beans
[Link]
<html>
<head>
<title> Customer Form </title>
</head>
<body>
<h3>Please fill in the following details and submit it </h3> <br />
<form action="[Link]" method="GET">
First Name: <input type="text" name="firstName"> <br>
Last Name: <input type="text" name="lastName"> <br>
Age: <input type="text" name="age"> <br>
<input type="submit" name="Submit" />
</form>
</body>
</html>
[Link]
package beans;
public class Customer implements [Link] {
private String firstName = null;
private String lastName = null;
private int age = 0;
public Customer() {
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public int getAge(){
return age;
}
public void setFirstName(String firstName){
[Link] = firstName;
}
public void setLastName(String lastName){
[Link] = lastName;
}
public void setAge(Integer age){
[Link] = age;
}
}
[Link]
<html>
<body>
<H2>Reading the form data</H2>
<H3>Populating the bean and Storing in session </H3>
<jsp:useBean id="userInfo" class="[Link]" scope="session" />
<jsp:setProperty name="userInfo" property="firstName"
value="<%=[Link](" firstName")%>" />
<jsp:setProperty name="userInfo" property="lastName"
value="<%=[Link](" lastName")%>" />
<jsp:setProperty name="userInfo" property="age"
value="<%=[Link](" age")%>" />
<H3>Finished storing in the session </H3>
<a href="[Link]">Click Here </a> to invoke the servlet that process the bean
data in session.
</body>
</html>
[Link]
<html>
<body>
<jsp:useBean id="userInfo" class="[Link]"scope="session" />
<jsp:getProperty name="userInfo" property="firstName"/>
<jsp:getProperty name="userInfo" property="lastName"/>
<jsp:getProperty name="userInfo" property="age"/>
</body>
</html>
Servlet
<html>
<body>
<form action="servlet1" method="post">
Name:<input type="text" name="userName" /><br />
Password:<input type="password" name="userPass" /><br />
<input type="submit" value="login" />
</form>
</body>
</html>
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class Login extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String uname=[Link]("userName");
String password=[Link]("userPass");
if([Link]("servlet"){
RequestDispatcher rd=[Link]("servlet2");
[Link](request, response);
}
else{
[Link]("Sorry UserName or Password Error!");
RequestDispatcher rd=[Link]("/[Link]");
[Link](request, response);
}
}
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class WelcomeServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();
String n=[Link]("userName");
[Link]("Welcome "+n);
}
}
[Link]
<web-app>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>[Link]</welcome-file>
</welcome-file-list>
</web-app>
APPLET
To execute the applet by appletviewer tool, write in command prompt:
c:\>javac [Link]
c:\>appletviewer [Link]
/*
Basic Java Applet Example
This Java example shows how to create a basic applet using Java Applet class.
*/
import [Link];
import [Link];
/*
<applet code = "[Link]" width = 200 height = 200>
</applet>
*/
public class BasicAppletExample extends Applet{
public void paint(Graphics g){
//write text using drawString method of Graphics class
[Link]("This is my First Applet",20,100);
}
}
APPLET ARITHMETIC OPERATION
import [Link].*;
import [Link].*;
import [Link].*;
public class Calc extends Applet implements ActionListener {
Panel p1, p2;
Label l1, l2, l3;
TextField t1, t2, t3;
Button b1, b2, b3, b4;
public void init() {
p1 = new Panel(new GridLayout(3, 2));
p2 = new Panel(new FlowLayout());
l1 = new Label("Num 1");
l2 = new Label("Num 2");
l3 = new Label("Result");
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);
[Link](false);
[Link](l1);
[Link](t1);
[Link](l2);
[Link](t2);
[Link](l3);
[Link](t3);
b1 = new Button("add");
b2 = new Button("mul");
b3 = new Button("sub");
b4 = new Button("div");
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
setLayout(new GridLayout(2, 1));
add(p1);
add(p2);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
public void actionPerformed(ActionEvent ae) {
int a = [Link]([Link]());
int b = [Link]([Link]());
int c;
if ([Link]() == "add") {
c = a + b;
[Link]("" + c);
if ([Link]() == "mul") {
c = a * b;
[Link]("" + c);
if ([Link]() == "sub") {
c = a - b;
[Link]("" + c);
if ([Link]() == "div") {
double x = [Link]([Link]());
double y = [Link]([Link]());
double z = x / y;
[Link]("" + z);
SWING RADIO BUTTON
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class RadioButtonDemo implements ActionListener {
private JLabel l1;
public RadioButtonDemo() {
JFrame f= new JFrame(“JRadioButton Demo”);
[Link](new FlowLayout());
[Link](200, 200);
JRadioButton jr1 = new JRadioButton("Male");
JRadioButton jr2 = new JRadioButton("Female");
l1 = new JLabel();
ButtonGroup bg = new ButtonGroup();
[Link](jr1);
[Link](jr2);
[Link](this);
[Link](this);
[Link](jr1);
[Link](jr2);
[Link](l1);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
public void actionPerformed(ActionEvent ae) {
[Link]("You selected " + [Link]());
public static void main(String[] args) {
new RadioButtonDemo();
}
FACTORIAL
import [Link].*;
import [Link].*;
/**
*
* @author Apex Lab
*/
public class FactorialDemo {
int n=1,fact=1,i;
public FactorialDemo(){
Frame f=new Frame("CountDownDemo");
Label l1=new Label("n");
Label l2=new Label("Factorial(n)");
TextField t1=new TextField("1");
TextField t2=new TextField("1");
Button b1=new Button("Next");
[Link](new ActionListener(){
public void actionPerformed(ActionEvent e){
n++;
//int a=[Link]([Link]());
for(i=1;i<=n;i++){
fact=fact*i;
}
// fact=fact*n;
[Link]([Link](n));
[Link]([Link](fact));
fact=1;
}
});
[Link](l1);
[Link](t1);
[Link](l2);
[Link](t2);
[Link](b1);
[Link](new FlowLayout());
[Link](400,500);
[Link](true);
}
public static void main(String[] args){
new FactorialDemo();
}
HttpSession
[Link]
1. <form action="servlet1">
2. Name:<input type="text" name="userName"/><br/>
3. <input type="submit" value="go"/>
4. </form>
1. import [Link].*;
2. import [Link].*;
3. import [Link].*;
4.
5.
6. public class FirstServlet extends HttpServlet {
7.
8. public void doGet(HttpServletRequest request, HttpServletResponse response){
9. try{
10.
11. [Link]("text/html");
12. PrintWriter out = [Link]();
13.
14. String n=[Link]("userName");
15. [Link]("Welcome "+n);
16.
17. HttpSession session=[Link]();
18. [Link]("uname",n);
19.
20. [Link]("<a href='servlet2'>visit</a>");
21.
22. [Link]();
23.
24. }catch(Exception e){[Link](e);}
25. }
26.
27. }
import [Link].*;
import [Link].*;
public class AccumulatorDemo {
int accumulatedsum=0;
public AccumulatorDemo(){
Frame f=new Frame("AccumulatorDemo");
Label l1=new Label("Enter an integer ");
Label l2=new Label("Accumulated sum is ");
TextField t1=new TextField();
TextField t2=new TextField();
[Link](new TextListener(){
public void textValueChanged(TextEvent e){
int a=[Link]([Link]());
accumulatedsum = a + accumulatedsum;
[Link]([Link](accumulatedsum));
}
});
[Link](new GridLayout(2,2));
[Link](l1);
[Link](t1);
[Link](l2);
[Link](t2);
[Link](200,200);
[Link](true);
}
public static void main(String[] args){
new AccumulatorDemo();
}
}
import [Link].*;
import [Link].*;
/**
*
* @author Apex Lab
*/
public class CountDownDemo {
int counter=88;
public CountDownDemo(){
Frame f=new Frame("CountDownDemo");
Label l1=new Label("Counter");
TextField t1=new TextField("88");
Button b1=new Button("Count Down");
[Link](new ActionListener(){
public void actionPerformed(ActionEvent e){
counter--;
[Link]([Link](counter));
}
});
[Link](l1);
[Link](t1);
[Link](b1);
[Link](new FlowLayout());
[Link](400,500);
[Link](true);
}
public static void main(String[] args){
new CountDownDemo();
Simple JAva program
1. import [Link].*;
2. import [Link].*;
3. public class TextFieldExample implements ActionListener{
4. JTextField tf1,tf2,tf3;
5. JButton b1,b2;
6. TextFieldExample(){
7. JFrame f= new JFrame();
8. tf1=new JTextField();
9. [Link](50,50,150,20);
10. tf2=new JTextField();
11. [Link](50,100,150,20);
12. tf3=new JTextField();
13. [Link](50,150,150,20);
14. [Link](false);
15. b1=new JButton("+");
16. [Link](50,200,50,50);
17. b2=new JButton("-");
18. [Link](120,200,50,50);
19. [Link](this);
20. [Link](this);
21. [Link](tf1);[Link](tf2);[Link](tf3);[Link](b1);[Link](b2);
22. [Link](300,300);
23. [Link](null);
24. [Link](true);
25. }
26. public void actionPerformed(ActionEvent e) {
27. String s1=[Link]();
28. String s2=[Link]();
29. int a=[Link](s1);
30. int b=[Link](s2);
31. int c=0;
32. if([Link]()==b1){
33. c=a+b;
34. }else if([Link]()==b2){
35. c=a-b;
36. }
37. String result=[Link](c);
38. [Link](result);
39. }
40. public static void main(String[] args) {
41. new TextFieldExample();
42. } }