Forums Register Login

Instatiating java class inside a servlet

+Pie Number of slices to send: Send
hi i want to connect to the database therefore i wrote a database class
package PlotManagement;

import java.sql.*;




public class DBConnection {


public Connection connection =null;

public Connection connect()
{


try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String connectionUrl = "jdbc:jtds:sqlserver://DCSRV02:1433/REGISTRATION_TEMP_LTR_PRINT";
connection = DriverManager.getConnection(connectionUrl,"sa","water");
System.out.println("Connected" );
}
catch (Exception e)
{
System.out.println( "<h1>exception: "+e+e.getMessage()+"</h1>" );
e.printStackTrace();
}

return connection;
}

}


and i am trying to use connection variable in my servlet

package PlotManagement;
import java.io.*;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.*;
import javax.servlet.http.*;

public class GetAreacode extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title> Films Example: Servlet, MSSQL version</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");

try {
DBConnection d= new DBConnection();

Statement stmt = d.connection.createStatement();
ResultSet rs;
rs = stmt.executeQuery("select areacode from areacodemaster");
out.println( "<table>" );
while ( rs.next() )
{
String areacode = rs.getString(1);
out.println("<tr><td>"+areacode+"</td></tr>");
}
out.println( "</table>" );
d.connection.close();

}
catch (Exception e)
{
out.println( "<h1>exception: "+e+e.getMessage()+"</h1>" );
e.printStackTrace();
}
out.println("</body>");
out.println("</html>");



}
}


But i am getting the following errors
GetAreacode.java:23: cannot find symbol
symbol : class DBConnection
location: class PlotManagement.GetAreacode
DBConnection d= new DBConnection();
^
GetAreacode.java:23: cannot find symbol
symbol : class DBConnection
location: class PlotManagement.GetAreacode
DBConnection d= new DBConnection();
^
2 errors

can any 1 suggest me whats wrong here.
+Pie Number of slices to send: Send
How are you compiling this code ? The problem seems to be complier not able to find the DBConnection class !!

goto PlotManagement directory and

compile as 'javac -classpath ".;servlet.jar" GetAreacode.java
+Pie Number of slices to send: Send
in my classpath all the jar files are added
My classpath is:--
.;C:\Java\jdk1.5.0_11\bin;C:\Java\jdk1.5.0_11\lib;C:\Tomcat\common\lib\servlet-api.jar;C:\Tomcat\common\lib\jsp-api.jar;C:\Tomcat\webapps\MyPlots\src\PlotManagement;C:\Tomcat\common\lib\jtds-1.2.2.jar;

Still it is showing the same error.
+Pie Number of slices to send: Send
though they are in the same package the servlet is not able to find the dbconnection.java class...
after including in the classpath also its the same error.
can any 1 suggest whats wrong here
+Pie Number of slices to send: Send
 

Originally posted by ruquia tabassum:
in my classpath all the jar files are added
My classpath is:--
.;C:\Java\jdk1.5.0_11\bin;C:\Java\jdk1.5.0_11\lib;C:\Tomcat\common\lib\servlet-api.jar;C:\Tomcat\common\lib\jsp-api.jar;C:\Tomcat\webapps\MyPlots\src\PlotManagement;C:\Tomcat\common\lib\jtds-1.2.2.jar;

Still it is showing the same error.



PlotManagement is part of the package name, so your classpath should actually be
.;C:\Java\jdk1.5.0_11\bin;C:\Java\jdk1.5.0_11\lib;C:\Tomcat\common\lib\servlet-api.jar;C:\Tomcat\common\lib\jsp-api.jar;C:\Tomcat\webapps\MyPlots\src;C:\Tomcat\common\lib\jtds-1.2.2.jar;
+Pie Number of slices to send: Send
 

Originally posted by ruquia tabassum:
can any 1 suggest whats wrong here

Joanne has explained it, but some people here didn't grow up speaking English, and may use automatic translators; writing "any 1" may confuse them. Please look at this FAQ.
I just had the craziest dream. This tiny ad was in it.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1126 times.
Similar Threads
Properties File Error java.util.Resource Bundle. throw Missing Resource Exception
Passing parameter to servlet from another servlet
Servlet becomming slow
can't define a variable
Servlet becomming slow
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:34:26.