Need Help in Connecting MySQL Database to JTable

Reply

Join Date: Jan 2009
Posts: 2
Reputation: SSJVEGETA is an unknown quantity at this point 
Solved Threads: 0
SSJVEGETA SSJVEGETA is offline Offline
Newbie Poster

Need Help in Connecting MySQL Database to JTable

 
0
  #1
Jan 5th, 2009
Good morning everyone. I am having trouble connecting my MySQL database to a JTable in Eclipse. I keep on getting an exception and receiving this message "Unable to find and load database driver". Here is a sample of database code for the program:
  1. class QueryTableModel extends AbstractTableModel
  2. {
  3. Vector cache;
  4. int ColCount;
  5. String[] headers;
  6. Connection db=null;
  7. Statement statement;
  8. String driverName="com.mysql.jdbc.Driver";
  9. //String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
  10. //String driverName = "com.jnetdirect.jsql.JSQLDriver";
  11. //String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  12. String SQLLoadDatabaseQuery = "SELECT * FROM HouseHoldApplianceDatabase";
  13. String URL = "jdbc:mysql://localhost:3306/DatabaseName";
  14. // String serverName = "127.0.0.1";
  15. // String portNumber = "1433";
  16. //String mydatabase = serverName + ":" + portNumber;
  17. //String currentURL = "jdbc:JSQLConnect://" + mydatabase;
  18. //String username = "username";
  19. // String password = "password";
  20.  
  21. public QueryTableModel()
  22. {
  23. cache = new Vector();
  24. closeDB();
  25. initDB(URL);
  26. loadDB();
  27. }
  28.  
  29. public String getColumnName(int i)
  30. {
  31. return headers[i];
  32. }
  33.  
  34. public int getColumnCount()
  35. {
  36. return ColCount;
  37. }
  38.  
  39. public int getRowCount()
  40. {
  41. return cache.size();
  42.  
  43. }
  44.  
  45. public Object getValueAt(int row, int col)
  46. {
  47. return ((String[]) cache.elementAt(row))[col];
  48. }
  49.  
  50. public void loadDB()
  51. {
  52. cache = new Vector();
  53. try {
  54.  
  55.  
  56. ResultSet rs = statement.executeQuery(SQLLoadDatabaseQuery);
  57. ResultSetMetaData meta = rs.getMetaData();
  58. ColCount = meta.getColumnCount();
  59.  
  60. headers = new String[ColCount];
  61. for (int h = 1; h <= ColCount; h++) {
  62. headers[h - 1] = meta.getColumnName(h);
  63. }
  64.  
  65. while (rs.next()) {
  66. String[] record = new String[ColCount];
  67. for (int i = 0; i < ColCount; i++) {
  68. record[i] = rs.getString(i + 1);
  69. }
  70. cache.addElement(record);
  71. }
  72. fireTableChanged(null);
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. System.err.println("Database still can't be loaded");
  76. }
  77.  
  78. }
  79.  
  80. public void closeDB() {
  81. try {
  82. if (statement != null) {
  83. statement.close();
  84. }
  85. if (db != null) {
  86. db.close();
  87. }
  88. } catch (Exception e) {
  89. System.out.println("Could not close the current connection.");
  90. e.printStackTrace();
  91. }
  92. }
  93.  
  94. public void initDB(String url) {
  95. try {
  96. Class.forName(driverName).newInstance();
  97. // Connection con = DriverManager.getConnection(url);
  98. db = DriverManager.getConnection(url,"******","******");
  99. statement = db.createStatement();
  100. System.err.println("The database in initialized");
  101. } catch (Exception e) {
  102. System.err.println("Unable to find and load driver");
  103. System.exit(1);
  104. }
  105. }
  106. }

I omitted the username and password in this sample code for security purposes. I have also included the MySQL driver .jar file into the WEB/Inf folder and I still cannot connect the database into the JTable. Could anyone give me any suggestions to resolve this issue? I am using Eclipse 3.3 Java EE platform.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,525
Reputation: javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light 
Solved Threads: 209
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Need Help in Connecting MySQL Database to JTable

 
0
  #2
Jan 5th, 2009
The driver you are using, seems to be correct.

Have you tried this:
Go to Project Properties > Java Build Path > Libraries. and add the mysql connectivity jar ?

Also can you try to add this:
printstacktrace, inside the catch:

  1. public void initDB(String url) {
  2. try {
  3. Class.forName(driverName).newInstance();
  4. // Connection con = DriverManager.getConnection(url);
  5. db = DriverManager.getConnection(url,"******","******");
  6. statement = db.createStatement();
  7. System.err.println("The database in initialized");
  8. } catch (Exception e) {
  9. System.err.println("Unable to find and load driver");
  10.  
  11. e.printStackTrace();
  12.  
  13. System.exit(1);
  14. }
  15. }
  16. }

And tell us what it prints
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: SSJVEGETA is an unknown quantity at this point 
Solved Threads: 0
SSJVEGETA SSJVEGETA is offline Offline
Newbie Poster

Re: Need Help in Connecting MySQL Database to JTable

 
0
  #3
Jan 5th, 2009
And tell us what it prints
Hi. I did what you say and I got these messages:
Unable to find and load driver
com.mysql.jdbc.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2120)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DamagedGoods$QueryTableModel.initDB(DamagedGoods.java:2897)
at DamagedGoods$QueryTableModel.<init>(DamagedGoods.java:2824)
at DamagedGoods.<init>(DamagedGoods.java:398)
at DamagedGoods.main(DamagedGoods.java:402)
Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:335)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2043)
... 9 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
... 10 more
What am I doing wrong to get this exception?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 3
Reputation: JavaCool4Me is an unknown quantity at this point 
Solved Threads: 0
JavaCool4Me JavaCool4Me is offline Offline
Newbie Poster

Re: Need Help in Connecting MySQL Database to JTable

 
0
  #4
Jan 5th, 2009
can you telnet to mysql? >telnet 127.0.0.1 3306

you might have to look at your mysql install, look at my.cnf, there is a bind-address, make sure it's 127.0.0.1, not your eth0 ip-address
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC