依赖引入:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
代码编写:
public class JdbcUtil {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/cms_base?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username="username";
String password="passowrd";
Connection connection = DriverManager.getConnection(url,username,password);
Statement statement = connection.createStatement();
String sql = "SELECT *FROM biz_book limit 10;";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
System.out.println("="+resultSet.getString("book_url"));
System.out.println("="+resultSet.getString("book_name"));
}
resultSet.close();
statement.close();
connection.close();
}
}