我的jdbc帮助类(SqlHelper)

package com.jxr.database;

import java.sql.*;
import java.io.FileInputStream;
import java.util.Calendar;

/**
 * User: [email protected]
 * Date: 2009-11-20
 * Time: 20:03:45
 * To change
 */
public abstract class SqlHelper {

    private Connection con;
    private Statement stmt;
    private DataType type;


    public SqlHelper(String connectionString, DataType type, String userName, String passWord) throws SQLException,
            ClassNotFoundException, IllegalAccessException, InstantiationException {
        setDataType(type);
        switch (getDataType()) {
            case Acess:
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
                break;
            case MySql:
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                break;
            case SQL:
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
                break;
            case Orace:
                Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
                break;
        }
        con = DriverManager.getConnection(connectionString, userName, passWord);
    }

    public SqlHelper(String connectionString, DataType type) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        this(connectionString, type, "", "");
    }

    public SqlHelper(String connectionString) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        this(connectionString, DataType.Acess, "", "");
    }


    protected SqlHelper(Connection con) {
        this.con = con;
    }

    protected ResultSet ExecuteSql(String sql) throws SQLException {
        stmt = GetStatement();
        return stmt.executeQuery(sql);
    }

    protected Statement GetStatement() throws SQLException {
        return con.createStatement(/*ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE */);
    }

    /*
    * 执行存储过程
    */
    public ResultSet ExecuteProcedure(String sql, SqlParamater... pars) throws SQLException {
        CallableStatement stmt = con.prepareCall(sql);
        for (SqlParamater sp : pars) {
            switch (sp.getType()) {
                case INTEGER: {
                    switch (sp.getInputType()) {
                        case OutPut:
                            stmt.registerOutParameter(sp.getName(), Types.INTEGER);
                            break;
                        default:
                            stmt.setInt(sp.getName(), Integer.parseInt(sp.getValue().toString()));
                            break;
                    }
                    break;
                }
                case VARCHAR: {
                    switch (sp.getInputType()) {
                        case OutPut:
                            stmt.registerOutParameter(sp.getName(), Types.VARCHAR);
                            break;
                        default:
                            stmt.setString(sp.getName(), String.valueOf(sp.getValue()));
                            break;
                    }
                    break;
                }
                case BOOLEAN: {
                    switch (sp.getInputType()) {
                        case OutPut:
                            stmt.registerOutParameter(sp.getName(), Types.BOOLEAN);
                            break;
                        default:
                            stmt.setBoolean(sp.getName(), Boolean.valueOf(sp.getValue().toString()));
                            break;
                    }
                    break;
                }
                case Float: {
                    switch (sp.getInputType()) {
                        case OutPut:
                            stmt.registerOutParameter(sp.getName(), Types.FLOAT);
                            break;
                        default:
                            stmt.setFloat(sp.getName(), Float.valueOf(sp.getValue().toString()));
                            break;
                    }
                    break;
                }
                case DOUBLE: {
                    switch (sp.getInputType()) {
                        case OutPut:
                            stmt.registerOutParameter(sp.getName(), Types.DOUBLE);
                            break;
                        default:
                            stmt.setDouble(sp.getName(), Double.valueOf(sp.getValue().toString()));
                            break;
                    }
                    break;
                }
                case BLOB: {
                    FileInputStream fin = (FileInputStream) sp.getValue();
                    stmt.setBlob(sp.getName(), fin, sp.getLength());
                    break;
                }

                case BIT: {
                    switch (sp.getInputType()) {
                        case OutPut:
                            stmt.registerOutParameter(sp.getName(), Types.BIT);
                            break;
                        default:
                            stmt.setByte(sp.getName(), Byte.valueOf(sp.getValue().toString()));
                            break;
                    }
                    break;
                }
                case DATE: {
                    switch (sp.getInputType()) {
                        case OutPut:
                            stmt.registerOutParameter(sp.getName(), Types.DATE);
                            break;
                        default:
                            stmt.setDate(sp.getName(), Date.valueOf(sp.getValue().toString()));
                            break;
                    }
                    break;
                }
                case BINARY:
                    break;
                case CLOB:
                    break;
                case DECIMAL:
                    break;
                case OutPut:
                    break;
                case SQLXML:
                    break;
                case TIME:
                    break;
                case TINYINT:
                    break;
                case VARBINARY:
                    break;
            }
        }
        return stmt.executeQuery();
    }

    protected boolean ExecuteQuery(String sql) throws SQLException {

        return GetStatement().execute(sql);
    }

    private void setDataType(DataType type) {
        this.type = type;
    }

    private DataType getDataType() {
        if (this.type == null)
            return DataType.Acess;
        else
            return this.type;
    }

    public enum CommandType {
        Transport,
        Text
    }

    public enum DataType {
        SQL,
        Acess,
        MySql,
        Orace,
        IBM_DB2
    }

    /*
    * 必须调用释放资源
    */
    protected void dispose() {
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        if (con != null)
            try {
                if (!con.isClosed())
                    try {
                        con.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }


}

你可能感兴趣的:(oracle,sql,mysql,jdbc,db2)