import java.sql.*; // 导入数据库操作的包,这种方式是将sql下面的所有的实体类都进行导入标签:utf8,JDBC,Java,url,getConnection,connection,static,mysql,链接 From: https://www.cnblogs.com/devilben/p/17354794.html
public class JDBCConnect {
static Connection connection = null;
// characterEncoding=utf8 设置编码格式为utf8,serverTimezone=GMT+8设置时区,在mysql 8.0+ 的url需要添加时区,该设置为东八区时间
static String url ="jdbc:mysql://localhost:3306/studentdb?characterEncoding=utf8&serverTimezone=GMT+8";
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver"); // 通过反射,获取到mysql的驱动
return DriverManager.getConnection("url","root","root"); // 通过驱动管理获取到和数据库的链接
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// connection = getConnection();
String url ="jdbc:mysql://localhost:3306/studentdb?characterEncoding=utf8";
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url,"root","123456");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from studentdb");
while (resultSet.next()){
resultSet.getInt("ID");
resultSet.getString("name");
}
System.out.println(connection);
}
}