一个简单的jdbc连接封装

不说废话直接上程序

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" };

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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值