错误展示:程序运行完后,出现这么一大堆,其实你不应该点项目右键运行,而是,点开项目的主类,右键运行就可以了,如果你的代码没问题,就OK了
The user to connect as
The password to use when connecting
~~~~~~~~~~
~~~~~~~~~~~
错误展示:实例中String url="jdbc:mysql://host:3306/my?user=root&password=mysqladmin";用的是蓝色的host,而我自己用的localhost,替换了蓝色host就可以了,哈哈,终于解决了控绕我1个星期的问题了,呵呵。
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:357)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2482)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at JDBCExample.main(JDBCExample.java:11)
Caused by: java.net.UnknownHostException: host
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(Unknown Source)
at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
at java.net.InetAddress.getAllByName0(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at java.net.InetAddress.getAllByName(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:249)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:307)
... 15 more
import java.sql.DriverManager; import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCExample {
public static void main(String args[]){
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/my?user=root&password=mysqladmin";
//注释解释,这是最为关键的,在建mysql时设置为了localhost,你要看自己设置的是什么,my是建的数据库的名字, Connection conn=DriverManager.getConnection(url); Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery("select *from user"); while(rs.next()){ System.out.println("用户的编号"+rs.getInt(1)); System.out.println("\t用户的名字"+rs.getString(2)); System.out.println("\t用户的密码"+rs.getString(3)); } rs.close(); stmt.close(); conn.close(); }catch(ClassNotFoundException e){ System.out.println("找不到指定的驱动程序类"); }catch(SQLException e){ e.printStackTrace(); } } }