Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Inheritance,connection object.

Inheritance,connection object.

Scheduled Pinned Locked Moved Java
questionoop
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chdboy
    wrote on last edited by
    #1

    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?

    }
    }

    L L 2 Replies Last reply
    0
    • C chdboy

      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?

      }
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C chdboy

        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?

        }
        }

        L Offline
        L Offline
        linaast
        wrote on last edited by
        #3

        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.
        }
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups