作者:于磊com | 来源:互联网 | 2023-06-30 09:08
packagecom.jdbc;importcom.mysql.cj.jdbc.Driver;importjava.sql.*;publicclassJdbc{publicstat
package com.jdbc;import com.mysql.cj.jdbc.Driver;import java.sql.*;public class Jdbc {public static void main(String[] args) throws ClassNotFoundException, SQLException {try {Class.forName("com.mysql.cj.jdbc.Driver");} catch (ClassNotFoundException e) {e.printStackTrace();}String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8";String username = "root";String password = "110221";Connection connection = DriverManager.getConnection(url, username, password);Statement statement = connection.createStatement();String sql = "SELECT * FROM users";ResultSet resultSet = statement.executeQuery(sql);while (resultSet.next()){System.out.println("id = "+resultSet.getObject("id"));System.out.println("name = "+resultSet.getObject("NAME"));System.out.println("pwd = "+resultSet.getObject("password"));System.out.println("email = "+resultSet.getObject("email"));System.out.println("birthday = "+resultSet.getObject("birthday"));}resultSet.close();statement.close();connection.close();}
}