Hi,
I just installed MySQL on my computer and went though a decent MySQL tutorial. Now I need you guys help me out so I can get my java programs to connect to MySQL.

I am using the following code from a book to connect.

import java.sql.*;

public class Hello{
    Connection connection;

    private void displaySQLErrors(SQLException e) {
         System.out.println("SQLException: " + e.getMessage());
         System.out.println("SQLEXception: " + e.getSQLState());
         System.out.println("VendorError: " + e.getErrorCode());
    }

    public Hello() {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    }
    catch (Exception e) {
        System.err.println("Unable to find and load driver");
        System.exit(1);
    }
    }

    public void connectToDB() {
    try {
        connection = DriverManager.getConnection(
              "jdbc:mysql://localost/accounts?user=userName&password=passwd");
    }
    catch(SQLException e) {
        displaySQLErrors(e);
    }
    }

    public void executeSQL() {
    try {
        Statement statement = connection.createStatement();

        ResultSet rs = statement.executeQuery("SELECT * FROM acc_acc");

        while (rs.next()) {
        System.out.println(rs.getString(1));
        }

        rs.close();
        statement.close();
        connection.close();
    }
    catch (SQLException e) {
        displaySQLErrors(e);
    }
    }

    public static void main(String[] args) {
    Hello hello = new Hello();

    hello.connectToDB();
    hello.executeSQL();
    }
}

When I run the program I get the following error. Even afer googling all the lines I can't seem to figure out what the problem is. Any help will be appreciated.

SQLException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.UnknownHostException
MESSAGE: localost

STACKTRACE:

java.net.UnknownHostException: localost
        at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
        at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
        at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1183)
        at java.net.InetAddress.getAllByName0(InetAddress.java:1136)
        at java.net.InetAddress.getAllByName0(InetAddress.java:1109)
        at java.net.InetAddress.getAllByName(InetAddress.java:1072)
        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:137)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
        at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
        at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
        at Hello.connectToDB(Hello.java:25)
        at Hello.main(Hello.java:55)


** END NESTED EXCEPTION **



Last packet sent to the server was 18 ms ago.
SQLEXception: 08S01
VendorError: 0
Exception in thread "main" java.lang.NullPointerException
        at Hello.executeSQL(Hello.java:35)
        at Hello.main(Hello.java:56)

check your URI for a small typo.

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.