| | |
Need Help in Connecting MySQL Database to JTable
![]() |
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 0
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:
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.
Java Syntax (Toggle Plain Text)
class QueryTableModel extends AbstractTableModel { Vector cache; int ColCount; String[] headers; Connection db=null; Statement statement; String driverName="com.mysql.jdbc.Driver"; //String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //String driverName = "com.jnetdirect.jsql.JSQLDriver"; //String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String SQLLoadDatabaseQuery = "SELECT * FROM HouseHoldApplianceDatabase"; String URL = "jdbc:mysql://localhost:3306/DatabaseName"; // String serverName = "127.0.0.1"; // String portNumber = "1433"; //String mydatabase = serverName + ":" + portNumber; //String currentURL = "jdbc:JSQLConnect://" + mydatabase; //String username = "username"; // String password = "password"; public QueryTableModel() { cache = new Vector(); closeDB(); initDB(URL); loadDB(); } public String getColumnName(int i) { return headers[i]; } public int getColumnCount() { return ColCount; } public int getRowCount() { return cache.size(); } public Object getValueAt(int row, int col) { return ((String[]) cache.elementAt(row))[col]; } public void loadDB() { cache = new Vector(); try { ResultSet rs = statement.executeQuery(SQLLoadDatabaseQuery); ResultSetMetaData meta = rs.getMetaData(); ColCount = meta.getColumnCount(); headers = new String[ColCount]; for (int h = 1; h <= ColCount; h++) { headers[h - 1] = meta.getColumnName(h); } while (rs.next()) { String[] record = new String[ColCount]; for (int i = 0; i < ColCount; i++) { record[i] = rs.getString(i + 1); } cache.addElement(record); } fireTableChanged(null); } catch (Exception e) { e.printStackTrace(); System.err.println("Database still can't be loaded"); } } public void closeDB() { try { if (statement != null) { statement.close(); } if (db != null) { db.close(); } } catch (Exception e) { System.out.println("Could not close the current connection."); e.printStackTrace(); } } public void initDB(String url) { try { Class.forName(driverName).newInstance(); // Connection con = DriverManager.getConnection(url); db = DriverManager.getConnection(url,"******","******"); statement = db.createStatement(); System.err.println("The database in initialized"); } catch (Exception e) { System.err.println("Unable to find and load driver"); System.exit(1); } } }
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.
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:
And tell us what it prints
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:
Java Syntax (Toggle Plain Text)
public void initDB(String url) { try { Class.forName(driverName).newInstance(); // Connection con = DriverManager.getConnection(url); db = DriverManager.getConnection(url,"******","******"); statement = db.createStatement(); System.err.println("The database in initialized"); } catch (Exception e) { System.err.println("Unable to find and load driver"); e.printStackTrace(); System.exit(1); } } }
And tell us what it prints
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
And tell us what it prints
•
•
•
•
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Finding open ports
- Next Thread: PrimeNumbers.java
| Thread Tools | Search this Thread |
2dgraphics @param account affinetransform android api apple applet application arc arguments array automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image inheritance integer j2me java java.xls javadesktopapplications javaprojects jlabel jni jpanel julia keytool keyword linux list macintosh map method methods midlethttpconnection mobile monitoring netbeans nullpointerexception object open-source pong problem producer program project projectideas property reference replaysolutions ria rim scanner server set size sms sourcelabs splash sql swing terminal threads transforms tree ui unicode validation web windows






