简体   繁体   中英

Java Connection to DB

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.

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:

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM