请大家推荐一个通用的数据库(MySQL、SQLServer、Oracle)操作类,Hibernate好象太复杂了,并且把数据库变的不象数据库了。在一些产品性的开发应用中,觉得Hibernate太过复杂,不够灵活,并且不标准化,而是采用了它自己定义的HSQL,这对于产品的扩展和维护应该是不利的。由于MySQL、SQLServer、Oracle在操作上会有一些不一样,比如分页机制等,是否能够做到开发上的通用?还是需要分别开发三种数据库版本的产品。

解决方案 »

  1.   

    有点难,各个数据库都有点不同的。
    sql都有点不同的。
    帮忙up。
      

  2.   

    使用设计模式中的工厂模式,自己定义通用数据库操作类。
    1、定义接口:ISqlHelper
    2、定义不同数据库的ISqlHelper的实现,如:MsSqlSqlHelper,OracleSqlHelper
    3、定义工厂,返回ISqlHelper的实现;参考思路,恕不提供代码。
      

  3.   

    使用Java中的反射来实现自己的通用的Dao啊
      

  4.   

    我写了一个 不嫌烂的话 拿去就是:
    package com.gogou.common;import java.util.*;
    import java.sql.*;
    import javax.servlet.jsp.jstl.sql.*;/**
     * This class is a bean for executing SQL statements. It has three
     * properties that can be set: connection, sqlValue and values.
     * The connection and sqlValue properties must always be set before
     * calling one of the execute methods. If the values property is
     * set, the sqlValue property must be an SQL statement with question
     * s as placeholders for the value objects in the values
     * property.
     *
     * erdong
     * @version 2.0
     */
    public class SQLCommandBean {
        private Connection conn;
        private String sqlValue;
        private List values;    public SQLCommandBean() {
            try {
                String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
                String sConnStr =
                        "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=GoGou";
                Class.forName(sDBDriver);
                this.conn = DriverManager.getConnection(sConnStr, "sa", "");
            } catch (ClassNotFoundException ex) {
                System.out.println("at SQLCommandBean ClassNotFoundException");
            } catch (SQLException ex) {
                System.out.println("at SQLCommandBean SQLException");
            }
        }    /**
         * Sets the Connection to use.
         */
        public void setConnection(Connection conn) {
            this.conn = conn;
        }    /**
         * @param sqlValue String
         */
        public void setCon() throws ClassNotFoundException, SQLException {
            String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
            String sConnStr =
                    "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=GoGou";
            Class.forName(sDBDriver);
            this.conn = DriverManager.getConnection(sConnStr, "sa", "");
        }    /**
         * Set the SQL string, possibly with question  placeholders for
         * values set by setValues().
         */
        public void setSqlValue(String sqlValue) {
            this.sqlValue = sqlValue;
        }    /**
         * Sets the values to use for the place holders in the SQL
         * string. The List must contain one Object for each place holder,
         * suitable for use with the PreparedStatement setObject() method.
         */
        public void setValues(List values) {
            this.values = values;
        }    /**
         * Executes the specified SQL string as a query and returns
         * a Result object
         *
         * @return a javax.servlet.jsp.jstl.sql.Result object
         * @exception SQLException
         */
        public Result executeQuery() throws SQLException {
            Result result = null;
            ResultSet rs = null;
            PreparedStatement pstmt = null;
            Statement stmt = null;
            try {
                if (values != null && values.size() > 0) {
                    // Use a PreparedStatement and set all values
                    pstmt = conn.prepareStatement(sqlValue);
                    setValues(pstmt, values);
                    rs = pstmt.executeQuery();
                } else {
                    // Use a regular Statement
                    stmt = conn.createStatement();
                    rs = stmt.executeQuery(sqlValue);
                }
                result = ResultSupport.toResult(rs);
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {}
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException e) {}
                }
                if (pstmt != null) {
                    try {
                        pstmt.close();
                    } catch (SQLException e) {}
                }
            }
            return result;
        }    /**
         * Executes the specified SQL string (any statement except SELECT, such
         * as UPDATE, INSERT, DELETE or CREATE TABLE) and returns
         * the number of rows affected by the statement, or 0 if none.
         *
         * @return the number of rows affected
         * @exception SQLException
         */
        public int executeUpdate() throws SQLException {
            int noOfRows = 0;
            ResultSet rs = null;
            PreparedStatement pstmt = null;
            Statement stmt = null;
            try {
                if (values != null && values.size() > 0) {
                    // Use a PreparedStatement and set all values
                    pstmt = conn.prepareStatement(sqlValue);
                    setValues(pstmt, values);
                    noOfRows = pstmt.executeUpdate();
                } else {
                    // Use a regular Statement
                    stmt = conn.createStatement();
                    noOfRows = stmt.executeUpdate(sqlValue);
                }
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {}
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException e) {}
                }
                if (pstmt != null) {
                    try {
                        pstmt.close();
                    } catch (SQLException e) {}
                }
            }
            return noOfRows;
        }    /**
         * Calls setObject() method on the PreparedStatement for all
         * objects in the values List.
         *
         * @param pstmt the PreparedStatement
         * @param values a List with objects
         * @exception SQLException
         */
        private void setValues(PreparedStatement pstmt, List values) throws
                SQLException {
            for (int i = 0; i < values.size(); i++) {
                Object v = values.get(i);
                // Set the value using the method corresponding to the type.
                // Note! Set methods are indexed from 1, so we add 1 to i
                pstmt.setObject(i + 1, v);
            }
        }
    }
      

  5.   

    Hibernate在你数据库的移植时候的优势有体现啊,把数据库底层的区别过滤了嘛
      

  6.   

    推荐使用ibatis,也是开源的,用起来比较的简单
      

  7.   

    to  edsoft() 
    ================
    哥们太豆了
      

  8.   

    如果在公司都有自己的类库,如果自己写的话hibernate比较好
      

  9.   

    iBatis
    绝对简单
    =========================
    用过一两次,就是写好sql就行,然后他自己好像封装了数据库的连接等操作.楼主可以试试
    就是xml.
      

  10.   

    微软的petshop就是就可以对付oracle和mssql啊
    其实就是工厂模式,接口加反射机制通用的效率肯定不好
      

  11.   

    ,
    支持cemoi(揭谛揭谛,波罗揭谛,波罗僧揭谛,菩提萨婆诃)
      

  12.   

    iBatis感觉还是不如Hibernete专业。
    如果不用Hibernate,使用Oracle自己提供的TopLInk
    或者干脆用EJB3.0。
      

  13.   

    不是hibernate难,数据库本身就难导致hibernate的复杂