No meu projeto possuo as Classes ConnectionFactory(Retorna a conexão),Customer(Bean),CustomerDAO(Manipula consultas SQL) ,eu testei essas classes acessando via um classe que implementa o método main e consigo normalmente manipular a base de dados via método main.Mas quando tento fazer a manipulação via o serviço RPC que eu criei, da um ClassNotFoundException como se o driver do bd não tivesse instalado no projeto. Existe algum detalhe a mais sobre acesso a banco de dado via JDBC com o GWT ? Alguém poderia me ajudar ?
Obs.: Estou utilizando o banco Firebird.
A seguir estão os códigos das três classes citadas acima:
public class ConnectionFactory {
public static Connection getConection() {
try {
Class.forName("org.firebirdsql.jdbc.FBDriver");
Driver dv = new org.firebirdsql.jdbc.FBDriver();
Properties prop = new Properties();
prop.put("user", "SYSDBA");
prop.put("password", "masterkey");
return dv.connect("jdbc:firebirdsql:localhost/3050:c:\\gwt.fdb",prop);
} catch (ClassNotFoundException ce) {
System.out.println("Classe não encontrada :"+ce.getMessage());
} catch (SQLException se) {
System.out.println("Caminho inválido :"+se.getMessage());
}
return null;
}
public static void closeConnection(Connection conn, Statement stmt,
ResultSet rs) throws Exception {
close(conn, stmt, rs);
}
public static void closeConnection(Connection conn, Statement stmt)
throws Exception {
close(conn, stmt, null);
}
public static void closeConnection(Connection conn) throws Exception {
close(conn, null, null);
}
private static void close(Connection conn, Statement stmt, ResultSet rs)
throws Exception {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
}
public class Customer {
private int id;
private String firstname;
private String lastname;
private String email;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private boolean male;
private int subscriptions;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
public int getSubscriptions() {
return subscriptions;
}
public void setSubscriptions(int subscriptions) {
this.subscriptions = subscriptions;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Customer() {
}
public Customer(String firstname, String lastname) {
setFirstname(firstname);
setFirstname(lastname);
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import br.com.impar.server.model.Customer;
import br.com.impar.server.uil.ConnectionFactory;
public class CustomerDAO {
public CustomerDAO() throws Exception {
}
private Connection getConnection() throws Exception{
try {
return ConnectionFactory.getConection();
} catch (Exception e) {
throw new Exception("Erro: " + ":\n" + e.getMessage());
}
}
public void salvar(Customer customer) throws Exception {
PreparedStatement ps = null;
Connection conn = this.getConnection();
String SQL = "";
if (customer == null)
throw new Exception("O valor passado não pode ser null");
try {
if(customer.getId()==0){
SQL = "INSERT INTO customers(firstname,lastname,address,email,male)"
+ "values(?,?,?,?,?)";
}else{
if(!this.procurar(customer.getId()).equals(null)){
SQL = "UPDATE customers SET firstname=?," + "lastname=?,"
+ "address=?, email=?,male=? " + "where id=?";
} else {
throw new Exception("Cliente inexistente");
}
}
ps = conn.prepareStatement(SQL);
ps.setString(1, customer.getFirstname());
ps.setString(2, customer.getLastname());
ps.setString(3, customer.getAddress());
ps.setString(4, customer.getEmail());
if (customer.isMale()) {
ps.setString(5, "S");
} else {
ps.setString(5, "N");
}
if(!(customer.getId()==0)){
ps.setInt(6, customer.getId());
}
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception("Erro ao inserir dados" + sqle);
} finally {
ConnectionFactory.closeConnection(conn, ps);
}
}
public void atualizar(Customer customer) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
if (customer == null)
throw new Exception("O valor passado não pode ser null");
try {
String SQL = "UPDATE customer SET firstname=?," + "lastname=?,"
+ "address=?, email=?,male=? " + "where id=?";
conn = ConnectionFactory.getConection();;
ps = conn.prepareStatement(SQL);
ps.setString(1, customer.getFirstname());
ps.setString(2, customer.getLastname());
ps.setString(3, customer.getAddress());
ps.setString(4, customer.getEmail());
if (customer.isMale()) {
ps.setString(5, "S");
} else {
ps.setString(5, "N");
}
ps.setInt(6, customer.getId());
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception("Erro ao atualizar dados" + sqle);
} finally {
ConnectionFactory.closeConnection(conn, ps);
}
}
public List<Customer> todos() throws Exception {
ResultSet rs = null;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = ConnectionFactory.getConection();
ps = conn.prepareStatement("select id,firstname,lastname,address,email,male from customers");
rs = ps.executeQuery();
List<Customer> list = new ArrayList<Customer>();
while (rs.next()) {
Customer customer = new Customer();
customer.setId(rs.getInt(1));
customer.setFirstname(rs.getString(2));
customer.setLastname(rs.getString(3));
customer.setAddress(rs.getString(4));
customer.setEmail(rs.getString(5));
boolean male;
if (rs.getString(6).equals("S")){
male = true;
}else{
male = false;
}
customer.setMale(male);
list.add(customer);
}
return list;
} catch (SQLException sqle) {
throw new Exception(sqle);
} finally {
ConnectionFactory.closeConnection(conn, ps);
}
}
public Customer procurar(Integer id) throws Exception {
PreparedStatement ps = null;
Connection conn = null;
ResultSet rs = null;
try {
conn = this.getConnection();
ps = conn.prepareStatement("select * from customers where id=?");
ps.setInt(1, id);
rs = ps.executeQuery();
if (!rs.next()) {
throw new Exception("Não foi encontrado nenhum"
+ "registro com o ID:" + id);
}
Customer customer = new Customer();
customer.setId(rs.getInt(1));
customer.setFirstname(rs.getString(2));
customer.setLastname(rs.getString(3));
customer.setAddress(rs.getString(4));
customer.setEmail(rs.getString(5));
boolean male;
if (rs.getString(6).equals("S")){
male = true;
}else{
male = false;
}
customer.setMale(male);
return customer;
} catch (SQLException sqle) {
throw new Exception(sqle);
} finally {
ConnectionFactory.closeConnection(conn, ps, rs);
}
}
public void excluir(Customer customer) throws Exception {
PreparedStatement ps = null;
Connection conn = null;
if (customer == null)
throw new Exception("O valor passado não pode ser null");
try {
String SQL = "DELETE from customers where id=?";
conn = this.getConnection();
ps = conn.prepareStatement(SQL);
ps.setInt(1, customer.getId());
ps.executeUpdate();
} catch (SQLException sqle) {
throw new Exception("Erro ao excluir dados:" + sqle);
} finally {
ConnectionFactory.closeConnection(conn, ps);
}
}
}