mysql数据库 连接myeclipse问题终于解决了

错误展示:程序运行完后,出现这么一大堆,其实你不应该点项目右键运行,而是,点开项目的主类,右键运行就可以了,如果你的代码没问题,就OK了

<ConnectionProperties>
 <PropertyCategory name="Connection/Authentication">
  <Property name="user" required="No" default="" sortOrder="-2147483647" since="all versions">
    The user to connect as
  </Property>
  <Property name="password" required="No" default="" sortOrder="-2147483646" since="all versions">
    The password to use when connecting
  </Property>
  ~~~~~~~~~~

~~~~~~~~~~~


错误展示:实例中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.<init>(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.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(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.<init>(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();
}
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值