MySql connection question.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2006
Posts: 138
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

MySql connection question.

 
0
  #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.
  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: MySql connection question.

 
0
  #2
Feb 3rd, 2007
check your URI for a small typo.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 138
Reputation: ryy705 is an unknown quantity at this point 
Solved Threads: 0
ryy705 ryy705 is offline Offline
Junior Poster

Re: MySql connection question.

 
0
  #3
Feb 3rd, 2007
Thank you.
Last edited by ryy705; Feb 3rd, 2007 at 11:16 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 4337 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC