hi,
i am using one
servlet program (simple
java program) in which,i am calling another java program which is simple connection to mysql database.
i am new to this and please be patient to my problem..thanks in advance...
i am unable to connect to the database..the reason for it i could not understand.i set classpaths and everthing correctly.
my code is as below:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Java Servlet done by L p Mahesh</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello,My name is lanka poojya mahesh</H1>\n" +
"</BODY></HTML>");
Exp ex = new Exp();
out.println("1");
ex.print();
out.println("2");
}//end of doGet method
}//end of helloservlet
and Exp.java program is below:
import java.io.*;
import java.sql.*;
import java.sql.*;
import java.util.*;
public class Exp
{
public Exp()
{
System.out.println("test");
Connection conn = null;
try
{
System.out.println("001");
String userName = "root";
String password = "admin";
System.out.println("012");
String url = "jdbc:mysql://localhost:3306/mysql";
System.out.println("123");
Class.forName ("org.gjt.mm.mysql.Driver");
System.out.println("234");
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}//end of finally
} // nd of Exp()
public int print()
{
System.out.println("inside this");
return 2;
}
}
upto this line "System.out.println("123");"
prog is ruunig correctly ..after that it is coming out pf try loop and stmt in catch is executed.
please solve my doubt......i would really apreciate that..thanks in advance...