连接数据库的五种方式

方式1

        //1.注册驱动
        Driver driver = new Driver();
        //2.得到连接
        //(1)jdbc:mysql:// 规定好的协议,通过jdbc的方式连接数据库
        //(2)localhost主机地址,可以是IP
        //(3)3306表示mysql监听接口
        //(4)cyy_db02表示连接到mysql dbms的哪个数据库
        //(5)mysql连接本质为socket连接
        String url="jdbc:mysql://localhost:3306/cyy_db02";
        //将用户名和密码放入到Properties 对象
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","cyy");
        Connection connect = driver.connect(url, properties);
        //3.执行sql
        String sql="insert into actor values(null,'xxx','男','1970-11-11','110')";
        //用于执行静态的SQL语句并返回其生成的结果的对象
        Statement statement = connect.createStatement();
        int rows=statement.executeUpdate(sql);//如果是dml语句,返回的就是影响的行数
        System.out.println(rows>0?"成功":"失败");
        //4.关闭连接资源
        statement.close();
        connect.close();

方式2

  //使用反射加载Driver类,动态加载,更加灵活,减少依赖性
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        String url="jdbc:mysql://localhost:3306/cyy_db02";
        //将用户名和密码放入到Properties 对象
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","cyy");
        Connection connect = driver.connect(url, properties);
        System.out.println("方式2"+connect);

方式3

 //方式3 使用DriverManager 代替Driver进行统一管理
    public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        //使用反射加载
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        //创建url和user和password
        String url="jdbc:mysql://localhost:3306/cyy_db02";
        String user="root";
        String password="cyy";
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("方式3"+connection);
    }

方式4(推荐使用)

 public void connect04() throws ClassNotFoundException, SQLException {
        //使用反射加载Driver类
        //在加载Driver类时,完成注册
        /**
         * 源码1.静态代码块,在类加载时会执行一次
         *static {
         *         try {
         *             DriverManager.registerDriver(new Driver());
         *         } catch (SQLException var1) {
         *             throw new RuntimeException("Can't register driver!");
         *         }
         *     }
         */
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        //创建url和user和password
        String url="jdbc:mysql://localhost:3306/cyy_db02";
        String user="root";
        String password="cyy";
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("方式4"+connection);
    }

方式5

  //通过Properties获得配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //获取相关的值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("方式5"+connection);

这里为配置文件mysql.properties信息
url=jdbc:mysql://localhost:3306/cyy_db02
user=root
password=cyy
driver=com.mysql.jdbc.Driver

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值