JSP

Eclipse Not Working Only Flickering

If you are finding issue somethig like this,then first of all just go to your command prompt under username and check java version there
by simply typing:-

java -version

If you can see java version there then simply checking below settings:-

JAVA Installation:

Installed JDK at C:\Program Files\Java\jdk1.7.0_10
Installed JRE at C:\Program Files\Java\jre7

Environment Variables Configuration:

JAVA_HOME = C:\Program Files\Java\jdk1.7.0_10
PATH = C:\Program Files\Java\jdk1.7.0_10\bin;

But if you are unable to see java version there and some error like:-

“Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object”

Then just go to :-
C:\Program Files\Java\jdk1.7.0_10\jre\lib

and check that “rt.jar” is there.If this is not there then uninstall java from your machine and install java again.

Hope fully it will help someone.

keep coding………….

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