diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..e7e9d11
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,2 @@
+# Default ignored files
+/workspace.xml
diff --git a/.idea/Sevlet_Assignment.iml b/.idea/Sevlet_Assignment.iml
new file mode 100644
index 0000000..d6ebd48
--- /dev/null
+++ b/.idea/Sevlet_Assignment.iml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..08aa60e
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..f67f056
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Administrator.java b/src/Administrator.java
new file mode 100644
index 0000000..f3cd98b
--- /dev/null
+++ b/src/Administrator.java
@@ -0,0 +1,25 @@
+package com.wipro.book.service;
+
+import com.wipro.book.bean.BookBean;
+import com.wipro.book.dao.BookDao;
+
+public class Administrator {
+ public String addBook(BookBean bookBean) {
+ if(bookBean==null||bookBean.getBookName().equals("")||bookBean.getIsbn().equals("")||bookBean.getBookType()!='T'&&bookBean.getBookType()!='G'||bookBean.getCost()<=0||bookBean.getAuthor().getAuthorName()==null||bookBean.getAuthor().getAuthorName().equals("")){
+ return "INVALID";
+ }
+ int res = new BookDao().createBook(bookBean);
+ if(res == 1){
+ return "SUCCESS";
+
+ }
+ else{
+ return "FAILURE";
+ }
+ }
+ public BookBean viewBook(String isbn) {
+ if(isbn.equals(""))
+ return null;
+ return new BookDao().fetchBook(isbn);
+ }
+}
diff --git a/src/AuthorBean.java b/src/AuthorBean.java
new file mode 100644
index 0000000..858aa0d
--- /dev/null
+++ b/src/AuthorBean.java
@@ -0,0 +1,26 @@
+package com.wipro.book.bean;
+
+public class AuthorBean {
+ private int authorCode;
+ private String authorName;
+ private long contactNumber;
+ public int getAuthorCode() {
+ return authorCode;
+ }
+ public void setAuthorCode(int authorCode) {
+ this.authorCode = authorCode;
+ }
+ public String getAuthorName() {
+ return authorName;
+ }
+ public void setAuthorName(String authorName) {
+ this.authorName = authorName;
+ }
+ public long getContactNumber() {
+ return contactNumber;
+ }
+ public void setContactNumber(long contactNumber) {
+ this.contactNumber = contactNumber;
+ }
+
+}
diff --git a/src/AuthorDao.java b/src/AuthorDao.java
new file mode 100644
index 0000000..995d9a4
--- /dev/null
+++ b/src/AuthorDao.java
@@ -0,0 +1,56 @@
+package com.wipro.book.dao;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import com.wipro.book.bean.AuthorBean;
+import com.wipro.book.util.*;
+public class AuthorDao {
+ public AuthorBean getAuthor(int authorCode) {
+ Connection conn = DBUtil.DBConnection();
+ PreparedStatement ps;
+ try {
+ ps = conn.prepareStatement("select * from author_tbl_me12 where author_code=?");
+ ps.setInt(1, authorCode);
+ ResultSet rs = ps.executeQuery();
+ AuthorBean bean = null;
+ while(rs.next()){
+ bean = new AuthorBean();
+ bean.setAuthorCode(authorCode);
+ bean.setAuthorName(rs.getString(2));
+ bean.setContactNumber(rs.getLong(3));
+ }
+ return bean;
+ }
+ catch(SQLException e) {
+ e.printStackTrace();
+ }
+ catch(NullPointerException e) {
+ System.out.println("error" + authorCode);
+ }
+ return null;
+ }
+ public AuthorBean getAuthor(String authorName) {
+ Connection conn = DBUtil.DBConnection();
+ PreparedStatement ps;
+ try {
+ ps = conn.prepareStatement("select * from author_tbl_me12 where author_name=?");
+ ps.setString(1, authorName);
+ ResultSet rs = ps.executeQuery();
+ AuthorBean bean = new AuthorBean();
+ while(rs.next()){
+ bean.setAuthorCode(rs.getInt(1));
+ bean.setAuthorName(rs.getString(2));
+ bean.setContactNumber(rs.getLong(3));
+ }
+ return bean;
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+}
diff --git a/src/BookBean.java b/src/BookBean.java
new file mode 100644
index 0000000..6b0af41
--- /dev/null
+++ b/src/BookBean.java
@@ -0,0 +1,39 @@
+package com.wipro.book.bean;
+
+public class BookBean {
+ private String isbn;
+ private String bookName;
+ private AuthorBean author;
+ private char bookType;
+ private float cost;
+ public String getIsbn() {
+ return isbn;
+ }
+ public void setIsbn(String isbn) {
+ this.isbn = isbn;
+ }
+ public String getBookName() {
+ return bookName;
+ }
+ public void setBookName(String bookName) {
+ this.bookName = bookName;
+ }
+ public AuthorBean getAuthor() {
+ return author;
+ }
+ public void setAuthor(AuthorBean author) {
+ this.author = author;
+ }
+ public char getBookType() {
+ return bookType;
+ }
+ public void setBookType(char bookType) {
+ this.bookType = bookType;
+ }
+ public float getCost() {
+ return cost;
+ }
+ public void setCost(float cost) {
+ this.cost = cost;
+ }
+}
diff --git a/src/BookDao.java b/src/BookDao.java
new file mode 100644
index 0000000..97678a4
--- /dev/null
+++ b/src/BookDao.java
@@ -0,0 +1,55 @@
+package com.wipro.book.dao;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import com.wipro.book.bean.BookBean;
+import com.wipro.book.util.DBUtil;
+
+public class BookDao {
+ public BookBean fetchBook(String isbn){
+ Connection conn = DBUtil.DBConnection();
+ PreparedStatement ps;
+ try {
+ ps = conn.prepareStatement("select * from book_tbl_me12 where ISBN=?");
+ ps.setString(1, isbn);
+ ResultSet rs = ps.executeQuery();
+ BookBean bean =null;
+ while(rs.next()){
+ bean = new BookBean();
+ bean.setIsbn(isbn);
+ bean.setBookName(rs.getString(2));
+ bean.setBookType(rs.getString(3).charAt(0));
+ bean.setAuthor( new AuthorDao().getAuthor(rs.getInt(4)));
+ bean.setCost(rs.getFloat(5));
+ }
+ return bean;
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ catch(NullPointerException e){
+ System.out.println("error" + isbn);
+
+ }
+
+ return null;
+ }
+
+ public int createBook(BookBean book){
+ Connection conn = DBUtil.DBConnection();
+ try {
+ PreparedStatement ps = conn.prepareStatement("insert into book_tbl_me12 values(?,?,?,?,?)");
+ ps.setString(1, book.getIsbn());
+ ps.setString(2, book.getBookName());
+ ps.setString(3,((Character)book.getBookType()).toString());
+ ps.setInt(4, book.getAuthor().getAuthorCode());
+ ps.setFloat(5, book.getCost());
+ ps.execute();
+ return 1;
+ } catch (SQLException e) {
+ e.printStackTrace();
+ return 0;
+ }
+ }
+}
diff --git a/src/DBUtil.java b/src/DBUtil.java
new file mode 100644
index 0000000..047731f
--- /dev/null
+++ b/src/DBUtil.java
@@ -0,0 +1,22 @@
+package com.wipro.book.util;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+public class DBUtil {
+ public static Connection DBConnection() {
+ try {
+ Class.forName("oracle.jdbc.driver.OracleDriver");
+ Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@orcl.rmk.ac.in:1521:ORCL","username","password");
+ return conn;
+ }
+ catch(ClassNotFoundException e) {
+ System.out.println("Driver Not Found");
+ }
+ catch(SQLException e) {
+ System.out.println("Could not connect to database");
+ e.printStackTrace();
+ }
+ return null;
+ }
+}
diff --git a/src/MainServlet.java b/src/MainServlet.java
new file mode 100644
index 0000000..91846f5
--- /dev/null
+++ b/src/MainServlet.java
@@ -0,0 +1,97 @@
+package com.wipro.book.servlets;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import com.wipro.book.bean.BookBean;
+import com.wipro.book.dao.AuthorDao;
+import com.wipro.book.service.Administrator;
+
+@WebServlet("/MainServlet");
+public class MainServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ public MainServlet() {
+ super();
+ }
+
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+
+ response.getWriter().append("Served at: ").append(request.getContextPath());
+ }
+
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+
+ String operation = request.getParameter("operation");
+ if(operation.equals("AddBook")){
+ String result = addBook(request);
+
+ if(result.equals("SUCCESS")){
+ RequestDispatcher rd = request.getRequestDispatcher("/Menu.html");
+ rd.include(request, response);
+ }
+ else if(result.equals("FAILURE")){
+ RequestDispatcher rd = request.getRequestDispatcher("Failure.html");
+ rd.include(request, response);
+ }
+ else{
+ RequestDispatcher rd = request.getRequestDispatcher("Invalid.html");
+ rd.include(request, response);
+ }
+ }
+ if(operation.equals("Search")){
+ BookBean bean = viewBook(request.getParameter("isbn"));
+ if(bean==null){
+ RequestDispatcher rd = request.getRequestDispatcher("Invalid.html");
+ rd.include(request, response);
+ return;
+ }
+ response.setContentType("text/html");
+ PrintWriter out = response.getWriter();
+ out.println("
View Book");
+ String type="";
+ if(bean.getBookType()=='T')
+ type = "Technical";
+ else if(bean.getBookType()=='G')
+ type = "General";
+ else{
+ RequestDispatcher rd = request.getRequestDispatcher("Failure.html");
+ rd.forward(request, response);
+ return;
+ }
+
+ out.println("| ISBN | "+bean.getIsbn()+" |
");
+ out.println("| Book Name | "+bean.getBookName()+" |
");
+ out.println("| Book Type | "+type+" |
");
+ out.println("| Author Name | "+bean.getAuthor().getAuthorName()+" |
");
+ out.println("| Contact | "+bean.getAuthor().getContactNumber()+" |
");
+ out.println("| Cost | "+bean.getCost()+" |
");
+ out.println("");
+ out.close();
+
+ }
+ }
+
+ public String addBook(HttpServletRequest request){
+ Administrator admin = new Administrator();
+ BookBean bean = new BookBean();
+ bean.setIsbn(request.getParameter("isbn"));
+ bean.setBookName(request.getParameter("bookname"));
+ bean.setBookType(request.getParameter("booktype").charAt(0));
+ bean.setAuthor(( new AuthorDao()).getAuthor(Integer.parseInt(request.getParameter("author")) ));
+ bean.setCost(Float.parseFloat(request.getParameter("cost")));
+ return admin.addBook(bean);
+ }
+
+
+ public BookBean viewBook(String isbn){
+ return new Administrator().viewBook(isbn);
+ }
+}