请大家推荐一个通用的数据库(MySQL、SQLServer、Oracle)操作类,Hibernate好象太复杂了,并且把数据库变的不象数据库了。在一些产品性的开发应用中,觉得Hibernate太过复杂,不够灵活,并且不标准化,而是采用了它自己定义的HSQL,这对于产品的扩展和维护应该是不利的。由于MySQL、SQLServer、Oracle在操作上会有一些不一样,比如分页机制等,是否能够做到开发上的通用?还是需要分别开发三种数据库版本的产品。
解决方案 »
- --tomcat和apache整合之后的问题
- java如何得到c函数的返回值
- 从数据库中读出的新闻内容,怎么显示在fck上?
- 求一个定时启动的Tomcat寄存方式
- jstl问题,JSP一直不能执行
- 刚接触Eclipse+myEclipse,问一些简单的问题????
- 如何用js去掉文本输入区域(TextArea)中的回车!!在线等,急!!
- SOS!!!关于JBuilder的使用,谢谢!
- 一困惑的问题:在线等?????
- 用过jspsmartupload的请进!!
- 有没有用过花生壳+tomcat的,请教一下
- 连库时出现这个错误:JDBC SQLSERVER"Error establishing socket"
sql都有点不同的。
帮忙up。
1、定义接口:ISqlHelper
2、定义不同数据库的ISqlHelper的实现,如:MsSqlSqlHelper,OracleSqlHelper
3、定义工厂,返回ISqlHelper的实现;参考思路,恕不提供代码。
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);
}
}
}
================
哥们太豆了
绝对简单
=========================
用过一两次,就是写好sql就行,然后他自己好像封装了数据库的连接等操作.楼主可以试试
就是xml.
其实就是工厂模式,接口加反射机制通用的效率肯定不好
支持cemoi(揭谛揭谛,波罗揭谛,波罗僧揭谛,菩提萨婆诃)
如果不用Hibernate,使用Oracle自己提供的TopLInk
或者干脆用EJB3.0。