Home Jsp Connect JSP with mysql
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Connect JSP with mysql

Advertisement
In this section, you will learn how to connect mysql with jsp.

Connect JSP with mysql

     

Create a database : First create a database named 'usermaster' in mysql. To create this database we need to run the following query in sql prompt.

mysql> create database usermaster;

This query creates database 'usermaster' in Mysql.

Connect JSP with mysql :

Now in the following jsp code, you will see how to connect with the database. First you need to copy my sql connector jar file in lib directory of your application. Name of mysql driver is passed to the forName() method of 'Class' class which loads the driver. To get the connection with the database, getConnection() method of DriverManager class is used which takes three parameters connection url, username and password.

Here is the full jsp code.

ConnectJspToMysql.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>Connection with mysql database</title> 
</head> 
<body>
<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". 
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");
// check weather connection is established or not by isClosed() method 
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<% 
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>
</font>
</body> 
</html>

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

Advertisements

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 19, 2008

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
nat
April 16, 2011
connectionURL URGENT

Hello, Great code, but I don't know what is connectionURL. I bought hosting from the hosting provider, jsp, mysql already there. So i created a database there, it is localhost:3306. do i need my ip address for connection url - or what? thank you very much
OM
April 24, 2011
sql connector jar

First you need to copy my sql connector jar file in lib directory of your application. tha's where am getting lost. I tried and it still doesnt work. can you explain how to do that? thnks
ERNEST RUGEMA
December 30, 2011
request

when I try to fetch data from table into database ,I get message unable to connect to a database.what shall I do?your feedback is important. thinks!
manju
February 7, 2012
cant able to connect to database.

i am getting only unable to connect error. try a lot
vikash
August 17, 2012
mysql conn with jsp in linux

Unable to connect to database java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
DMCA.com