控制台报错

控制台报错

package com.imooc.jdbc.demo1;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


import org.junit.Test;


import com.mysql.jdbc.Driver;


public class JDBCDemo1 {

@Test

/*

* jdbc的入门程序

*/

public void demo1() {

//1.加载驱动

try {

DriverManager.registerDriver(new Driver());

//2.获得链接

Connection conn = DriverManager.getConnection("jdbc:mysql://locahost:3306/jdbctest", "root@localhost", "12345");

//3.创建执行SQL语句的对象,并执行SQL

//3.1创建执行SQL对象

String sql = "select * from user";

Statement stmt = conn.createStatement();

//3.2执行sql

ResultSet resultSet = stmt.executeQuery(sql);

while(resultSet.next()) {

int uid = resultSet.getInt("uid");

String username = resultSet.getString("username");

String password = resultSet.getString("password");

String name = resultSet.getString("name");

System.out.println(uid+"   "+username+"  "+password+"   "+name);

}

//4.释放资源

resultSet.close();

stmt.close();

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}




com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure


Last packet sent to the server was 0 ms ago.

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:406)

at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)

at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2120)

at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)

at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)

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:406)

at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)

at java.sql.DriverManager.getConnection(Unknown Source)

at java.sql.DriverManager.getConnection(Unknown Source)

at com.imooc.jdbc.demo1.JDBCDemo1.demo1(JDBCDemo1.java:23)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure


Last packet sent to the server was 0 ms ago.

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:406)

at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)

at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:335)

at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2043)

... 35 more

Caused by: java.net.UnknownHostException: locahost

at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)

at java.net.InetAddress$2.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:243)

at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)

... 36 more

http://img1.sycdn.imooc.com//climg/5e32a8320911434b04580433.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕柯南 2020-02-02 11:00:33

同学你在运行时是否运行了全部的测试方法,这样只要一个有错,就会报错呢.

建议你在运行时选中要运行的方法,右键运行。

如果仍然报错,可能是你的电脑并不能识别localhost,建议你将localhost改为127.0.0.1,测试一下。

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

好帮手慕柯南 2020-02-01 11:09:08

同学你好!

报错信息是你的主机名错误:

http://img1.sycdn.imooc.com//climg/5e34eb780958852e05960077.jpg

你的代码中将localhost写成了locahost少写了一个l

http://img1.sycdn.imooc.com//climg/5e34ebb9095323c108780131.jpg

如果我的回答解决了你的疑惑,请采纳,祝 学习愉快~




  • 提问者 weixin_慕瓜9083905 #1
    我其他写对的也报错,都是报的这个
    2020-02-01 14:25:36
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师