Database Application in JSP using NetBeans

Created by: Siddharth Chatrola

1. 2. 3. 4. 5. 6. 7. 8. Basic Creating application and adding drivers Creating Database Connection Creating Database Connection pool in application Creating Insert page Creating Update page Creating Delete page Final Screenshots

It contains four jsp files. o Index. o Go to File -> New project. o Insert. o You will see the above window.jsp : contains link for other three pages.1. o Delete. o Update.jsp : Insert data into STUDENT table and display.jsp : Delete data from STUDENT table and display. . o Select Web Application and give next. COLUMN NAME ID NAME BRANCH TYPE VARCHAR(15) VARCHAR(40) VARCHAR(40) DETAIL PRIMARY KEY NOT NULL NOT NULL 2. Here from categories choose Java Web. o You will see next window as shown below.jsp : Update data of STUDENT table and display. STUDENT table contains three fields and database is made using JavaDB. Basic:We make simple JSP application in NetBeans. o It will show you projects as shown in fig in right side tab. Creating application and adding drivers: o Start NetBeans.

We are not using framework here so don’t select anyone and click finish button. Our project is created. When you give next it will show the web server settings.o o o o o Here you can specify project name and path where to store project. We are using GlassFish as web server. . After giving next it will show window to choose framework.

Now. o It will add drivers to access database into your project. . If you are using other database then you have to include appropriate drivers. you have to create other three JSP pages. o Right click on Libraries -> Add JAR/Folders…. o It will open new window which ask for page name.o Index. I have to include driver for that into my library. Give appropriate name. o I have used JavaDB as database. o It will open a window. o Create Insert.jsp page is automatically created by default.jsp. browse through your hard disk and select appropriate JAR and click open. So.jsp and Delete.Update. o To do it.jsp. in project window write click on Web pages -> New -> JSP.

o You have to fill database details here.database type in this case it is JavaDB.3.  Name:.  Host:.  Port:. Creating Database Connection: o Go to services tab. o Details for these fields are as follows.It is the name of computer on which database is resides. User Name and Password is self explanatory. .Port number through which you connect to database  Database. o Right click on Databases and chose New Connection….

File Type select JDBC Resource and click next. Creating Database Connection pool in application: o Go to project window. . 4. It will open a window as shown below.o When you will click ok it will create connection to your database. o In Categories chose GlassFish . o I hope you have created STUDENT table in your database. o Right click on server resources -> New -> Others. o Select your project.

Give JNDI name.o o o o Select create New_JDBC Connection Pool. You will see window shown below. so it will ask for properties at that window just click next. I have given pool name jdbc/myDatasource. Then give next. . Here.

o Select a connection that you have created previously in services and click next. . Give connection pool name.o Here.

Now click finish button.o You will see connection pool properties. . in Resource references click on Add. o Open Web pages -> WEB-INF -> web.xml o Click on references tab.

jsp. Inside body part create one form. o Open Web pages -> WEB-INF -> sun-web. This is JNDI resource that we already created. o So page will be reloaded. 5. Inside that form tag create tax boxes and submit button.xml o Between </jsp-config> and </sun-web-app> tag enter following lines. <form action="insert. <resource-ref> <res-ref-name>jdbc/myDatasource</res-ref-name> <jndi-name>jdbc/myDatasource</jndi-name> </resource-ref> o It will create reference name correspond to JNDI name. request will go to page it self.o Give resource name in the textbox. if not already open. Creating Insert page: o o o o o Open Insert page. Put action of this form as insert. o When you click submit button.jsp" method="POST"> . Put all things in such a way so that final view will look like as follows. Code for this html form is as follows. o You can chose any arbitrary name as reference name.

sun. drag and drop JSTL If from there to application after last c:set tag. name and text. you have to include following two lines at top in JSP page. <c:set var="id" value="${param.branch}"/> o For using standard tag library. o First time when this page is called these values are}"/> <c:set var="branch" value="${param. <%@taglib prefix="sql" uri="http://java. go to palette window. . we get parameters from tax boxes that we want to enter into"%> o After this c:set statements insert one JSTL IF tag. put three c:set tags to get value of id. under that go to JSP tag. o When we come to this page after click on submit.<table border="0"> <tbody> <tr> <td>ID:-</td> <td><input type="text" name="id" value="" size="30" /></td> </tr> <tr> <td>NAME:-</td> <td><input type="text" name="name" value="" size="30" /></td> </tr> <tr> <td>BRANCH:-</td> <td><input type="text" name="branch" value="" size="30" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="SUBMIT" /></td> </tr> </tbody> </table> </form> o Above <form>}"/> <c:set var="name" value="${param. o It will show you following window. o To do"%> <%@taglib prefix="c" uri="http://java.

o If this condition is true then and only then we want to insert data into database. o Inside c:if tag write code for insert query. drag and drop DB Insert from there to application inside c:if tag. you have to insert data source name that we have created and insert query as shown in fig. o So all the code inside c:if tage will be executed if and only if condition is true. under that go to Database tag. o To do this.o Write condition as shown in fig. go to palette window. o It will show you following window. In which. .

o We want to insert parameter value that we set using c:set. drag and drop DB Report from there to application after </form> tag. o When you built and run application for first time it will show empty html table with only column names in report. we place three sql:param tags to set value of question mark. o It will add some code. insert query. go to palette window. o So after INSERT statement. o We want to print STUDENT table detail. under that go to Database tag. o Add following three statements after INSERT statement but inside sql:update tag. o Because initially database table is empty. value of name in place of second question mark and so on. o Now. <sql:param value="${id}"/> <sql:param value="${name}"/> <sql:param value="${branch}"/> o These statements set value of id in place of first question mark. data source name and variable as shown in fig and click ok. o To generate report. o Here. . code to insert data into database table is over.

Creating Update page: o o o o We made update page such that. Name and branch are also removed from every where. We will add one more option name as select in combo box for id.jsp page except followings. After clicking submit button.}"/> <c:if test="${(id != null) && (id != 'select') }"> . if id is null or select then we have to do nothing. We have to change action of form tag to Delete. 6. <select name="id"> <sql:query var="result" dataSource="jdbc/myDatasource"> SELECT ID FROM STUDENT </sql:query> <c:forEach var="row" items="${result. report show data into html table.rowsByIndex}"> <c:forEach var="column" items="${row}"> <option> <c:out value="${column}"/></option> </c:forEach> </c:forEach> </select> o Instead of insert query we put update query as shown below. BRANCH = ? WHERE ID =? <sql:param value="${name}"/> <sql:param value="${branch}"/> <sql:param value="${id}"/> </sql:update> o This completes our update page. <c:set var="id" value="${param. We also remove c:set for name and branch. we can not update id (primary key). All things are same as Insert.jsp We remove tax box and label for name and branch.jsp o Instead of using text box for id. o You have to change action of form to update. We choose any id and enter updated value for name and branch for that. Otherwise we have to run DELETE query as shown below.o After you insert data into database. <sql:update var="res" dataSource="jdbc/myDatasource"> UPDATE STUDENT SET NAME = ?. we use combo box. 7. Creating Delete page: o o o o o o o o All things are same as update page except following. Code for that is as shown below. we run UPDATE query instead of INSERT query.

Final Screenshots: o Insert page o Update Page o Delete Page .<sql:update var="res" dataSource="jdbc/myDatasource"> DELETE FROM STUDENT WHERE ID = ? <sql:param value="${id}"/> </sql:update> </c:if> 8.

Sign up to vote on this title
UsefulNot useful