Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp Connect JSP with mysql

Related Tutorials


 
 

Share on Google+Share on Google+

Connect JSP with mysql

Advertisement
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.

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: 
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

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

Discuss: Connect JSP with mysql   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:14
Francis
March 22, 2012
CoDLxPnnvWJriJ

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.
Neeraj
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..
dularl
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
java

good
raju
September 27, 2012
thanks

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

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

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.
prashanth r
June 12, 2014
not able to connect to database using jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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/students"; // 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", "123"); // check weather connection is established or not by isClosed() method if(!connection.isClosed()) %> <font size="+3" color="green"> <% 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> but it is showing that unable to connect database... please tell me what is wrong with my code
sangeetha
August 5, 2013
help

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