备注:try catch快捷键:选中代码块,然后Ctrl+Alt+T //idea
所需文件夹
查询:statement.executeQuery(sql) 返回结果是数据集
package com;
import java.sql.*;
public class Demo1 {
//备注:try catch快捷键:选中代码块,然后Ctrl+Alt+T
public static void main(String[] args){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//1.加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");//mysql8.0以后版本
//2.获得链接
String username = "root";
String passWord = "123456";
String url = "jdbc:mysql://localhost:3306/mysql2?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("