JDBC获取数据库连接的方式

方式一:

    @Test
    public void testConnection1() {
        try {
//            1、提供java.sql.Driver接口实现类的对象
            Driver driver = null;
            driver = new Driver();

//            2、提供url,指明要操作的数据库
            String url = "jdbc:mysql://localhost:3307/test";

//            3、提供Properties对象,指明用户名和密码
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "123456");

//            4、调用driver的connect(),获取连接
            Connection conn = driver.connect(url, info);
            System.out.println(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

方式二: 

 @Test
    public void testConnection2() {
        try {
//            1、实例化Driver
            String className = "com.mysql.jdbc.Driver";
            Class clazz = Class.forName(className);
            Driver driver = (Driver) clazz.newInstance();

//            2、提供url,指明要操作的数据库
            String url = "jdbc:mysql://localhost:3307/test";

//            3、提供Properties对象,指明用户名
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "123456");

//            4、调用driver的connect(),获取连接
            Connection conn = driver.connect(url, info);
            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

方式三: 

 @Test
    public void testConnection3() {
        try {
//            1、数据库连接的4个基本要素
            String url = "jdbc:mysql://localhost:3307/test";
            String user = "root";
            String password = "123456";
            String className = "com.mysql.jdbc.Driver";

//            2、实例化Driver
            Class clazz = forName(className);
            Driver driver = (Driver) clazz.newInstance();

//            3、注册驱动
            DriverManager.registerDriver(driver);

//            4、获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

方式四:

@Test
    public void testConnection4() {
        try {
//            1、获取数据库连接的4个基本要素
            String url = "jdbc:mysql://localhost:3307/test";
            String user = "root";
            String password = "123456";
            String driverName = "com.mysql.jdbc.Driver";

//            2、加载驱动(实例化Driver、注册驱动)
            Class.forName(driverName);

//            实例化Driver
//            Class<?> clazz = forName(driverName);
//            Driver driver = (Driver) clazz.newInstance();

//            注册驱动
//            DriverManager.registerDriver(driver);

//            Driver类中的静态代码块随类的加载而加载
//            static {
//                try {
//                    DriverManager.registerDriver(new Driver());
//                } catch (SQLException var1) {
//                    throw new RuntimeException("Can't register driver!");
//                }
//            }

//            3、获取连接
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

方式五:(最推荐的方式) 

jdbc.properties 配置文件声明在工程的src目录下

# =号两边不能有空格,否则会报错
user=root
password=123456
url=jdbc:mysql://localhost:3307/test
driverClass=com.mysql.jdbc.Driver

  @Test
    public void testConnection5() throws Exception{
//        1、加载配置文件
        InputStream is = MyTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);

//        2、读取配置文件
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

//        3、加载驱动
        Class.forName(driverClass);

//        4、获取连接
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

slx9920

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值