栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDBCUtils附加增删改查功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDBCUtils附加增删改查功能

  1. db.properties连接
    driver = com.mysql.jdbc.Driver
    url = jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
    user = root
    password = 123456
  2. 工具类连接数据库                                                                                                                        JDBCUtils:
    package cn.pzhu.util;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.*;
    
    public class JDBCUtils {
        private static String driver = null;
        private static String url = null;
        private static String user = null;
        private static String password = null;
        private Connection con;
        private PreparedStatement preparedStatement;
        private ResultSet resultSet;
        //1.注册驱动
        static{
            InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            try {
                properties.load(in);
                driver = properties.getProperty("driver");
                url = properties.getProperty("url");
                user = properties.getProperty("user");
                password = properties.getProperty("password");
                try {
                    Class.forName(driver);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       public JDBCUtils(){
           try {
               con=DriverManager.getConnection(url, user, password);
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
        //2.获得连接
        public  Connection getConnection(){
            return con;
        }
    
        //3.释放资源
        public static void close(Connection connection, Statement statement, ResultSet result){
            if(result!=null){
                try {
                    result.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }           if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    
        
        public boolean updateSql(String sql, List params) throws SQLException{
            boolean flag=false;
            int result=-1;
            int index=1;
            preparedStatement=con.prepareStatement(sql);
            //空参数判断
            if(params!=null&&!params.isEmpty()) {
                //params遍历
                for (int i = 0; i < params.size(); i++) {
                    //预编译 填充参数
                    preparedStatement.setObject(index++, params.get(i));
    
                }
            }
    
            result=preparedStatement.executeUpdate();
            //执行成功
            if(result>0){
                flag=true;
            }
            return flag;
        }
    
        
        public Map findSimpleResult(String sql, List params) throws SQLException{
           Map map=new HashMap();
           int index=1;//setObject()都是1开始
           preparedStatement=con.prepareStatement(sql);
           //空参数判断
           if(params!=null&&!params.isEmpty()){
               for (int i=0;i> findMoreResult(String sql, List params) throws SQLException{
            List> list=new ArrayList>();
            int index=1;//setObject()都是1开始
            preparedStatement=con.prepareStatement(sql);
                for (int i=0;i map=new HashMap();
                    for(int i=0;i 

    3.测试类测试

    package cn.pzhu.test;
    
    import cn.pzhu.util.JDBCUtil;
    import cn.pzhu.util.JDBCUtils;
    import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Test_7 {
        public static void main(String[] args) throws SQLException {
            JDBCUtils jdbcUtils=new JDBCUtils();
             Connection connection= jdbcUtils.getConnection();
             List list=new ArrayList();
             list.add("1");
             list.add("Alan");
             boolean key= jdbcUtils.updateSql("UPDATE USER SET PASSWORD = ? WHERe NAME = ? ",list);
             System.out.println("updateSql测试.........");
             System.out.println(key);
             List  list1=new ArrayList();
             list1.add("6");
             String sql="SELECT  * FROM USER WHERe NAME=?";
             Map map=jdbcUtils.findSimpleResult(sql,list1);
             System.out.println("单条查询测试............");
             System.out.println(map);
             List list2=new ArrayList();
             String sql2="SELECt  * FROM USER";
             List> listmap=jdbcUtils.findMoreResult(sql2,list2);
             System.out.println("多条查询测试.............");
             System.out.println(listmap);
             jdbcUtils.closeAll();
    
        }
    }
     

    测试结果:

    OK.............测试成功! 

    转载请注明:文章转载自 www.mshxw.com
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号