This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Instatiating java class inside a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instatiating java class inside a servlet" Watch "Instatiating java class inside a servlet" New topic
Author

Instatiating java class inside a servlet

carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

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.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

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.
carina caoor
Ranch Hand

Joined: Jun 23, 2007
Posts: 300

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
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3456
    
  12
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;


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38424
    
  23
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Instatiating java class inside a servlet