JDBC连接数据库六步曲
1.注册驱动
2.获取数据库连接
3.获取数据库操作对象
4.执行sql语句
5.处理查询结果
6.关闭资源
import java.sql.*; /* * 第五步:处理查询结果 * */ public class JDBCTest05 { public static void main(String[] args) { try { //1 注册驱动 Driver driver = new com.mysql.jdbc.Driver(); DriverManager.registerDriver(driver); //2 获取数据库链接 String url = "jdbc:mysql://127.0.0.1:3306/bd171"; String user = "root"; String passwd = "root"; Connection connection = DriverManager.getConnection(url, user, passwd); //3 获取数据库操作对象 Statement statement = connection.createStatement(); //4 执行sql语句 String sql = "select id,last_name,salary from s_emp"; ResultSet resultSet = statement.executeQuery(sql); System.out.println(resultSet); //5 处理查询结果 while (resultSet.next()){ int id = resultSet.getInt("id"); String last_name = resultSet.getString("last_name"); double salary = resultSet.getDouble("salary"); System.out.println(id+" "+last_name+" "+salary); } }catch (Exception e){ e.printStackTrace(); } } }