sap
sap

Reputation: 1141

Google web toolkits

Can anyone please suggest a website where I can find step by step instructions on how to add jdbc to a gwt project and how to access data from a database within a gwt project? I'm new to GWT and can't to find any good resources to learn from.

Thanks

Upvotes: 2

Views: 908

Answers (3)

programmer
programmer

Reputation: 127

In that code you can learn how to connect database in gwt with jdbc

public class ExampleServiceImpl  extends RemoteServiceServlet implements ExampleService{

//private Connection con=null;
    private String status;
    private String url="jdbc:mysql://localhost:3306/test";
    private String user="test";
    private String pass = "123456";
    private  Person people;
    private ResultSet resultSet=null;
    private Statement stm=null;
    private Connection con=null;

    private Statement stm2=null;
    private Connection conn2=null;
     private ResultSet resultSet2=null;
    private MySqlConnection conn=new MySqlConnection();


@Override
public Person getPerson(String name,String surname,int password) {
    Person personinfo=new Person();
    personinfo.setName(name);
    personinfo.setSurname(surname);
    personinfo.setPassword(password);

    ResultSet resultSet=null;
    Statement stm=null;
    Connection con=null;
     MySqlConnection conn=new MySqlConnection();     
      con = conn.getConnection();
     try {
        stm = ((Connection) con).createStatement();
    } catch (SQLException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }
     String sorgu = "SELECT * FROM person";
      try {
        resultSet = stm.executeQuery(sorgu);
    } catch (SQLException e3) {
        // TODO Auto-generated catch block
        e3.printStackTrace();
    }
   while(true){
     String sql = "INSERT INTO person " +
               "VALUES ("+ password +", '" + name+ "','" + surname + "')";

     try {
        stm.executeUpdate(sql);
    } catch (SQLException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

     try {
        ((Connection) con).setAutoCommit(false);
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

     try {
        ((Connection) con).commit();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     try {
        stm.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return personinfo;
   }
}

public class MySqlConnection extends RemoteServiceServlet { private static final long serialVersionUID = 1L;

public static Connection con;

public static Connection getConnection()
  {
    try
    {            
      if(con==null)
      {
        Class.forName("com.mysql.jdbc.Driver");  
        String url = "jdbc:mysql://localhost:3306/mysqlconn?user=root&password=123456";
        con= DriverManager.getConnection(url);
      }
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }        
    return con;
  }

  public static void CloseConnection()
  {
    try
    {
       con.close();
       con = null;
    }
    catch (SQLException e)
    {
       e.printStackTrace();
    } 
  }
}

Upvotes: 0

Peter Knego
Peter Knego

Reputation: 80340

GWT is a client side technology - it creates code that runs on a browser. You can not talk to databases directly from browser. You need an intermediary servlet server. Here is what you need to do:

  1. Use GWT-RPC for your GWT code to talk to the servlet server. There are a lot of good tutorials around the web.

  2. Create server-side code that uses JDBC to talk to your database. Ankit already provided you with a link to example: http://code.google.com/p/gwt-examples/wiki/project_MySQLConn

You can also take a direct route and use one of the pre-packaged frameworks that allow you to talk "directly" from GWT to database, where framework provides the intermediate step (GWT-RPC to JDBC): gwtexpress

Upvotes: 2

DonX
DonX

Reputation: 16379

Just follow the google's tutorial itself. GWT is a java framework which converts java code into javascript code on compilation. And please dont confuse with database connection and gwt application. They are totally independent. In gwt application you will be seeing client & server packages. Only classes inside client package will be compiled to javascript code. You have to write jdbc code inside the server package. These codes will not be (can't be) compiled into javascript.

To bring the database data into client side you have to make rpc call.

I hope this information helps you for your basic understanding.

Upvotes: 0

Related Questions