码迷,mamicode.com
首页 > 数据库 > 详细

java版本的sqlHelper

时间:2015-06-12 18:46:55      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

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

package com.bobo.util;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import com.mysql.jdbc.Connection;
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);
        }
    }

    /**
     * 用于执行语句(insert,update,delete)
     * 
     * @param sqlText
     *            sql语句
     * @param params
     *            参数集合
     * @return int,sql语句受影响的行数
     * @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());
        }
    }

    /**
     * 用于获取结果集语句(eg:selete * from table)
     * 
     * @param cmdtext
     *            sql语句
     * @param parms
     * @return ArrayList 里面的每一个元素是一个object[列数]的数组
     * @throws Exception
     */
    public static ArrayList 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 al = new ArrayList();
            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 name
     *            列名
     * @param parms
     *            OracleParameter[]
     * @return Object 返回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版本的sqlHelper

标签:

原文地址:http://www.cnblogs.com/bobodeboke/p/4572021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!