my dog learned polymorphism*
The moose likes Beginning Java and the fly likes packages --- Not allowing me to access public class from the same package... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "packages --- Not allowing me to access public class from the same package..." Watch "packages --- Not allowing me to access public class from the same package..." New topic
Author

packages --- Not allowing me to access public class from the same package...

Bhavik Patel
Ranch Hand

Joined: Jul 12, 2004
Posts: 155
i have made one package com.beans and put my two src files DBConnection.java and userInfoBean.java in that package.in the constructor of userInfobean class i m trying to create object of DBConnection class to establish database conncetion.
following error comes
---------- Java Compiler ----------
userInfoBean.java:185: cannot find symbol
symbol : variable DBConnection
location: class com.beans.userInfoBean
Connection tempCon = DBConnection.con;
^
userInfoBean.java:210: password is already defined in chkUser(java.lang.String,java.lang.String)
String password = getPassword();
^
userInfoBean.java:230: cannot find symbol
symbol : class DBConnection
location: class com.beans.userInfoBean
DBConnection db = new DBConnection();
^
userInfoBean.java:230: cannot find symbol
symbol : class DBConnection
location: class com.beans.userInfoBean
DBConnection db = new DBConnection();
^
4 errors
userInfobean.java
package com.beans;
//import com.beans.*;
import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.*; /* Classes available in Classes12.jar file */
import oracle.jdbc.*; /* Classes available in Classes12.jar file */

public class userInfoBean
{


private String sid;
private String sname;
private int year;
private String address;
private String bdate;
private String phone;
private String email;
private String ssn;
private double gpa;
private String semcode;
private String fssn;
private String username;
private String password;




public void setSid(String sid)
{

this.sid=sid;
}

public String getSid()
{
return sid;
}
public void setYear(int year)
{

this.year=year;
}

public int getyear()
{
return year;
}
public void setSname(String sname)
{

this.sname=sname;
}

public String getSname()
{
return sname;
}






public void setAddress(String address)
{


this.address = address;
}
public String getAddress()
{


return address;
}

public void setEmail(String email)
{


this.email = email;
}
public String getEmail()
{


return email;
}public void setPhone(String phone)
{


this.phone = phone;
}
public String getphone()
{


return phone;
}
public void setFssn(String fssn)
{


this.fssn = fssn;
}
public String getFssn()
{


return fssn;
}

public void setBdate(String bdate)
{


this.bdate = bdate;
}
public String getBdate()
{


return bdate;
}
public void setSsn(String ssn)
{


this.ssn = ssn;
}
public String getSsn()
{


return ssn;
}
public void setUser(String username)
{


this.username = username;
}
public String getUser()
{


return username;
}public void setPassword(String password)
{


this.password = password;
}
public String getPassword()
{


return password;
}

public void setGpa(double gpa)
{

this.gpa=gpa;
}

public double getGpa()
{
return gpa;
}public void setSemcode(String semcode)
{

this.semcode=semcode;
}

public String getSemcode()
{
return semcode;
}

public void getStudentInfo(String sid)
{

Connection tempCon = DBConnection.con;
Statement stmt = tempCon.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM STUDENT where SID="+sid);
while(rs.next())
{
setSid(rs.getString("SID"));
setSname(rs.getString("SNAME"));
setYear(rs.getInt("YEAR"));
setAddress(rs.getString("ADDRESS"));
setBdate(rs.getString("BDATE"));
setPhone(rs.getString("PHONE"));
setEmail(rs.getString("EMAIL"));
setSsn(rs.getString("SSN"));
setGpa(rs.getDouble("GPA"));
setFssn(rs.getString("FSSN"));
setUser(rs.getString("USERNAME"));
setPassword(rs.getString("PASSWORD"));
setSemcode(rs.getString("SEMCODE"));
}
}

public boolean chkUser(String sid,String password)
{
getStudentInfo(sid);
String user = getUser();
String password = getPassword();
if(user.equals(sid) && password.equals(password))
{
return true;
}
else
{return false;}


}








public userInfoBean()
{
DBConnection db = new DBConnection();

}



}

----
DBConncetion.java
---
package com.beans;


import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.*; /* Classes available in Classes12.jar file */
import oracle.jdbc.*; /* Classes available in Classes12.jar file */

public class DBConnection
{


static Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String classPath ;


DBConnection()
{


/*classPath = System.getProperty("java.class.path",".");
System.out.println("System Path is "+classPath);*/

try
{

// Load (and therefore register) the Oracle Driver
Class.forName("oracle.jdbc.driver.OracleDriver");

// Get a Connection to the database
//cssid
con = DriverManager.getConnection(
"jdbc racle:thin:@turing.cs.lamar.edu:1521:sidcs", "group07", "sholay"); // Replace group_name and password with your group username and password
// Create a Statement object
stmt = con.createStatement();

// Execute an SQL query, get a ResultSet
rs = stmt.executeQuery("SELECT SID FROM STUDENT");

while(rs.next())
{

System.out.println("Student ID"+"::"+ rs.getString("SID"));
}



}
catch(ClassNotFoundException e)
{
System.out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e)
{
System.out.println("SQLException caught: " + e.getMessage());
System.out.println("stack trace");
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Exception Occurred");
e.printStackTrace();

}
finally
{
// Always close the database connection.

}// end of finally*/
}

public static void closeConnection()
{

try
{
if (con != null) con.close();
}
catch (SQLException ignored) { }

}




}


i am compiling from c:\project\com\beans directory...i don't know why its not allowing me to use the class that is in the same package and declared as public


SCJP 1.4<br />SCWCD 1.4(91%)<br />Working on SCJD -Bodgitt & Scrapper Constructions...<br /> <br />"It takes 43 muscles to frown & 17 to smile but it doen't take any to just sit there with a dumb look on your face .. Keep Smiling "
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Because you have packages, you need to correctly set you classpath. Since you are compiling from the com/beans directory, when it tries to import your DBConnection class, it is trying to look in the com/beans/com/beans directory. It will try to take the directory you are compiling in and add the package directories, which obviously doesn't exist.

You need to set your classpath to c:\project, and compile from that directory like

javac com.beans.userInfoBean.

You should also change this classes name to UserInfoBean, capitaliz the U in User.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

In addtion to Mark's Post
Please check the signature of method chkUser,
public boolean chkUser(String sid,String password)
It has String password in aguement and same time you are declaring a varibale with same name inside the method.

Shailesh
[ April 06, 2005: Message edited by: Shailesh Chandra ]

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: packages --- Not allowing me to access public class from the same package...
 
Similar Threads
Problem with the query in JDBC ,resultset empty
Session Scope JSF
what's wrong
accessing one class from other
SQLException : not all variables bound