How to access MySQL database from JSP?
In this tutorial,I will teach you how to access data from JSP page. We are using MySQL database and we have a table called book details where area is filled for ID,book name and author. And this is the SQL to create the table.So, first of all create the books database (My SQL minus roots minus zero). So we have connected to the MySQL server. And now we are creating the books table.
It is created. Now we are connecting books database. So there is no table in the database. Now there is a query to create the table called 'books details'. So,it is created now. Let's check the table .
Show tables. So this is the table. Now we have book details table. Now we have book details table and next thing is to create the web application in eclipse. Write the JSP page and then test the code. So, the first step is to create the project - dynamic web project - JSP access database. Click next and then again next and then select generateweb.xml deployment descriptor and click on finish.And within the lib directory we have to add the libraries to connect to the MySQL database.
Here is the video tutorial of "How to access MySQL database from JSP?":
So, we have pasted it here and in the project now we should create a linl books detail and thwe name should be book entry form.jpg. This is the link and now we will create a new JSP file and name it as book entry.jsp and click finish. And this is the code we can copy it here and we have to change the database here. Books is the table and user name is root and password is also root.
So let's run the example first. 'Run as ' on java's server. Since 'tom cat' is already embedded with this we can run the project right away.So it is showing error that access derived for user,root-local host. So, some error is still there and the error is identified. Now right click and 'run as' on server and click finish. There is no error .
Book name-java 1, author - Deepak kumar and submit. It's edited and now we select from select asterisk from book underscore(_) details. And it's here and now we can go back again.Name JSP Book 1,Author -Deepak kumar and it is added and thus it runs. It's a very simple program in JSP which adds the data into the databaseas well as display data from the database.Let's see how it works.We have a project and this is the index.JSP file. This file is loaded when application is accessed.So this is the default file and we have a link from the book form.JSP.
In this book form.JSP we have the MySQL driver which is connected to the database 'local host', books database with the user name root and password root. The connection is created and if the action is not null it means some form is submitted by using an user name and book.
It is getting the data-request.get parameter,book name and this is the form. It is getting the value ,book name and author from this entry form. There is a SQL insert into book details and author and these are the values. And with the help of statement objects it is executing the query for getting data statement and select. Select asterisk from book details and data is selected.
Now it is displaying all the data in the database.SO while rst.next is displaying book name and the author finally closing the rst statement and the connection.And this is java script validation for validating the form.Enter book name and author name,It is a very simple program in JSP which accesses the data from My SQL database.
You can download the source code from website for this project and use it from experimenting in JSP SQL.This is the tutorial details and you can download it.S o first of all we have created a database then table is created and then in elipse we have created the project and this is the file for starting the data and in displaying from the database.
We have thousands of tutorials on JSP programming on our website roseindia.net and you can learn all these JSP tutorials by visiting our website.Apart from this we have tutorials on Advanced java,hibernate framework,struts framework etc. So keep visiting our site. www.roseindia.net