Jdbc连接入门

连接前准备工作

连接前首先安装驱动

驱动下载网址:MySQL :: MySQL Community Downloads

下载后导入项目

添加库后就可以开始写代码了

1.连接方式1 :静态加载,灵活性较差,依赖强

mysql8.0版导入该包

import com.mysql.cj.jdbc.Driver;

mysql5.0导入 (且驱动也是5.0版)

import com.mysql.jdbc.Driver;

代码

public class Main {
    public static void main(String[] args) throws SQLException {
        // 注册驱动   在项目下添加jar驱动
        Driver driver = new Driver();
        // 设置url url为要连接数据库的地址以及各种相关信息
        String url = "jdbc:mysql://127.0.0.1:3306/fzhl_";
        // 设置相关数据 连接用户 和 密码
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","root");
        // 将url 与 Properties 的数据赋给 connection对象
        Connection connection = driver.connect(url,properties);
        // 执行sql语句
        // 设置
        String sql = "delete from fz03 where id = 5";
        // 执行
        Statement statement = connection.createStatement();
        // 如果是dml 语句 返回影响行
        int a = statement.executeUpdate(sql);
        System.out.println(a>0?"成功":"失败");
        // 关闭流
        connection.close();
        statement.close();
    }
}

运行结果:成功;

2.方法2 利用反射创建driver 动态实例化 增加灵活性

public class Main2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, SQLException {

        // 反射实例化对象
        Class<?> class_ = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) class_.getDeclaredConstructor().newInstance();
        String url = "jdbc:mysql://127.0.0.1:3306/fzhl_";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","root");

        // 执行
        Connection connection = driver.connect(url,properties);
        System.out.println(connection);
        String sql = "delete from fz03 where id = 5";
        Statement statement = connection.createStatement();
        System.out.println(statement.executeUpdate(sql)>0?"成功":"失败");
        // 回收
        connection.close();
        statement.close();
    }
}

3.方法3 

利用DriverManager方法来连接数据库

public class Main3 {
    public static void main(String[] args) throws Exception {
        Class class_ = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) class_.getDeclaredConstructor().newInstance();
        String url = "jdbc:mysql://127.0.0.1:3306/fzhl_";
        String user = "root";
        String pass = "root";

        // 注册Driver 驱动
        DriverManager.registerDriver(driver);
        // 拿到连接
        Connection connection = DriverManager.getConnection(url,user,pass);
        // 执行sql语句的专用对象
        Statement statement = connection.createStatement();
        System.out.println(connection);
        connection.close();
        statement.close();
    }
}

4.方法4

利用DriverManager但跳过注册

public class Main4 {
    public static void main(String[] args) throws Exception{
        Class class_ = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) class_.getDeclaredConstructor().newInstance();
        String url = "jdbc:mysql://127.0.0.1:3306/fzhl_";
        String user = "root";
        String pass = "root";
        // 直接注册
        Connection connection = DriverManager.getConnection(url,user,pass);
        System.out.println(connection);
        connection.close();
    }
}

由于跳过了Driver注册

driver显得毫无作用

这是因为jdk1.5以后使用了jdbc4,不再需要调用class.forname来注册驱动了,而是自动注册

但还是建议写上

5.方法5

利用properties文件注册

properties文件

pass=root
url=jdbc\:mysql\://127.0.0.1\:3306/fzhl_
user=root

代码

public class Main5 {
    public static void main(String[] args) throws Exception{
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));

        //建议写上
        Class.forName("com.mysql.cj.jdbc.Driver");
      
        Connection connection = DriverManager.getConnection(
                properties.get("url").toString(),
                properties.get("user").toString(),
                properties.get("pass").toString());
        System.out.println(connection);
        connection.close();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值