IntelliJIdea2019.3 连接Mysql数据库步骤详解(三)

这篇博客主要讲解如何IntelliJIdea2019.3开发工具编写Java代码链接数据库

第一步:编写Java代码

package com.mysql;

import java.sql.*;

public class test {
    public static void main(String[] args) {
        //加载驱动器类
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //驱动器路径
        String url="jdbc:mysql://localhost:3306/lianxi";
        //查询语句
        String sql="select * from student";
        //加载链接
        try {
            Connection con = DriverManager.getConnection(url, "root", "123456");
            //创建statement对象
            Statement sta = con.createStatement();
            //执行查询语句
            ResultSet res = sta.executeQuery(sql);
            //处理结果
            while(res.next()){
                System.out.println(
                       "姓名:"+ res.getString(1)+ "年龄"+res.getInt(2)
                );
            }
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

 第二步:加载Jar包依赖:点击File---》点击Project Structure---》点击Module---》点击+号---》JAR or directories

第三步:运行主方法:最后运行成功

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值