JDBC操作步骤1 -查询

import java.sql.*;

public class Demo1 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;


        try {
            //1.加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获得链接
            String userName = "root";
            String password = "root";
            String url = "jdbc:mysql://localhost:3306/yhp2?serverTimezone=UTC";
            connection = DriverManager.getConnection(url,userName,password);
            
           
            //3.定义sql,创建状态通道(进行sql语句的发送)
            statement = connection.createStatement();

变动区域

        resultSet = statement.executeQuery("select * from emp1"); //executeQuery(sql) 执行查询
        
        
        //4.取出结果集信息
        while(resultSet.next()){ //判断是否有下一条数据
            //取出数据: resultSet.getXXX("列名")  xxx表示数据类型
            System.out.println("姓名:"+resultSet.getString("ename")+"工资:"+
                    resultSet.getDouble("sal")+",雇佣日期:"+resultSet.getDate("hiredate"));

        }
      } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                
                
                //5.关闭资源
                if(resultSet !=null){
                    resultSet.close();
                }
                if(statement !=null){
                   statement.close();
                }
                if( connection !=null){
                    connection.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }

    }
}

数据库yhp2
在这里插入图片描述


emp1表
在这里插入图片描述

执行结果显示:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值