944,004 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 6214
  • Java RSS
Feb 2nd, 2007
0

MySql connection question.

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  1. import java.sql.*;
  2.  
  3. public class Hello{
  4. Connection connection;
  5.  
  6. private void displaySQLErrors(SQLException e) {
  7. System.out.println("SQLException: " + e.getMessage());
  8. System.out.println("SQLEXception: " + e.getSQLState());
  9. System.out.println("VendorError: " + e.getErrorCode());
  10. }
  11.  
  12. public Hello() {
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver").newInstance();
  15. }
  16. catch (Exception e) {
  17. System.err.println("Unable to find and load driver");
  18. System.exit(1);
  19. }
  20. }
  21.  
  22. public void connectToDB() {
  23. try {
  24. connection = DriverManager.getConnection(
  25. "jdbc:mysql://localost/accounts?user=userName&password=passwd");
  26. }
  27. catch(SQLException e) {
  28. displaySQLErrors(e);
  29. }
  30. }
  31.  
  32. public void executeSQL() {
  33. try {
  34. Statement statement = connection.createStatement();
  35.  
  36. ResultSet rs = statement.executeQuery("SELECT * FROM acc_acc");
  37.  
  38. while (rs.next()) {
  39. System.out.println(rs.getString(1));
  40. }
  41.  
  42. rs.close();
  43. statement.close();
  44. connection.close();
  45. }
  46. catch (SQLException e) {
  47. displaySQLErrors(e);
  48. }
  49. }
  50.  
  51. public static void main(String[] args) {
  52. Hello hello = new Hello();
  53.  
  54. hello.connectToDB();
  55. hello.executeSQL();
  56. }
  57. }
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.
Java Syntax (Toggle Plain Text)
  1. SQLException: Communications link failure due to underlying exception:
  2.  
  3. ** BEGIN NESTED EXCEPTION **
  4.  
  5. java.net.UnknownHostException
  6. MESSAGE: localost
  7.  
  8. STACKTRACE:
  9.  
  10. java.net.UnknownHostException: localost
  11. at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
  12. at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
  13. at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1183)
  14. at java.net.InetAddress.getAllByName0(InetAddress.java:1136)
  15. at java.net.InetAddress.getAllByName0(InetAddress.java:1109)
  16. at java.net.InetAddress.getAllByName(InetAddress.java:1072)
  17. at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:137)
  18. at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
  19. at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
  20. at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
  21. at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
  22. at java.sql.DriverManager.getConnection(DriverManager.java:582)
  23. at java.sql.DriverManager.getConnection(DriverManager.java:207)
  24. at Hello.connectToDB(Hello.java:25)
  25. at Hello.main(Hello.java:55)
  26.  
  27.  
  28. ** END NESTED EXCEPTION **
  29.  
  30.  
  31.  
  32. Last packet sent to the server was 18 ms ago.
  33. SQLEXception: 08S01
  34. VendorError: 0
  35. Exception in thread "main" java.lang.NullPointerException
  36. at Hello.executeSQL(Hello.java:35)
  37. at Hello.main(Hello.java:56)
Last edited by ryy705; Feb 2nd, 2007 at 5:17 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
ryy705 is offline Offline
171 posts
since Jul 2006
Feb 3rd, 2007
0

Re: MySql connection question.

check your URI for a small typo.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 3rd, 2007
0

Re: MySql connection question.

Thank you.
Last edited by ryy705; Feb 3rd, 2007 at 11:16 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster
ryy705 is offline Offline
171 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Super-power compare method? <?>
Next Thread in Java Forum Timeline: ServletContextAttributeListener not working





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC