0

I am having a slight problem here. basically I want to create a connection pool to DB using a class. This pool can be usable by other classes to execute queries. I have made other classes subclasses of the connection class. here is what I have so far.

the Connection class/(connection Pool class)

import java.sql.*;  public class connect extends cPool {
   public static void main(String[] args) {
       cPool en = new cPool(); //crate an object in the name of cPoll calss
       Connection conn = null;
       Object data;
       data = (connect) conn;
       en.readinfo(); //call object then method name
       String userName = "root";
           String password = "" + en.paword + "";// hold outside try catch block to get and set
           String url = "" + en.url + "";

       try
       {

           Class.forName ("com.mysql.jdbc.Driver").newInstance ();
           conn = DriverManager.getConnection (url, userName, password);
           System.out.println ("Database connection established");



       }
       catch (Exception e)
       {
           System.err.println ("Cannot connect to database server");
           System.err.println("Tried connecting using" + url + userName + password +"");

       }
       finally
       {

       }
   }    

}

here is the Execute statement class

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class getProduct extends connect {
    public static void main(String[] args) {
        connect cn = new connect();
        Connection conn = cn.data;

        try {
           Statement stmt = conn.createStatement();
           ResultSet rs = stmt.executeQuery("SELECT * FROM My_Table");
        }
        catch (SQLException ex) {
            Logger.getLogger(getProduct.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {

        }
    }
}

I can't execute any statements. from the 2nd class, when I do I get an error with createStatement(). It says 'Uncompilable source code - cannot find symbol' Thank You very much.

3
  • 1
    So what's the problem? I don't see a question. Commented May 18, 2011 at 9:09
  • I can't execute any statements. from the when I do I get an error with createStatement(). It says 'Uncompilable source code - cannot find symbol' Commented May 18, 2011 at 9:13
  • So your code won't compile - fix it. Commented May 18, 2011 at 9:19

3 Answers 3

1

Connection Pooling is an advanced topic and judging by your code I'd say let it rest for the moment and learn some Java basics first. So maybe you should use an existing solution instead:

Sign up to request clarification or add additional context in comments.

Comments

1

You've got issues.

I wouldn't recommend all this inheritance; extends is not a good idea.

getProduct should be a method, not a class.

Your getProduct class is useless as written. You don't get results out of it. You don't clean up resources. Don't worry about pooling until you can write one proper JDBC class.

Something like this (left some things open for you to figure out):

package persistence;

public class ProductDaoImpl implements ProductDao
{
    private static final String BASE_SELECT = "select * from product ";

    private Connection connection;

    public ProductDaoImpl(Connection connection) { this.connection = connection; }

    public List<Product> find() throws SQLException
    {
        List<Product> products = new ArrayList<Product>();

        Statement st = null;
        ResultSet rs = null;

        try
        {
            st = this.connection.createStatement();
            rs = st.executeQuery(BASE_SELECT);
            while (rs.next())
            {
               Product product = new Product();
               // map columns into product
               products.add(product);
            }
        }
        finally
        {
            DatabaseUtils.close(rs);
            DatabaseUtils.close(st);
        }

        return products;
    }
}

1 Comment

Vote the answer up if it helped; accept it if it's correct. I'd prefer that to your thanks.
0

Not sure what the question is, but i think a sensible answer is "don't write your own". There are many good options, two just mentioned by Sean (+1). I'll add my own favourite: BoneCP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.