1,105,226 Community Members

MySql connection question.

Member Avatar
ryy705
Junior Poster
171 posts since Jul 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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)
Member Avatar
jwenting
duckman
8,333 posts since Nov 2004
Reputation Points: 1,399 [?]
Q&As Helped to Solve: 445 [?]
Skill Endorsements: 35 [?]
Infraction Points: 5
Team Colleague
 
0
 

check your URI for a small typo.

Question Answered as of 7 Years Ago by jwenting
Member Avatar
ryy705
Junior Poster
171 posts since Jul 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article