获取连接的几种初始方法(没有使用数据库连接池)

从今天开始加更数据库中的内容了,满足大家需求
首先更新如何去连接数据库
这是连接数据库的第一种方式,以及里面的具体内容

 public  void testconnetction1() throws SQLException {
        //获取Driver实现类对象,加载驱动
         Driver driver=new com.mysql.cj.jdbc.Driver();
//        jdbc:mysql://localhost:8080/test
        //jdbc:mysql协议
        //localhost:ip地址
        //3306:mysql默认端口号
        //test:使用的数据库名称
        String url ="jdbc:mysql://localhost:3306/test";
        //将用户名和密码封装在 Properties
        Properties info =new Properties();
        info.setProperty("uer","root");
        info.setProperty("password","password");
        Connection connect = DriverManager.getConnection(url,"root","password");
        System.out.println(connect);
    }

数据库连接的第二种方式

//对方式一的迭代,在如下的程序中不出现第三方api,使得程序具有可移植性
    @Test
    public  void testconnetction2() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
      //1.使用Driver实现类对象,使用反射。
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver)clazz.newInstance();
        //2.提供要连接的数据库
        String url="jdbc:mysql://localhost:3306/test";
        //提供连接所需要的用户名和密码
        Properties in=new Properties();
        in.setProperty("user","root");
        in.setProperty("password","password");
        //4.获取链接
        Connection connect = driver.connect(url, in);
        System.out.println(connect);
    }

这是连接数据库的两种方式,大家可以去试试
在这里插入图片描述
给大家提供连接成功的参考图,可以试试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值