Month: August 2013

Java and MySQL Connectivity

Connecting java with database is heart of any application.

Below is code of a jsp page simply save it with any file name with .jsp to use.

 <%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JDBC Demo with MySQL</title>
</head>
<body>
<table>
<%@ page import="java.util.*" %>
<%@ page import="javax.sql.*;" %>
<%

java.sql.Connection connection = null;
java.sql.Statement statement = null;
java.sql.ResultSet resultSet = null;
java.sql.PreparedStatement preparedStatement = null;

// Change below three variables according to your database settings.
String connectionString= "jdbc:mysql://127.0.0.1:3306/mydb";
String userId= "root";
String password = "admin";

try
{
Class.forName("com.mysql.jdbc.Driver");
connection = java.sql.DriverManager.getConnection(connectionString, userId, password);
}
catch(ClassNotFoundException classNotFoundException)
{
classNotFoundException.printStackTrace();
}

//employee is table name in database mydb.
String sqlSelectQueryString = "select * from employee";

try
{
statement= connection.createStatement();
resultSet = statement.executeQuery(sqlSelectQueryString);
%>

<%
while( resultSet.next() )
{
%><tr>
<td><%= resultSet.getString("EmpId") %></td>
<td><%= resultSet.getString("EmpName") %></td>
</tr>
<%
}
%>

<%
}

catch(Exception exception)
{
exception.printStackTrace();
}
finally
{
if(resultSet != null)
resultSet.close();
if(statement != null)
statement.close();
if(connection !=null)
connection.close();
}

%>

</table>
</body>
</html>
 

Assuming that you will be using this inside the Eclipse IDE.

Once you will run this in Eclipse then an exception will be shown to you something like:-

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
………………………………………………………………………………………………………………………………………………….
…………………………………………………………………………………………………………………………………………………..

This is major issue,this exception is need to handle,otherwise you will never know what’s issue is.As you can see it is ClassNotFoundException and asking for ‘com.mysql.jdbc.Driver’.So now lets download it from here:-

http://dev.mysql.com/downloads/mirror.php?id=13598

you will found a zip with name:-‘mysql-connector-java-5.0.8.zip’
Just extract it and copy “mysql-connector-java-5.0.8-bin.jar” from here
and paste it into ‘lib’ folder inside ‘WEB-INF’ folder in Eclipse.
After this just clean and rebuild your application.Now no exception is going to occur.
And you will see output into your page working perfectly.

For additional info on the same you can reffer to the url below:-
http://www.cbcb.umd.edu/confcour/CMSC424/Simple%20tutorial%20for%20using%20JDBC.pdf

Also good information can be found on:-

http://dev.mysql.com/doc/refman/5.6/en/connector-j-installing.html

Enjoy………..

Installing Eclipse For Java

Eclipse an one of the most popular IDE for Java based application development.You can find necessary steps from url below:-

http://www.javaprogrammingforums.com/java-jdk-ide-tutorials/253-beginners-eclipse-tutorial-how-run-first-java-application-eclipse.html

Or if you directly want to download then follow this:-

http://www.eclipse.org/downloads/

Better to go for the version with lable,Eclipse IDE for Java EE Developers Eclipse IDE for Java EE Developers

for full feature.