-
Java (
http://www.daniweb.com/forums/forum9.html)
| SSJVEGETA | Jan 5th, 2009 6:30 am | |
| Need Help in Connecting MySQL Database to JTable 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:
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. |
| javaAddict | Jan 5th, 2009 7:15 am | |
| Re: Need Help in Connecting MySQL Database to JTable 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:
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 |
| SSJVEGETA | Jan 5th, 2009 7:02 pm | |
| Re: Need Help in Connecting MySQL Database to JTable Quote: And tell us what it prints | Hi. I did what you say and I got these messages:
Quote: 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? |
| JavaCool4Me | Jan 5th, 2009 8:39 pm | |
| Re: Need Help in Connecting MySQL Database to JTable 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 |
| All times are GMT -4. The time now is 5:12 pm. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC