一个简单的jdbc连接封装

来源:互联网 发布:编程用什么cpu 编辑:程序博客网 时间:2024/05/16 16:06

不说废话直接上程序

JDBC

----------------------------------------------------------------------------------------------

package com.ebay.api;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class OracleJDBC {
private static final String URL = "jdbc:oracle:thin:@192.168.2.20:1521:yinyuan";
    private static final String USER = "eshop";
    private static final String PASSWORD = "eshop";
 
    static {
        try {
            Class.forName("oracle.jdbc.OracleDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("加载Oracle数据库驱动失败!");
        }
    }
 
    /**
     * 获取Connection
     * 
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static Connection getConnection() throws SQLException {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            System.out.println("获取数据库连接失败!");
            throw e;
        }
        return conn;
    }
     
    /**
     * 关闭ResultSet
     * @param rs
     */
    public static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    }
     
    /**
     * 关闭Statement
     * @param stmt
     */
    public static void closeStatement(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            }       
            catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
     
    /**
     * 关闭ResultSet、Statement
     * @param rs
     * @param stmt
     */
    public static void closeStatement(ResultSet rs, Statement stmt) {
        closeResultSet(rs);
        closeStatement(stmt);
    }
     
    /**
     * 关闭PreparedStatement
     * @param pstmt
     * @throws SQLException
     */
    public static void fastcloseStmt(PreparedStatement pstmt) throws SQLException
    {
        pstmt.close();
    }
     
    /**
     * 关闭ResultSet、PreparedStatement
     * @param rs
     * @param pstmt
     * @throws SQLException
     */
    public static void fastcloseStmt(ResultSet rs, PreparedStatement pstmt) throws SQLException
    {
        rs.close();
        pstmt.close();
    }
     
    /**
     * 关闭ResultSet、Statement、Connection
     * @param rs
     * @param stmt
     * @param con
     */
    public static void closeConnection(ResultSet rs, Statement stmt, Connection con) {
        closeResultSet(rs);
        closeStatement(stmt);
        closeConnection(con);
    }
     
    /**
     * 关闭Statement、Connection
     * @param stmt
     * @param con
     */
    public static void closeConnection(Statement stmt, Connection con) {
        closeStatement(stmt);
        closeConnection(con);
    }
     
    /**
     * 关闭Connection
     * @param con
     */
    public static void closeConnection(Connection con) {
        if (con != null) {
            try {
               con.close();
            }
            catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }


}

-----------------------------------------------------------------------------------------------------------------------




然后调取用的一个封装,返回值是一个List<HashMap<String,Object>>    参数是SQL语句和String[]数组

-----------------------------------------------------------------------------------------------------------------------

public static List<HashMap<String, String>> getObjectMap(String sql,
String[] strings) throws SQLException {
OracleJDBC jdbc = new OracleJDBC();
Connection connection = jdbc.getConnection();
Statement stm = connection.createStatement();
ResultSet rs = stm.executeQuery(sql);
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
while (rs.next()) {
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < strings.length; i++) {
String value="";
if(rs.getString(strings[i])==null||"".equals(rs.getString(strings[i]))){
value="";
}
else{
value=rs.getString(strings[i]).toString();
}

map.put(strings[i],value);
}
list.add(map);
}
return list;


}


参数列子

String sql = "select * from db_skuandtemplet";
String[] s = { "SKU", "HTML1", "HTML2", "HTML3", "HTML4" };

其中数组种是查询出来的数据的名字

-------------------------------------------------------------------

原创粉丝点击