首先导入jar包。

 https://downloads.mysql.com/archives/c-j/
package com.test.sql;import java.sql.*;public class StudySql {public static void init() throws SQLException {Statement  stmt = null;Connection conn = null;ResultSet  res  = null;PreparedStatement pstmt =  null;try {Class.forName("com.mysql.cj.jdbc.Driver");conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book_db?serverTimezone=UTC","root","123456");String sql = "select * from books";pstmt = conn.prepareStatement("select * from books where book_id>?");pstmt.setInt(1, 0);res = pstmt.executeQuery();//stmt = conn.createStatement();//res  = stmt.executeQuery(sql);while (res.next()) {int    id     = res.getInt("book_id");String name   = res.getString("book_name");String author = res.getString("book_author");float  price  = res.getFloat("book_price");int    stock  = res.getInt("book_stock");String desc   = res.getString("book_desc");System.out.printf("%d %s %s %f %s %s\n", id, name, author, price, stock, desc);}} catch (ClassNotFoundException | SQLException e) {e.printStackTrace();} finally {if (stmt != null) {stmt.close();}if (conn != null) {conn.close();}if (res != null) {res.close();}}}
}//~ Formatted by Jindent --- http://www.jindent.com