Inheritance,connection object.
-
public class someclass{
public void savedata()
{
Connection con//I want to use con object in another class
//con has the connection properties
}
public void usecon()
{
//I want to use con in this class
//I have tried
savedata sd = new savedata();//it says savedata cannot be //resolved to a type,how do I use the variable con?}
} -
public class someclass{
public void savedata()
{
Connection con//I want to use con object in another class
//con has the connection properties
}
public void usecon()
{
//I want to use con in this class
//I have tried
savedata sd = new savedata();//it says savedata cannot be //resolved to a type,how do I use the variable con?}
}Why are you trying to create an object with the name of one of your class's member functions? If
con
is a member variable of the class then you need to initialise it somewhere and then you just use it by its name in any of the other functions. Maybe you should go back and read up on classes and their members.Veni, vidi, abiit domum
-
public class someclass{
public void savedata()
{
Connection con//I want to use con object in another class
//con has the connection properties
}
public void usecon()
{
//I want to use con in this class
//I have tried
savedata sd = new savedata();//it says savedata cannot be //resolved to a type,how do I use the variable con?}
}public class ConClass
{
public Connection con = null;// constructor to initialize con.
public ConClass(DataSource ds){
try{
con = ds.getConnection();
} catch(SqlException ex){
System.out.println(ex.getMessage());
}
}
// the con to get.
public Connection getCon(){
return con;
}
}public class SaveData
{
private Connection con = null;public void save(ConClass cObj){
if(con == null){
con = cObj.getCon();
}
// do whatever and use whenever you want your con in this class.
}
}