Home Jsp Connect JSP with mysql


Share on Google+Share on Google+

Connect JSP with mysql

In this example we will show you how to connect to MySQL database from your JSP code.

Connect JSP with mysql


In this example we will show you how to connect to MySQL database from your JSP code. First, you need to create database and then write jsp code to connect jsp to database.

1. Create a database: 

First create a database named usermaster in mysql. Before running the jsp code you need to paste mySqlConnector.jar in lib directory of jdk.

mysql> create database usermaster;
(This query create database in my sql command prompt)

2. Connect JSP with mysql:  

Here is the jsp code.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 

<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 

<title>Connection with mysql database</title> 
<h1>Connection status </h1>
try {
/* 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 usermaster. */
String connectionURL = "jdbc:mysql://localhost:3306/usermaster"; 

// declare a connection by using Connection interface 
Connection connection = null; 

// Load JBBC driver "com.mysql.jdbc.Driver" 

/* 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");

// check weather connection is established or not by isClosed() method 
<font size="+3" color="green"></b>
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
catch(Exception ex){
<font size="+3" color="red"></b>
out.println("Unable to connect to database.");

Output of the program when connection is established with specified mysql database :

Output of the program when unable to connect to specified MySQL database :

Download source code


Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 4, 2008

Ask Questions?    Discuss: Connect JSP with mysql   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 1, 2011

hello!! the solution provided for database connectivity was woking properly, it worked when i copy mySqlConnector.jar in lib folder.
June 9, 2011

I tried the code for connecting jsp to mysql. Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.' at line 1 (0 ms taken) That was the result when I execute the query. What is my mistake? Thanks!
November 17, 2011

How can i connect data base in MysQl???
January 3, 2012

what shall I do when Output of the program is unable to connect to specified MySQL database .please your feedback is important.
March 9, 2012
data base

con't connect db in mysql for jsp
March 22, 2012

I would like to show some appreciation to you just for ruecsing me from this type of crisis. Right after checking throughout the world wide web and seeing ideas which were not helpful, I believed my life was over. Living without the presence of answers to the issues you've solved as a result of your main article content is a crucial case, as well as ones that would have negatively damaged my entire career if I had not discovered your website. That expertise and kindness in controlling all the details was priceless. I'm not sure what I would have done if I had not encountered such a subject like this. I'm able to at this moment look ahead to my future. Thanks a lot very much for your high quality and effective guide. I will not be reluctant to propose your web blog to anyone who would need recommendations about this problem.
April 25, 2012
connection problem in jsp using mysql

i have written this code using mysql and java and it is not connecting it is giving the message as u have mentioned above in program,,,, what should i do to connect ??? if i try java application using mysql and compile using javac it is connecting,, it is creating problem in jsp... it doesn't give me any error.. please help..
July 22, 2012
Not works

I am 1000% sure that i followed every step precisely and i can access my databases through navicat too but it is still unable to connect, username and password are correct too.
ashem dijen singh
August 24, 2012

September 27, 2012

thanks a lot ..you saved my day
January 5, 2013

how to develop knowledge in java and how to become a good programmer.give your suggestions
barack hussein
May 15, 2013

Please help me,i followed the step but i am not able to connect jsqp and database mysql.I paste mysqlconnector in lib folder of JDK but it's not working.
August 5, 2013

how i copy the mysqlconnector.jar file.where it is