MySQL版本:8.0.11 社区 windows版。
执行以下三条命令即可:
mysql> use mysql; Database changed mysql> update user set host='%' where user='root'; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.02 sec)
程序如下:
package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class ConnMysql8 { public static void main(String[] args) { Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try{ Class.forName("com.mysql.cj.jdbc.Driver");
// 注意,地址用环回地址和真实IP,付出的努力是要更多的,当然收获也更多 conn=DriverManager.getConnection("jdbc:mysql://192.168.0.109/hy?useSSL=false", "root", "123456"); String sql=" select * from emp"; ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ System.out.print(rs.getString("id")+"\t"); System.out.print(rs.getString("name")+"\t"); System.out.println(); } } catch(SQLException se){ try{ conn.rollback(); } catch(Exception ex){ ex.printStackTrace(); } } catch(Exception e){ try{ conn.rollback(); } catch(Exception ex){ ex.printStackTrace(); } } finally{ try{ rs.close(); ps.close(); conn.close(); } catch(Exception ex){ ex.printStackTrace(); } } } }
运行结果:
1 andy
2 cindy
程序下载地址(内有MySQL8驱动包mysql-connector-java-8.0.11):
https://files.cnblogs.com/files/heyang78/MySQL8_classic_jdbc.rar
END