• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instatiating java class inside a servlet

 
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic