getting java.sql.Connection error

944 views
Skip to first unread message

phamtranquocviet

unread,
Jan 7, 2009, 11:40:56 PM1/7/09
to Google Web Toolkit
Hi,

I am getting this error:
[ERROR] Line 11: No source code is available for type
java.sql.SQLException; did you forget to inherit a required module?

when trying to check if an entered SSN is a duplicate via RPC. Here is
my RPC implementation:

package com.cadao.server;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.cadao.client.RegistrationService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class RegistrationServiceImpl extends RemoteServiceServlet
implements RegistrationService
{
private static final long serialVersionUID = 1L;

public boolean isDuplicateSsn(String ssn) throws SQLException
{
String sql = "SELECT Ssn " +
"FROM User U JOIN Mem M ON U.UserId = M.UserId " +
"WHERE Ssn = '" + ssn + "'";
CadaoDb cadaoDb = new CadaoDb();
Connection connection = cadaoDb.getConnection();
Statement select = connection.createStatement();
ResultSet resultSet = select.executeQuery(sql);
resultSet.last();
if(resultSet.getRow() > 0)
return true;
return false;
}
}
============================================
package com.cadao.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public abstract class Db
{
private String driver = "com.mysql.jdbc.Driver";

/**
* Constructor
*/
public Db()
{ }

protected abstract String getUrl();
protected abstract String getDb();
protected abstract String getUser();
protected abstract String getPassword();

public final Connection getConnection()
{
String url = getUrl();
String db = getDb();
String user = getUser();
String password = getPassword();
Connection connection = null;
try
{
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url + db, user, password);
}
catch(Exception e)
{
System.err.println("Mysql Connection Error: ");
e.printStackTrace();
}
return connection;
}

protected static int getResultSetSize(ResultSet resultSet)
{
int size = -1;
try
{
resultSet.last();
size = resultSet.getRow();
resultSet.beforeFirst();
}
catch(SQLException e)
{ return size; }
return size;
}
}
======================================
I got this code from here:
http://gwt-examples.googlecode.com/svn/trunk/gwt-test-MySQLConn/src/com/tribling/gwt/test/mysqlconn/server/

Could somebody help me see what's missing? Thanks.

Lothar Kimmeringer

unread,
Jan 8, 2009, 2:55:13 AM1/8/09
to Google-We...@googlegroups.com
phamtranquocviet schrieb:

> public boolean isDuplicateSsn(String ssn) throws SQLException

You can't throw a SQLException from a method of a RemoteServiceServlet.
To be able to transport this exception to the client (where it is passed
as argument to the onFailure-method of the AsyncCallback, it's necessary
to extend it from SerializableException which SQLException clearly isn't.

I solved it that way, that I throw a RemoteServiceException where I add
the stacktrace of the causing exception as String, allowing to show the
error in a usual way (if you want to show that much information to an
user) without the need to create many many different exceptions.


Regards, Lothar

Pham Tran Quoc Viet

unread,
Jan 8, 2009, 1:10:37 PM1/8/09
to Google-We...@googlegroups.com
Thanks Lothar. I'll give it a try tonight and see what happens. Thanks again.
Viet Pham

Pham Tran Quoc Viet

unread,
Jan 9, 2009, 12:10:40 AM1/9/09
to Google-We...@googlegroups.com
Lothar,
Because I had isDuplicateSsn (on server) throw SQLException, I was forced to throw the same exception on RegistrationService, which is on client, which does not understand server language. That was the problem.

I fixed the code to catch SQLException instead of throwing it. This works for now, but I will probably log, then wrap it the way you suggested, and throw it again in a format that is serializable to be sent to client.

Thanks a bunch.

Viet Pham

Pham Tran Quoc Viet

unread,
Jan 9, 2009, 12:28:57 AM1/9/09
to Google-We...@googlegroups.com
Lothar,
By the way, I could not find RemoteServiceException in GWT doc. So, I assume it's your custom exception, right?
Thanks.

Viet Pham

Lothar Kimmeringer

unread,
Jan 9, 2009, 3:28:02 AM1/9/09
to Google-We...@googlegroups.com
Pham Tran Quoc Viet schrieb:

> By the way, I could not find RemoteServiceException in GWT doc. So, I
> assume it's your custom exception, right?

That's correct. But it's nothing very secret happening there so I
can post it here without being sent to the south-east-end of Cuba ;-)

------snip
/**
* Exception that will be thrown if there was an error while performing
* remote functions on the server
* @author "Lothar Kimmeringer" <lot...@kimmeringer.de>
*
*/
public class RemoteServiceException extends SerializableException {
private String stackTrace;
private String message;

private static final long serialVersionUID = 2L;

/**
* Creates a new instance of RemoteServiceException
*/
public RemoteServiceException() {
super();
}

/**
* Creates a new instance of RemoteServiceException
* @param message The message indicating the reason for the exception
* @param cause The cause leading to the exception
*/
public RemoteServiceException(String message, String cause) {
super(message);
this.message = message;
stackTrace = cause;
}

/**
* Creates a new instance of RemoteServiceException
* @param message The message indicating the reason for the exception
*/
public RemoteServiceException(String message) {
super(message);
this.message = message;
}

/**
* Returns the stacktrace of the cause
* @return The cause
*/
public String getCauseStackTrace() {
return stackTrace;
}

/**
* Returns the message of the exception
*/
public String getMessage(){
return message;
}
}
------snip

You can get the stacktrace as string doing the following:

StringWriter sw = new StringWriter();
throwable.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();

You have to do it outside the above exception, because the exception above
must be in a client-package, i.e. you are restricted to the GWT Java
ruling out PrintWriter at least.


Regards, Lothar

Pham Tran Quoc Viet

unread,
Jan 9, 2009, 3:22:57 PM1/9/09
to Google-We...@googlegroups.com
Thanks for taking the time to explain it so carefully. You are a good person.
Thanks again, Lothar.

Viet Pham
Reply all
Reply to author
Forward
0 new messages