Private Access problem

I have created a normal Java class "ConnectionManager" to get the connection and reated public method called getConnection inside this.

I am having a servlet "MatchServlet" in the same directory. I want to call the getConnection method of ConnectionManager. I faced 2 problems

1)I wanted to package, both r in same directory, for both on top i us

package manisha;

But when i used package command I could not create instance of connection manager in my servlet

ConnectionManager cm = new......

This was failed

When i removed the package statement above line is ok. Why it is so ?

2)Now another problem i am facing is i can not call getConnection method

Connection conn = cm.getConnection();

Inside my servlet, I am getting compilation error sayting getConnection() method has "private access". But the method is public and class is also public.

What's going wrong here ?

Thanks in advance,

regards

Manisha

[997 byte] By [manisha_cssa] at [2007-10-1 0:42:25]
# 1
Post the code please... reading this is difficult to analyze.
annie79a at 2007-7-8 0:56:43 > top of Java-index,Security,Event Handling...
# 2

> Post the code please... reading this is difficult to

> analyze.

The problem Annie, is that you do not speak fluent, 'onedoubt-ese'.

Manisha's package statements are likely incorrect.

Also, I'm guessing that she has overriden her getConnection() method.

With out code, I can't be certain but I don't want to see any code.

filestreama at 2007-7-8 0:56:43 > top of Java-index,Security,Event Handling...
# 3

I am copying my codes as it is

ConnectionManager.java - this gets compiled properly

import java.sql.*;

class ConnectionManager {

//implements HttpSessionBindingListener{

private Connection conn;

private Statement stmt;

private String host = "localhost";

private String dbName = "sbcs";

private String port = "1433";

private String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

private String dbURL = "jdbc:microsoft:sqlserver://" +host+":" +port+ ";DatabaseName="+dbName;

private String login = "sbcsusr";

private String password = "sbcspwd";

public ConnectionManager() {}

public Connection getConnection ()

{

if (conn == null ) {

try{

String connectString = dbURL + "," + login + "," + password;

Class.forName(driver);

conn=DriverManager.getConnection(connectString);

stmt=conn.createStatement();

}catch (ClassNotFoundException e){

System.out.println("ConnectionManager: driver unavailable.");

conn = null;

}catch (SQLException e){

System.out.println("ConnectionManager: SQL Exception");

conn = null;

}

}//if

return conn;

}//getConnection

public void closeConnection(){

try{

if ( conn != null || !conn.isClosed()) {conn.close();}

}catch (SQLException e){

System.out.println("problem closing the connection");

}

}//closeConnection

}//ConnectionManager

But below is not getting compiled properly

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.sql.*;

import java.util.*;

public class testServletDB extends HttpServlet {

public void doPost(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

ConnectionManager cm = new ConnectionManager();

Connection conn;

conn = cm.getConnection();

res.setContentType("text/html");

PrintWriter out = res.getWriter();

}//doPost

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

doPost(req, res);

}//doGet

}//testServlet

I get following error when try to compile above program

D:\jakarta-tomcat-5.0.25\webapps\servlets-examples\WEB-INF\classes\testServletDB.java:13: getConnection() has private access in ConnectionManager

conn = cm.getConnection();

^

1 error

Tool completed with exit code 1

--

I hope above helps to clarify my problem. Pls guide me

Thanks and Regards,

Manisha

manisha_cssa at 2007-7-8 0:56:43 > top of Java-index,Security,Event Handling...
# 4
I really don't know why your class doesn't get compiled. I compiled it on my PC and it worked.Try experimenting... all the best.***Annie***
annie79a at 2007-7-8 0:56:43 > top of Java-index,Security,Event Handling...
# 5

Thanks Annie, finally i could get it. It was a path problem and was picking up wrong file.

Now i can create instance into my Servlet but the same thing does not work in JSP.

ConnectionManager cm = new ConnectionManager();

Connection conn;

conn = cm.getConnection();

This i can not use inside JSP file. Whether any special setting is required for JSP?

regards

Manisha

manisha_cssa at 2007-7-8 0:56:43 > top of Java-index,Security,Event Handling...
# 6

> Thanks Annie, finally i could get it. It was a path

> problem and was picking up wrong file.

>

> Now i can create instance into my Servlet but the

> same thing does not work in JSP.

>

> ConnectionManager cm = new ConnectionManager();

> Connection conn;

> conn = cm.getConnection();

>

> This i can not use inside JSP file. Whether any

> special setting is required for JSP?

>

> regards

> Manisha

Where have you placed the ConnectionManager class. You will need to specifically import it in the JSP. You might have circumvented your problem by placing the servlet and class in the same directory. But for JSP, you will need to place it in a proper package (directory) in WEB-INF/classes.

***Annie***

annie79a at 2007-7-8 0:56:43 > top of Java-index,Security,Event Handling...
# 7
> class ConnectionManager {Your COnnectionManager is package private. This means that even though the method is public, the class cannot be accessed from another package.My suggestion: Make it public like you other class is.
Peter-Lawreya at 2007-7-8 0:56:43 > top of Java-index,Security,Event Handling...