Reputation: 4167
I am new to GWT. I am trying to connect with a mysql server in it.
Following is my project hierarchy:
Project name is hello
and package name is com.hello
.
By default GWT creates 3 folders that are
The main java file that contains entry point for GWT is in the com.hello.client folder by name of hello.java
I have created a class db_conn (file name is db_conn.java) which contains all the required code to connect with mysql database. This file is placed in com.hello.server
folder. Following is the code
package com.hello.server;
import java.sql.Connection;
import java.sql.DriverManager;
public class db_conn
{
public Connection con;
public db_conn()
{
}
public String ConnectToDB()
{
try
{
String host = "localhost";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "pwd";
Class.forName(driver).newInstance();
con = DriverManager.getConnection(host+db, user, pass);
return "Connected to Database";
}
catch(Exception ex)
{
return ex.toString();
}
}
}
In hello.java file (which is in com.hello.client folder and contains the main entry point for GWT) I have imported above class by import com.hello.server.*;
When I am trying to use the ConnectToDB()
function from the db_conn
class on onModuleLoad()
in hello.java, it is giving me the following error:
com.hello.server.db_conn can not be found in source packages. Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly.
The code snippet window shows the db_conn class but when I use this class, it gives me error. I am not able to figure out the exact problem. Please help.
Thanks
Upvotes: 3
Views: 8708
Reputation: 276
I have been facing the same problem. Lucky enough I found a solution for it, it's rather very simple.
Let me tell you the steps
I assume you are using Eclipse IDE, so while creating a GWT project make sure while naming your project you "TICK OFF" the Google APPLICATION engine and let your application run on Google web tool KIT (This solves a bug that comes during database connectivity, something like transitional error).
If you are having an error / exception like (class not found ), make sure that you have imported the SQL connector jar file. Do the following:
goto your: WAR -> WEBINF -> lib -> paste the SQL connector files inside lib, this is going to solve the class not found bug.
Upvotes: 0
Reputation: 20865
Your client files should only contain code that GWT can actually compile in to JavaScript. So you must remove the
import com.hello.server.*
from the client code. What you must do instead is to add a service interface in the client code and then implement this interface in the server.
So your project layout should end up being something like this
client code (compiled in to JavaScript)
com/hello/client/HelloService.java
com/hello/client/HelloServiceAsync.java
RPC code shared between server and client (also compiled into JavaScript)
com/hello/shared/HelloModel.java
Server code that implements HelloService and uses HelloModel (plus references your MySQL connection).
com/hello/server/HelloServiceImpl.java
Upvotes: 8
Reputation: 328556
The client can't talk to the DB directly. Instead it must talk to some interface code (the services) which send commands to the server. The server reads the commands, translates them into DB operations, executes them and then uses the services again to send the results back to the client.
See this tutorial for an example.
Upvotes: 1
Reputation: 13709
Agree with Lars. You really don't want to connect to your database from the client code, for stability and security reasons, among others. Apply "separation of concerns" here. The client should call the server code, and the server code should call the database. So, read on GWT about creating service methods and invoking them from the client.
Upvotes: 1