database connectivity - java.net.UnknownHostException

15,201

Solution 1

You have missed the colon between hostname and port number... Your connection URL should be

String URL_connection="jdbc:mysql://localhost:3306/vendor";

Solution 2

I think the connection URL should be jdbc:mysql://localhost:3306/vendor . localhost is the server name and 3306 the port name separated by :.

String URL_connection="jdbc:mysql://localhost:3306/vendor";

Please go through the docs.

The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:

jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306.

Share:
15,201
uLYsseus
Author by

uLYsseus

Like interacting and asking questions.....

Updated on June 07, 2022

Comments

  • uLYsseus
    uLYsseus almost 2 years

    I have an exception here in the following code... I am trying to build a simple servlet project and I have the following exception raised: java.net.UnknownHostException...
    I have the mysql jar file added and besides...I have added it as an external jar file and also under lib in WEB-INF, as stated in this page, but now this is throwing an java.net.UnknownHostException.

    This is the code and the exception stack trace.

    package com.servlet.Mysql;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    
    public class Connection_Class {
    String driver_ClassName="com.mysql.jdbc.Driver";
    String URL_connection="jdbc:mysql://localhost3306/vendor";
    String user="root";
    String password="lifesuckzz";
    private static Connection_Class connectionclass=null;
    
    private Connection_Class(){
        try{
            Class.forName(driver_ClassName);
    
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    
    public Connection getConnection() throws SQLException{
        Connection con=null;
        con=DriverManager.getConnection(URL_connection,user,password);
        return con;
    }
    
    public static Connection_Class getInstance(){
        if(connectionclass==null){
            connectionclass=new Connection_Class();
        }
        return connectionclass;}}
    

    Stack trace:

    com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 
    
    ** BEGIN NESTED EXCEPTION ** 
    
    java.net.UnknownHostException
    MESSAGE: localhost3306
    
    STACKTRACE:
    
    java.net.UnknownHostException: localhost3306
    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:133)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1699)
    at com.mysql.jdbc.Connection.<init>(Connection.java:405)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.servlet.Mysql.Connection_Class.getConnection(Connection_Class.java:25)
    at com.servlet.Mysql.LoginServlet.doGet(LoginServlet.java:49)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    
    
    ** END NESTED EXCEPTION **
    
    
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1764)
    at com.mysql.jdbc.Connection.<init>(Connection.java:405)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.servlet.Mysql.Connection_Class.getConnection(Connection_Class.java:25)
    at com.servlet.Mysql.LoginServlet.doGet(LoginServlet.java:49)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    

    I am using Apache Web container and Eclipse, this post java.lang.ClassNotFoundException in spite of using CLASSPATH environment variable I think wasnt near to my case here.....please help me out, thank you all in advance...

  • Amit
    Amit almost 11 years
    Some times it happens buddy... We are all same.... You can upvote the answer if it helped you..