MySQL数据库的四种连接方式

MySQL数据库连接

  • JDBC :sun公司提供的一套api(java.sql,javax.sql),
    使用这套api可以实现对数据的连接和操作(DML、DDL)

  • 如下代码来测试:获取MySQL数据库连接
    要想获取连接,准备工作:
    ①MySQL服务开启
    ②连接的基本条件:指明数据库的IP地址、端口号、用户名、密码
    ③导入MySQL的驱动(即为jdbc中的相关接口的实现类和集合)

  • 3.网络编程中的url:代表着互联网中的某一资源的地址。
    http://192.168.21.38:8080/zhoushun/index.jsp?user=tom
    协议 IP地址 端口号 目标资源 参数列表

获取数据库连接方式一:

1.提供MySQL中的Driver接口的实现类

Driver driver = new com.mysql.jdbc.Driver();

2.注册驱动

DriverManager.registerDriver(driver);

3.获取连接的基本信息

String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名
String user = "root";
String password = "root";

4.获取连接

 Connection connection = DriverManager.getConnection(url, user, password);
 System.out.println(connection);

代码(方式一):

 @Test
    public void test1() throws SQLException {


        //提供MySQL中的Driver接口的实现类
        Driver driver = new com.mysql.jdbc.Driver();

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


        String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名
        String user = "root";
        String password = "root";

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

    }



获取数据库连接方式二:使用反射实现Driver的实例化。面向接口编程 使程序具备更好的移植性

1.提供MySQL中的Driver接口的实现类

 String className = "com.mysql.jdbc.Driver";
 Class cla = Class.forName(className);
 Driver driver = (Driver) cla.newInstance();

2.注册驱动

DriverManager.registerDriver(driver);

3.获取连接的基本信息

String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名
String user = "root";
String password = "root";

4.获取连接

 Connection connection = DriverManager.getConnection(url, user, password);
 System.out.println(connection);

代码(方式二):

  @Test
    public void test2() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {


        //提供MySQL中的Driver接口的实现类
        String className = "com.mysql.jdbc.Driver";
        Class cla = Class.forName(className);
        Driver driver = (Driver) cla.newInstance();

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


        String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名
        String user = "root";
        String password = "root";

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

    }




获取数据库连接方式三:省略注册过程。面向接口编程 使程序具备更好的移植性

1.获取连接的基本信息

String className = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名
String user = "root";
String password = "root";

2.加载驱动

 Class.forName(className);

3.获取连接

 Connection connection = DriverManager.getConnection(url, user, password);
 System.out.println(connection);

代码(方式三):

    @Test
    public void test3() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {


        //1.获取连接的4个基本信息
        String className = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test";//test:表示具体的数据库名
        String user = "root";
        String password = "root";

        //2.加载驱动
        Class.forName(className);
        //获取连接
        
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);

    }





(重点)获取数据库连接方式四

配置文件在src目录下新建一个jdbc.properties文件
在文件中写入如下代码:

className=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=root

1.读取配置文件中的4个基本信息

 Properties pros = new Properties();


        //加载资源的路径默认在src下
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

        pros.load(is);
        String className = pros.getProperty("className");
        String url = pros.getProperty("url");
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");

2.加载驱动

 Class.forName(className);

3.获取连接

 Connection conn = DriverManager.getConnection(url, user, password);
 System.out.println(conn);

代码(方式四):

 @Test
    public void test4() throws Exception {

        //读取配置文件中的4个基本信息
        Properties pros = new Properties();


        //加载资源的路径默认在src下
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

        pros.load(is);
        String className = pros.getProperty("className");
        String url = pros.getProperty("url");
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");

        //2.加载驱动
        Class.forName(className);

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


    }


目录:
在这里插入图片描述




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值