简体   繁体   English

Java连接到DB

[英]Java Connection to DB

I am having a slight problem here. 我这里有一个小问题。 basically I want to create a connection pool to DB using a class. 基本上我想使用类创建到DB的连接池。 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) Connection类/(连接池类)

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 这是Execute语句类

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(). 从第二课开始,当我这样做时,我遇到了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. 连接池是一个高级主题,根据您的代码判断我会说暂时让它休息并首先学习一些Java基础知识。 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. extends不是一个好主意。

getProduct should be a method, not a class. getProduct应该是一个方法,而不是一个类。

Your getProduct class is useless as written. 你的getProduct类在编写时没用。 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. 在编写一个适当的JDBC类之前,不要担心池。

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). 有许多不错的选择,其中两个刚刚被Sean(+1)提到。 I'll add my own favourite: BoneCP . 我将添加自己的最爱: BoneCP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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