java版本的sqlHelper

以下版本的sqlHelper可以支持普通的DDL,DML和查询语句,暂不支持存储过程,事务等

备注:其实将sqlHelper中的方法定义为静态方法,并不是一种很好的解决方法,因此在用户多的时候,很容易造成用户排队。改进的方向:

1)定义为普通类,每一次使用的时候,都新建一个sqlHelper,或者,定义为单例类会好一些?

2)动用高级的数据库特性,如连接池等,这个以后可能的话,可以好好研究下

 

package com.bobo.utils;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap; 
import java.sql.*;

public class SqlHelper {
    
    private static String driverName = "com.mysql.jdbc.Driver";
    private static String urlName = "jdbc:mysql://127.0.0.1:3306/bobo_test";
    private static String user = "root";
    private static String password = "bobo";

    // 防止内外部初始化
    private SqlHelper() {

    }

    private static java.sql.Connection getConnection() {
        try {
            Class.forName(driverName);
            return DriverManager.getConnection(urlName, user, password);
        } catch (Exception e) {
            // TODO Auto-generated catch block

            return null;
        }

    }

    private static void prepareCommand(PreparedStatement pstmt, String[] parms) {
        try {
            if (parms != null) {
                for (int i = 0; i < parms.length; i++) {
                    try {
                        pstmt.setDate(i + 1, java.sql.Date.valueOf(parms[i]));
                    } catch (Exception e) {
                        try {
                            pstmt.setDouble(i + 1, Double.parseDouble(parms[i]));
                        } catch (Exception e1) {
                            try {
                                pstmt.setInt(i + 1, Integer.parseInt(parms[i]));
                            } catch (Exception e2) {
                                try {
                                    pstmt.setString(i + 1, parms[i]);
                                } catch (Exception e3) {
                                    System.out
                                            .print("SQLHelper-PrepareCommand Err1:"
                                                    + e3);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e1) {
            System.out.print("SQLHelper-PrepareCommand Err2:" + e1);
        }
    }

    /**
     * 执行非查询sql语句(insert,update,delete)
     * 
     * @param sqlText
     *            sql命令
     * @param params
     *            参数值
     * @return int 返回操作影响的记录条数
     * @throws Exception
     */
    public static int ExecuteNonQuery(String sqlText, String[] params)
            throws Exception {
        PreparedStatement ps = null;
        java.sql.Connection con = null;
        try {
            con = getConnection();
            ps = con.prepareStatement(sqlText);
            prepareCommand(ps, params);
            return ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception("executeNonQuery出错:" + e.getMessage());
        }
    }

    /**
     * 执行查询语句,返回记录内容
     * 
     * @param cmdtext
     *            sql指令
     * @param parms 参数
     * @return ArrayList 返回一个list,里面是object[列数]对象
     * @throws Exception
     */
    public static ArrayList<Object[]> ExecuteReader(String cmdtext, String[] parms)
            throws Exception {
        PreparedStatement pstmt = null;
        java.sql.Connection conn = null;

        try {
            conn = getConnection();

            pstmt = conn.prepareStatement(cmdtext);

            prepareCommand(pstmt, parms);
            ResultSet rs = pstmt.executeQuery();

            ArrayList<Object[]> al = new ArrayList<Object[]>();
            ResultSetMetaData rsmd = rs.getMetaData();
            int column = rsmd.getColumnCount();

            while (rs.next()) {
                Object[] ob = new Object[column];
                for (int i = 1; i <= column; i++) {
                    ob[i - 1] = rs.getObject(i);
                }
                al.add(ob);
            }

            rs.close();
            return al;

        } catch (Exception e) {
            throw new Exception("executeSqlResultSet出错:" + e.getMessage());
        } finally {
            try {
                if (pstmt != null)
                    pstmt.close();
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                throw new Exception("executeSqlResultSet出错:" + e.getMessage());
            }
        }
    }
    /**
     * 执行查询语句,返回符合条件的记录数目
     * 
     * @param cmdtext
     *            sql指令
     * @param parms 参数
     * @return int 返回符合条件的记录数目,如果没有返回-1
     * @throws Exception
     */
    public static int ExecuteRowCountQuery(String cmdtext, String[] parms)
            throws Exception {
        PreparedStatement pstmt = null;
        java.sql.Connection conn = null;
        int result=-1;
        try {
            conn = getConnection();

            pstmt = conn.prepareStatement(cmdtext);

            prepareCommand(pstmt, parms);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
             result= rs.getInt(1);
             rs.close();         
             

        } catch (Exception e) {             
            throw new Exception("executeSqlResultSet出错:" + e.getMessage());
        } finally {
            try {
                if (pstmt != null)
                    pstmt.close();
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                throw new Exception("executeSqlResultSet出错:" + e.getMessage());
            }
        }
        return result;
    }
    
    /**
     * 执行单结果单列查询语句,如果记录存在,返回首条记录的对应列,否则返回空(按照列名查询)
     * 
     * @param cmdtext
     *            SQL命令
     * @param name
     *          列名称  
     * @param parms
     *            OracleParameter[]
     * @return Object 返回列对象
     * @throws Exception
     */
    public static Object ExecuteScalar(String cmdtext, String name,
            String[] parms) throws Exception {
        PreparedStatement pstmt = null;
        java.sql.Connection conn = null;
        ResultSet rs = null;

        try {
            conn = getConnection();

            pstmt = conn.prepareStatement(cmdtext);
            prepareCommand(pstmt, parms);

            rs = pstmt.executeQuery();
            if (rs.next()) {
                return rs.getObject(name);
            } else {
                return null;
            }
        } catch (Exception e) {
            throw new Exception("executeSqlObject出错:" + e.getMessage());
        } finally {
            try {
                if (rs != null)                    
                    rs.close();
                if (pstmt != null)
                    pstmt.close();
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                throw new Exception("executeSqlObject出错:" + e.getMessage());
            }
        }
    }

    /**
     *执行单结果单列查询语句,如果记录存在,返回首条记录的对应列,否则返回空(按照列索引查询)
     * 
     * @param cmdtext
     *            SQL命令
     * @param index
     *            第几列
     * @param parms
     *            OracleParameter[]
     * @return Object
     * @throws Exception
     */
    public static Object ExecuteScalar(String cmdtext, int index, String[] parms)
            throws Exception {
        PreparedStatement pstmt = null;
        java.sql.Connection conn = null;
        ResultSet rs = null;

        try {
            conn = getConnection();

            pstmt = conn.prepareStatement(cmdtext);
            prepareCommand(pstmt, parms);

            rs = pstmt.executeQuery();
            if (rs.next()) {
                return rs.getObject(index);
            } else {
                return null;
            }
        } catch (Exception e) {
            throw new Exception("executeSqlObject出错:" + e.getMessage());
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (pstmt != null)
                    pstmt.close();
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                throw new Exception("executeSqlObject出错:" + e.getMessage());
            }
        }
    }
    
 

}

 

你可能感兴趣的:(java)