在IDEA中使用JDBC(maven)

在IDEA使用JDBC连接数据库

在maven的pom.xml中导入依赖

  <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>

刷新maven项目,待项目中下载完mysql-connector的jar文件.

在项目中创建一个JDBCDeamo类

import java.sql.*;

/**
 * @Deacription TODO
 * @Author d1252
 * @Date 2021/5/19 15:08
 **/

public class JDBCDemo1 {
    private String user = null;
    private String password = null;
    private String Driver = null;
    Connection conn = null;
    Statement state = null;
    ResultSet rs = null;

    public void getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        if (conn == null) {

            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "12345678");
            System.out.println("连接数据库成功");
            state = conn.createStatement();
            int count = 0;
            rs = state.executeQuery("select * from studentinfo");
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                Boolean gender = rs.getBoolean("gender");
                Date birthday = rs.getDate("birthday");
                System.out.println("序号:" + id + "\t名称:" + name + "\t性别:" + gender + "\t生日:" + birthday
                );
            }

        }

    }

    public void closeAll() throws SQLException {
        if (rs!=null){
            rs.close();
        }
        if (state != null) {
            state.close();
        }
        if (conn != null) {
            conn.close();
        }
        System.out.println("释放资源成功!");
    }

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        JDBCDemo1 jdbcDemo1 = new JDBCDemo1();
        jdbcDemo1.getConnection();
        jdbcDemo1.closeAll();

    }

}

连接数据库成功
序号:1	名称:孙悟空	生日:1993-03-24
序号:2	名称:白骨精	生日:1995-03-24
序号:3	名称:猪八戒	生日:1903-03-08
序号:4	名称:孙悟空	生日:1993-03-24
序号:5	名称:黑熊怪	生日:1999-03-23
序号:6	名称:黑熊怪	生日:1999-03-23
序号:7	名称:白龙马	生日:1994-01-21
序号:8	名称:白龙马	生日:1994-01-21
序号:9	名称:白龙马	生日:1994-01-21
序号:10	名称:白龙马	生日:1994-01-21
序号:12	名称:白龙马	生日:1994-01-21
序号:13	名称:白龙马	生日:1994-01-21
序号:14	名称:白龙马	生日:1994-01-21
释放资源成功!

Process finished with exit code 0

值得注意的是高版本的mysql-connector-java jar文件,在JDBC中的Driver变量值为"com.mysql.cj.jdbc.Driver",低版本比如mysql-connector-java:5.1.25则是上述"com.mysql.jdbc.Driver"

如果非Maven管理项目的,可在项目构造Project Structure中导入mysql的jar包,并且在此处也可以管理项目中导入jar包的版本

File>>>Project Structure 或者Shift+Alt+Ctrl+S打开

image-20210519184744879

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

坐着地板打地鼠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值