java常用sql处理函数类

java常用sql处理函数类

使用jdbc操作

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;


//封装了数据库的增删改查操作

public class Sqlutil {
    /**
     * 连接数据库 
     * @param url
     * @param user
     * @param passwd
     * @return
     */
    public static Connection connect(String url, String user, String passwd) {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url, user, passwd);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

/**
 *   查一条
 * @param conn
 * @param sql
 * @return
 */
    public static HashMap<String, String> fetchOne(Connection conn, String sql) {
        Statement stat = null;
        ResultSet res = null;
        HashMap<String, String> map = new HashMap<>();
        try {
            stat = conn.createStatement();
            res = stat.executeQuery(sql);
            int col = res.getMetaData().getColumnCount();
            res.next();
            for (int i = 1; i < col; i++) {
                map.put(res.getMetaData().getColumnName(i), res.getString(i));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }

    /**
     *   查多条
     * @param conn
     * @param sql
     * @return
     */
    public static HashMap<Integer, HashMap<String, String>> fetchAll(Connection conn, String sql) {
        Statement stat = null;
        ResultSet res = null;
        HashMap<Integer,HashMap<String, String>> map = new HashMap<>();
        try {
            stat = conn.createStatement();
            res = stat.executeQuery(sql);
            int col = res.getMetaData().getColumnCount();
            int j = 0;
            while (res.next()) {
                HashMap<String, String> tmap = new HashMap<>();
                for (int i = 1; i < col; i++) {
                    tmap.put(res.getMetaData().getColumnName(i), res.getString(i));
                }
                map.put(j, tmap);
                j++;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }

    /**
     *    增删改  
     * @param conn 
     * @param sql
     * @return
     */
    public static int update(Connection conn, String sql) {
        Statement stat;
        int res = 0;
        try {
            stat = conn.createStatement();
            res = stat.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return res;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值