Connect to mysql database using jsp
Connect to mysql database using jsp
Connect to MySQL database using jsp
This can be deployed in apache tomcat and Table needs to be created in Mysql. This is the very first tutorial on JSP and MYSQL in our website. A special thanks to Sean Ni Cleirigh for his support.
Steps to Follow:
1. create a database named ‘user’ in mysql and create table “user_details”.
2. Create an application directory named ‘user’ in the Tomcat.
3. Before running this java code you need mysql connector jar
in the
Creating JSP pages:
In this example, we have created two JSP pages.
First page is to provide link to create table and the next page for processing the table creation.
1. Hello_database_query.jsp
2.create_table.jsp
1. Hello_database_query.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="java.sql.*" %> <%@ page import="java.io.*" %> <html> <head> <title>Create table in mysql database using jsp</title> </head> <body> <TABLE style="background-color: #ffffcc;"> <TR> <TD align="center"><h2>To create table click on the link given below</h2></TD> </TR> <TR> <TD align="center"><A HREF="create_table.jsp"> <font size="+2" color="blue">create table</font> </TR> </TABLE> </body> </html>
Save this code as a .jsp file named “Hello_database_query.jsp” in the directory Tomcat-6.0.16/webapps/user/.
You can run this jsp page typing http://localhost:8080/user/Hello_database_query.jsp url in address bar of the browser.
2.create_table.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="java.sql.*" %> <%@ page import="java.io.*" %> <html> <head> <title>display data from the table using jsp</title> </head> <body> <% /* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is user. */ String connectionURL = "jdbc:mysql://localhost:3306/user"; // declare a connection by using Connection interface Connection connection = null; // declare object of Statement interface that is used for // executing sql statements. Statement statement = null; try { // Load JBBC driver "com.mysql.jdbc.Driver". Class.forName("com.mysql.jdbc.Driver").newInstance(); /* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database. */ connection = DriverManager.getConnection(connectionURL, "root", "root"); /* createStatement() is used for create statement object that is used for sending sql statements to the specified database. */ statement = connection.createStatement(); // sql query to retrieve values from the secified table. String QueryString = "create table user_details(id int not null auto_increment,name " + "varchar(25),city varchar(20), primary key(id));"; // execyteUpdate() mothod execute specified sql query. statement.executeUpdate(QueryString); %> <TABLE border="0" style="background-color: #ffffcc;"> <TR> <TD align="center" ><font size="+3" color="green">Congratulations ! </font></TD> <tr><TD align="center" ><font size="+2">Table of specified name is created successfully.</font> </TD></tr> </TR> <% } catch (Exception ex) {%> <TABLE border="0" style="background-color: #ffffcc;"> <TR> <TD align="center" ><font size="+3" color="red">Some problems to create table.</font></TD> </TR> <% } finally { // close all the connections. statement.close(); connection.close(); } %><tr><td align="center" > <A HREF="Hello_database_query.jsp"> <font size="5" color="blue">back to home page</font></A></td> </tr> </TABLE> </body> </html>
Save this code with name “create_table.jsp” in the directory
This jsp code creates table of specified name in the mysql database and shows a response page.
If table exists already or found any error to make connection with database then shows an error page.
Thanks to codingbasics.
Incoming search terms:
- create pdf with mysql use jsp (2)
- jsp and mysql tutorial pdf (2)
- access mysql with javascript using tomcat Apache (1)
- JSP MySql PDF (1)
- jsp program to connect to database on tomcat (1)
- jsp program to insert data in table in mysql using primary key:pdf (1)
- jsp simple databse connectivity with xampp (1)
- jsp with mysql connections with step by step procedures (1)
- mysql jsp pdf (1)
- photo gallary using mysql jsp (1)
- plmadm (1)
- retrieve mysql columns on dropdown change in jsp (1)
- searching a database using jsp pdf tutorial (1)
- searching records from the database code in jsp (1)
- slideshow images database jsp (1)
- steeps to connect jsp to mysql from xamp (1)
- jsp mysql create table (1)
- Jsp mysql connection pdf (1)
- Create a jQuery slideshow using database in jsp (1)
- database connectivity for mysql in xampp using jsp (1)
- database of site in apache tomcat (1)
- example of html code for upload pictures using jsp in mysql (1)
- fetch database columns onchange of dropdown in jsp (1)
- get mysql table columns on dropdown change in jsp (1)
- how to show image on pdf from mysql database using jsp (1)
- javascript createDatabaseConnection (1)
- jquery ui theme jsp apache tomcat (1)
- jsp and mysql database connectivity pdf (1)
- jsp and mysql pdf (1)
- jsp connectivity with mysql pdf (1)
- jsp datbase images slideshow examole (1)
- where do i import database for jsp in xampp tomcat (1)