package com.mf.jdbc;
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
- JDBC的快速入门--ResultSet
*/
public class JDBCDemo {
public void JDBCDemo() throws Exception {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
//3.定义sql语句
String sql = "Select * from emp";
//4.获取Statement对象
Statement stmt = conn.createStatement();
//5.执行sql,现在执行的是查询语句,所以用ex
ResultSet rs = stmt.executeQuery(sql);
//6.处理结果,遍历rs中的所有数据
/*
光标向下移动,并且判断当前行有没有数据
获取数据,getXxx()
*/
while(rs.next() ){
int id = rs.getInt(1);
String name = rs.getString(2);
double salary = rs.getDouble(3);
System.out.println(id);
System.out.println(name);
System.out.println(salary);
System.out.println("----------------------");
}
stmt.close();
rs.close();
conn.close();
}
}
标签:总结,String,rs,5.15,System,sql,println,import From: https://www.cnblogs.com/XiMenXve/p/17401007.html