User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,083 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,942 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2069 | Replies: 2 | Solved
Reply
Join Date: Jul 2006
Posts: 107
Reputation: ryy705 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

MySql connection question.

  #1  
Feb 2nd, 2007
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)
Last edited by ryy705 : Feb 2nd, 2007 at 4:17 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 191
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: MySql connection question.

  #2  
Feb 3rd, 2007
check your URI for a small typo.
42 Private messages asking for help will be ignored
In the frozen land of Nador they were forced to eat Steve's iMinstrels, and there was much rejoicing.
Reply With Quote  
Join Date: Jul 2006
Posts: 107
Reputation: ryy705 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

Re: MySql connection question.

  #3  
Feb 3rd, 2007
Thank you.
Last edited by ryy705 : Feb 3rd, 2007 at 10:16 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 12:26 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC