login登录

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

                        1       Testjdbcs
public class Testjdbcs {
    public static void main(String[] args) throws Exception {
        Connection connection=DbManager10.getConnection("day106","root"," ***** ");
        String select="select * from note ";
        PreparedStatement preparedStatement=connection.prepareStatement(select);
        ResultSet resultSet= preparedStatement.executeQuery();
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        List<Map<String ,Object>> list=new ArrayList<>();
        while (resultSet.next()){
            Map<String, Object> map=new HashMap<>() ;
            for (int i = 1; i <columnCount+1; i++) {
                String columnName = metaData.getColumnName(i);
                Object object = resultSet.getObject(columnName);
                map.put(columnName,object);
            }
            list.add(map);
            System.out.println();
        }
        System.out.println(list);

        DbManager10.closeAll(connection,preparedStatement,resultSet);
    }
}


                    
                    2      DbManagerTest
                        


import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DbManagerTest {
    public static Connection getConnection(String  database,String user,String pass) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://127.0.0.1:3306/"+database+"?characterEncoding=UTF-8";
    Connection connection= DriverManager.getConnection(url,user,pass);
    return connection;
    }



    public static void closeAll(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException {
        if (connection != null && preparedStatement !=null && resultSet !=null) {
            connection.close();
            preparedStatement.close();
            resultSet.close();

        }

    }
}


                        3    TestMeta

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.*;

public class TestMeta {
    public static void main(String[] args) {
       
            Connection connection = DBManager.getConnection("root", null);
            String sql="select * from t_user";
            List<Map<String, Object>> queryList = DBManager.query(sql, null);
            for (int i = 0; i < queryList.size(); i++) {
                Map<String, Object> map = queryList.get(i);
                Set<String> set = map.keySet();//获取存放key的set集合
                for (String s : set) {
                    System.out.print("键:"+s+" 值:"+map.get(s)+"\t");
                }
                System.out.println("");
            }








































package com.cmcc.factory;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DbManager {
    public static Connection getConnection(String user,String pass) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://127.0.0.1:3306/tests?characterEncoding=UTF-8";
        Connection connection= DriverManager.getConnection(url,user,pass);
        return connection;
    }
    public static List<Map<String, Object>> query(String select, Object[] params) throws Exception {
        Connection connection=getConnection("root"," ***** ");
        PreparedStatement preparedStatement=connection.prepareStatement(select);
        if (params!=null){
            preparedStatement=setParams(preparedStatement,params);
        }
        ResultSet resultSet=preparedStatement.executeQuery();
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        List<Map<String,Object>> list=new ArrayList<>();
        while (resultSet.next()){
            Map<String,Object> map=new HashMap<>();
            for (int i = 1; i <=columnCount ; i++) {
                String columnName = metaData.getColumnName(i);
                Object object = resultSet.getObject(columnName);
                map.put(columnName,object);
            }
            list.add(map);
        }
        return list;
    }
    public static PreparedStatement setParams(PreparedStatement pst,Object[] params) throws SQLException {
        if (params==null){
            return null;
        }
        for (int i = 0; i < params.length; i++) {
            pst.setObject(i+1,params[i]);
        }
        return pst;
    }
    public static int update(String sql, Object[] params) throws Exception {
        Connection connection=getConnection("root"," ***** ");
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement=setParams(preparedStatement,params);
        int i = preparedStatement.executeUpdate();
        return i;
    }
    public static void closeAll(Connection  connection, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException {
        if (connection != null && preparedStatement !=null && resultSet !=null) {
            connection.close();
            preparedStatement.close();
            resultSet.close();
        }
    }
}



posted @ 2021-03-03 01:45  梦已然清空  阅读(610)  评论(0编辑  收藏  举报