链接阿里连接池(listof)

 

 

 


public class druid {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("url","jdbc:mysql:/db1");
map.put("username","aa");
map.put("password","");
try {
DataSource ds = DruidDataSourceFactory.createDataSource(map);//(建立数据库链接)
System.out.println(ds.getConnection());
QueryRunner run= new QueryRunner();//(发送sql)
} catch (Exception e) {
e.printStackTrace();
}
}
}
 

使用

public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("url","jdbc:mysql:/db1");
        map.put("username","aa");
        map.put("password","");

        try {
            DataSource ds = DruidDataSourceFactory.createDataSource(map);//(建立数据库链接)
            System.out.println(ds.getConnection());
            QueryRunner run= new QueryRunner(ds);//(发送sql)

            List<Map<String, Object>> tb =run.query("select *from stu", new MapListHandler());
            System.out.println(tb);

            List<String> tbs=run.query("show tables",new ColumnListHandler<>());//查询列
            System.out.println(tbs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

JDK9的新特性:
    List接口,Set接口,Map接口:里边增加了一个静态的方法of,可以给集合一次性添加多个元素
    static <E> List<E> of​(E... elements)
    使用前提:
        当集合中存储的元素的个数已经确定了,不在改变时使用
 注意:
    1.of方法只适用于List接口,Set接口,Map接口,不适用于接接口的实现类
    2.of方法的返回值是一个不能改变的集合,集合不能再使用add,put方法添加元素,会抛出异常
    3.Set接口和Map接口在调用of方法的时候,不能有重复的元素,否则会抛出异常
posted @ 2021-03-03 21:31  谷先生  阅读(294)  评论(0)    收藏  举报