idea2020使用JDBC连接mysql数据库

首先打开模块化设置
在这里插入图片描述
在这里添加相应的.jar包
在这里插入图片描述
然后会得到这样的一个结果
在这里插入图片描述
接着就可以进行测试了,在测试样例中我结合了配置文件一起使用的
配置文件.properties

DRIVER_HOME=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/chencj?characterEncoding=utf-8&serverTimezone=UTC
user=root
password=自己设置的密码
import java.sql.*;
import java.util.ResourceBundle;

public class selectSalgrade {
    public static void main(String[] args) {
        ResourceBundle bundle = ResourceBundle.getBundle("配置文件");

        String DRIVER_HOME = bundle.getString("DRIVER_HOME");
    //    System.out.println(DRIVER_HOME);
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");

        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            //1.加载驱动
            Class.forName(DRIVER_HOME);
            //2.创建连接
            conn = DriverManager.getConnection(url,user,password);
            //3.写SQL语句
            String sql = "select * from salgrade";
            //4.获取执行SQL的statement对象
            statement = conn.createStatement();
            //5.执行select返回结果
            rs = statement.executeQuery(sql);

            System.out.println("============================");
            while(rs.next()){
                System.out.print(rs.getInt(1) + ",");
                System.out.print(rs.getString(2) + ",");
                System.out.println(rs.getString(3));
            }
            System.out.println("============================");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //6.关闭连接
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            if(statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}

测试结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

译制片~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值