复习JDBC第一课(以mysql为例--连接问题)

Java连接数据库需要以下四个要素:
1.数据库的用户名
2.数据库的密码
3.url
4.数据库的驱动(需要导入相关jar包)

以IDEA为例,首先新建一个文件(文件名自拟,我这里取为properties)
在这个文件中写入上方需要的四个元素:

user=数据库用户名
password=数据库密码
url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
driverClass=com.mysql.cj.jdbc.Driver

一定注意这四个元素的每行都不要在"="两边加空格!!!
这些准备好了,接下来就可以进行mysql数据库的连接操作啦!
方式一:
比较容易理解,但是耦合度较高基本不用

public void testConnection1() throws SQLException {
        //获取Driver实现类对象
        Driver driver = new com.mysql.cj.jdbc.Driver();
         String url = "jdbc:mysql://localhost:3306/你的数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
         //将用户名和密码封装在Properties中
        Properties info = new Properties();
        info.setProperty("user",你的数据库用户名");
        info.setProperty("password","你的数据库密码");
        Connection conn = driver.connect(url,info);
        System.out.println(conn);
    }

方式二:对方式一的迭代,不出现第三方API是程序具有更好的可移植性

public void testConnection2() throws ClassNotFoundException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取Driver实现类对象,使用反射
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver)clazz.getDeclaredConstructor().newInstance();

        //提供要连接的数据库
        String url = "jdbc:mysql://localhost:3306/你的数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";

        //提供连接需要的用户名和密码
        Properties info = new Properties();
        info.setProperty("user","你的数据库用户名");
        info.setProperty("password","你的数据库密码");

        //获取连接
        Connection connect = driver.connect(url, info);
        System.out.println(connect);
    }

方式三:使用DriverManager替换Driver获取连接

public void testConnection3() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, SQLException {
        //1.获取Driver实现类对象,使用反射
        Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver)clazz.getDeclaredConstructor().newInstance();

        //提供三个连接信息
        String url = "jdbc:mysql://localhost:3306/你的数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
        String user = "root";
        String password = "718528hxy";

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

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

方式四:相对于方式三:可以省略注册…
在mysql的Driver的实现类中声明了注册操作接下来截取一段自动注册的源码在这里插入图片描述

    public void testConnection4() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, SQLException {
   		 //提供三个连接信息
    	String url = "jdbc:mysql://localhost:3306/你的数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
    	String user = "你的数据库用户名";
    	String password = "你的数据库密码";
    	//1.加载Driver
   		Class.forName("com.mysql.cj.jdbc.Driver");
    	//2.省略注册直接获取连接
   		Connection conn = DriverManager.getConnection(url,user,password);
    	System.out.println(conn);
}

方式五:将数据库连接所需要的四个基本信息声明在配置文件中,通过读取配置文件的方式,获取连接,强推这种方法
优点:1.实现了代码和数据的分离
2.可移植性强

public void testConnection5() throws IOException, ClassNotFoundException, SQLException {
        //读取配置文件中的四个基本信息
        //由于这是一个方法属于个人写的类,我这里的方法就在ConnectionTest类中,进行类的加载同时以流的方式读取配置文件
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("你的配置文件名");
        Properties properties = new Properties();
        properties.load(is);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");

        //加载驱动
        Class.forName(driverClass);
        Connection con = DriverManager.getConnection(url,user,password);
        System.out.println(con);
    }

当调用方法输出结果为类似于com.mysql.cj.jdbc.ConnectionImpl@2f9f7dcf即证明你的数据库连接成功。
以上就是几种连接Mysql数据库的方式,我在这里犯了个懒,把所有的异常都抛出去了,省的try…catch调格式,大家还是正规的使用异常处理哈,希望对大家有帮助,如有哪些地方说明错误,请各位指正!!共同学习!(2020-07-05加油!)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值