
Quick Contact
Java Tutorial
- What is Java?
- History of Java
- Hello Java Program
- Features of Java
- Basic Syntax
- Java Setup
- Data Types in Java
- Java Variables
- Operators in Java
- JVM
- Java If-else Statement
- Switch Case Statement
- Java Loops
- Do-While Loop
- Java While Loop
- Continue Statement
- Break Statement in Java
- Constructors in Java
- Oops Concept in Java
- Features of OOPs
- Inheritance
- Exeception handeling
- Aggregation (HAS-A relationship) in Java
- Method Overriding in Java
- Method Overloading
- Java Static Keyword
- Java This Keyword
- Java Final Keyword
- Polymorphism
- Static Binding and Dynamic Binding
- Abstract class in Java
- Access Modifiers in Java
- Difference between abstract class and interface
- Interface in Java
- Garbage Collection in Java
- Java Package
- Encapsulation
- Serialization and Deserialization in Java
- Java Inner Classes
- Java Applets
- Multithreading in Java
- Thread Priorities in Java
- Thread Creation
- Inter Thread Communication
- Wrapper Classes in Java
- Java Input Output
- Java AWT Introduction
- Java Layout Manager
- Java Layout Policy
- Java AWT Events
- Collection Framework
- Collection Framework List Interface
- Swing in Java
- Swing Utility Classes
- Swing Layout Managers
- Java JDBC
- Hibernate Framework Overview – Architecture and Basics
Springboot
- Spring Environment Setup
- Spring Boot CRUD REST API Project using IntelliJ IDEA | Postman | MySQL
- Dockerizing Spring Boot Application | Spring Boot Docker Tutorial
- spring-boot-restapidocumentation with swagger
- Spring Boot HttpClient Overview
- Apache HttpClient POST HTTP Request Example
- Apache HttpClient PUT HTTP Request Example
- Apache HttpClient DELETE HTTP Request Example
- Apache HttpClient HTML Form POST Request Example
- Spring Boot JSP Exampl
- Deploying Spring Boot WAR file with JSP to Tomcat
- Spring Boot Annotations
- Spring Core Annotations
- Spring MVC Annotations with Examples
- Spring Scheduling Annotations
- Spring - Java-based Container Configuration
- Spring Java Based Configuration Example
Hibernate
- Hibernate 5 hello world
- Hibernate- One to One Unidirectional Mapping Annotation Example
- Hibernate - Batch Processing
- Hibernate - Interceptors
- Hibernate 5 - Create, Read, Update and Delete (CRUD) Operations Example
- Hibernate Transaction Management
- Hibernate One to Many Unidirectional Mapping Example
- Hibernate One to Many Bidirectional Mapping Example
- Hibernate Many to Many Annotation Mapping Example
- Hibernate Primary KeyJoin Column
- Hibernate First Level Cache with Example
- Hibernate XML Configuration Example with Maven + Eclipse + MySQL Database
- Hibernate Java Configuration Example
- JPA 2 with Hibernate 5 Bootstrapping Example
- JPA and Hibernate Cascade Types
- Hibernate/JPA - Primary Key Generation
- Hibernate 5 - Enum Type Mapping Example
- Hibernate Component Mapping
- Hibernate Object States – Transient,Persistent and Detached
- Hibernate 5 - Save an Entity Example
- Hibernate 5 - Persist an Entity Example
- Hibernate 5 - saveOrUpdate() Method Example
- Hibernate 5 - get(), load() and byId() Method Examples
- Hibernate 5 - merge() Example
- Hibernate 5 - Delete or Remove an Entity Example
- Hibernate 5 - load() Method Example
- Hibernate Session Interface Methods
- Hibernate Session.clear() Method Example
- Introduction Of Java strutes to Architecture
- Struts 2 - Architecture
- Struts 2 - Configuration Files
- Struts 2 - Actions
- Struts 2 - Interceptors
- Struts 2 - Results & Result Types
- Struts 2 - Value Stack/OGNL
- Struts 2 - File Uploading
- Struts 2 - Database Access
- Struts 2 - Validations Framework
JAVA FX
- JavaFX Tutorial
- Introduction to JavaFX Pane
- JavaFX Popup
- JavaFX group
- JavaFX Controller
- JavaFX Gradient Color
- JavaFXAnchorPane
- JavaFXTabPane
- JavaFX Scene
- JavaFX Stage
- JavaFXWebView
- JavaFX Timeline
- JavaFX Timer
- JavaFX Image
- JavaFX Background
- JavaFX dialog
- JavaFX Font
- JavaFXTextArea
- JavaFXObservableList
- JavaFX GUI
- JavaFX FXML
- JavaFXEventHandler
- JavaFXGradle
- JavafxScrollpane
- JavaFXAPI
Struts 2 – Database Access
Struts is a MVC framework and not a database framework but it provides excellent support for JPA/Hibernate integration. We shall look at the hibernate integration in a later chapter, but in this chapter we shall use plain old JDBC to access the database.
The first step in this chapter is to setup and prime our database. I am using MySQL as my database for this example. I have MySQL installed on my machine and I have created a new database called “struts_tutorial”. I have created a table called login and populated it with some values. Below is the script I used to create and populate the table.
My MYSQL database has the default username “root” and “root123” password
CREATE TABLE `struts_tutorial`.`login`( `user` VARCHAR(10) NOT NULL , `password` VARCHAR(10) NOT NULL , `name` VARCHAR(20) NOT NULL , PRIMARY KEY (`user`) ) ENGINE =InnoDB; INSERT INTO `struts_tutorial`.`login`(`user`,`password`,`name`) VALUES ('scott','navy','Scott Burgemott');
Next step is to download the MySQL Connector jar file and placing this file in the WEB-INF\lib folder of your project. After we have done this, we are now ready to create the action class.
Create Action
The action class has the properties corresponding to the columns in the database table. We have user, password and name as String attributes. In the action method, we use the user and password parameters to check if the user exists, if so, we display the user name in the next screen.
If the user has entered wrong information, we send them to the login screen again.
Following is the content of LoginAction.java file −
package com.ducatindia.struts2; importjava.sql.Connection; importjava.sql.DriverManager; importjava.sql.PreparedStatement; importjava.sql.ResultSet; import com.opensymphony.xwork2.ActionSupport; publicclassLoginActionextendsActionSupport{ privateString user; privateString password; privateString name; publicString execute(){ String ret = ERROR; Connection conn =null; try{ String URL ="jdbc:mysql://localhost/struts_tutorial"; Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(URL,"root","root123"); Stringsql="SELECT name FROM login WHERE"; sql+=" user = ? AND password = ?"; PreparedStatementps=conn.prepareStatement(sql); ps.setString(1, user); ps.setString(2, password); ResultSetrs=ps.executeQuery(); while(rs.next()){ name=rs.getString(1); ret= SUCCESS; } }catch(Exception e){ ret= ERROR; }finally{ if(conn !=null){ try{ conn.close(); }catch(Exception e){ } } } return ret; } publicStringgetUser(){ return user; } publicvoidsetUser(String user){ this.user= user; } publicStringgetPassword(){ return password; } publicvoidsetPassword(String password){ this.password= password; } publicStringgetName(){ return name; } publicvoidsetName(String name){ this.name = name; } }
Create Main Page
Now, let us create a JSP file index.jsp to collect the username and password. This username and password will be checked against the database.
<%@ page language ="java"contentType="text/html; charset = ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@taglib prefix ="s"uri="/struts-tags"%>
< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < html> < head> < title>Login< /title> < /head> < body> < formaction="loginaction"method="post"> User:< br/>< inputtype="text"name="user"/>< br/> Password:< br/>< inputtype="password"name="password"/>< br/> < inputtype="submit"value="Login"/> < /form> < /body> < /html>
Create Views
Now let us create success.jsp file which will be invoked in case action returns SUCCESS, but we will have another view file in case of an ERROR is returned from the action.
<%@ page contentType="text/html; charset = UTF-8" %>
<%@taglib prefix ="s"uri="/struts-tags" %>
< html> < head> < title>Successful Login< /title> < /head> < body> Hello World, < s:propertyvalue="name"/> < /body> < /html>
Following will be the view file error.jsp in case of an ERROR is returned from the action.
<%@ page contentType="text/html; charset = UTF-8" %>
<%@taglib prefix ="s"uri="/struts-tags" %>
< html> < head> < title>Invalid User Name or Password< /title> < /head> < body> Wrong user name or password provided. < /body> < /html>
Configuration Files
Finally, let us put everything together using the struts.xml configuration file as follows −
< ?xml version ="1.0"Encoding="UTF-8"?> < !DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> < struts> < constantname="struts.devMode"value="true"/> < packagename="helloworld"extends="struts-default"> < actionname="loginaction" class="com.ducatindia.struts2.LoginAction" method="execute"> < resultname="success">/success.jsp< /result> < resultname="error">/error.jsp< /result> < /action> < /package> < /struts>
Following is the content of web.xml file −
< ?xml version ="1.0"Encoding="UTF-8"?> < web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"version="3.0"> < display-name>Struts 2< /display-name> < welcome-file-list> < welcome-file>index.jsp< /welcome-file> < /welcome-file-list> < filter> < filter-name>struts2< /filter-name> < filter-class> org.apache.struts2.dispatcher.FilterDispatcher < /filter-class> < /filter> < filter-mapping> < filter-name>struts2< /filter-name> < url-pattern>/*< /url-pattern> < /filter-mapping> < /web-app>
Apply now for Advanced Java Training Course