2016-05-06 9 views
0

SQLデータベースに接続してテーブルに出力するVaadinプロジェクトがあります。 "sqlGrid.setEditorEnabled(true);"を追加しました。テーブルを編集することはできません。セルをクリックしたり、Enterキーを押すと、データが入力されなくなります。ここに私のコードです:テーブルをVaadinで編集する方法

package com.example.testyui; 

import java.sql.ResultSet; 
import java.sql.ResultSetMetaData; 
import java.sql.SQLException; 
import java.text.NumberFormat; 
import java.util.Properties; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ScheduledFuture; 
import java.util.concurrent.TimeUnit; 

import javax.servlet.annotation.WebServlet; 

import com.evolt.data.connection.ConnectionHelper; 
import com.txfb.bai.jtds.jdbc.JtdsConnection; 
import com.vaadin.annotations.Push; 
import com.vaadin.annotations.Theme; 
import com.vaadin.annotations.VaadinServletConfiguration; 
import com.vaadin.data.util.sqlcontainer.SQLContainer; 
import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; 
import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; 
import com.vaadin.data.util.sqlcontainer.query.FreeformQuery; 
import com.vaadin.data.util.sqlcontainer.query.FreeformQueryDelegate; 
import com.vaadin.data.util.sqlcontainer.query.TableQuery; 
import com.vaadin.data.util.sqlcontainer.query.generator.MSSQLGenerator; 
import com.vaadin.server.VaadinRequest; 
import com.vaadin.server.VaadinServlet; 
import com.vaadin.ui.Grid; 
import com.vaadin.ui.HorizontalLayout; 
import com.vaadin.ui.Label; 
import com.vaadin.ui.UI; 
import com.vaadin.ui.VerticalLayout; 
import com.vaadin.ui.renderers.NumberRenderer; 
import com.vaadin.ui.renderers.ProgressBarRenderer; 

//creates new layout and adds a grid to it 
@SuppressWarnings("serial") 
@Theme("valo") 
@Push() 
public class TestyUI extends UI { 

    //set connection pool variable 
    SimpleJDBCConnectionPool connectionPool; 
    SQLContainer container; 

    @WebServlet(value = "/*", asyncSupported = true) 
    @VaadinServletConfiguration(productionMode = false, ui = TestyUI.class) 
    public static class Servlet extends VaadinServlet { 
    } 

    @Override 
    protected void init(VaadinRequest request) { 
     //calls method from ConnectionHelper 
     connectionPool = new ConnectionHelper().getConnectionPool(); 

     // declare new vertical layout 
     final VerticalLayout layout = new VerticalLayout(); 



     // Declare grid variable 
//  Grid grid = new Grid("Data Grid"); 

     //Sql Grid 
     Grid sqlGrid = new Grid("Users from TXS9316135\\M12QA"); 
     layout.addComponent(sqlGrid); 
     sqlGrid.isImmediate(); 
     sqlGrid.isEnabled(); 
     sqlGrid.setVisible(true); 
     sqlGrid.setSizeFull(); 
     sqlGrid.setEditorEnabled(true); 
     sqlGrid.isEditorActive(); 



     // add layout to UI; add grid to layout; 
     //layout.addComponent(grid); 

     layout.setExpandRatio(sqlGrid, 1); 
     layout.setSizeFull(); 


     //Static Populate 
//  try { 
//   ResultSet rs = ConnectionHelper.getConnection().createStatement().executeQuery("SELECT * FROM EVOLT.DBO.EVOLTTESTGROUPS;"); 
//  
//   ResultSetMetaData md = rs.getMetaData(); 
//   
//   for (int i = 1; i <= md.getColumnCount(); i++) { 
//    sqlGrid.addColumn(md.getColumnName(i)); 
//   } 
//   
//   while (rs.next()) { 
//    String[] values = new String[md.getColumnCount()]; 
//    for (int i = 1; i <= md.getColumnCount(); i++) { 
//     values[i - 1] = rs.getString(i); 
//    } 
//    sqlGrid.addRow(values); 
//   } 
//  } catch (SQLException e) { 
//   // TODO Auto-generated catch block 
//   e.printStackTrace(); 
//  } 

     //SqlContainer Populate 
     TableQuery tq = new TableQuery("ABC", "dbo", "ABCUSERCOM", connectionPool, new MSSQLGenerator()); 
//  FreeformQuery ff = new FreeformQuery("SELECT * FROM ABC.DBO.ABCUSERCOM;", connectionPool); 
//  tq.setVersionColumn("UPDATETIMESTAMP"); 
     try { 
      container = new SQLContainer(tq); 
      sqlGrid.setContainerDataSource(container); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // adds layout and therefore grid to ui/layout 
     this.setContent(layout); 

     // TableQuery tq = new TableQuery("EVOLT", "dbo", "EVOLTTESTGROUPS", 
     // connHelper.getConnectionPool(), new MSSQLGenerator()); 
     // tq.setVersionColumn("OPTLOCK"); 
     // 
     // SQLContainer container = new SQLContainer(tq); 

     ScheduledFuture<?> future = Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() { 
      public void run() { 
       access(new Runnable() { 
        public void run() { 
         container.refresh(); 
        } 
       }); 
      } 
     }, 0, 500, TimeUnit.MILLISECONDS); 

    } 
} 

ありがとうございました。

答えて

0

私はあなたのコードをMySQLデータベースでテストしており、セルを編集することができます。 これは私が使ったinitメソッドです。 私が変更したのは接続プールの作成だけです。 問題はあなたのConnectionプールに関係していると思います。 お手伝いをしてください。 scheduleAtFixedRate(0)フィールドへの更新を行うために非常に少し時間が可能(500ミリ秒)が速すぎるグリッドをリフレッシュするためにプールされたことが判明

protected void init(VaadinRequest request) { 
     //calls method from ConnectionHelper 
     JDBCConnectionPool pool =null; 
     try { 
      pool = new SimpleJDBCConnectionPool(
        "com.mysql.jdbc.Driver", 
        "jdbc:mysql://127.0.0.1:3306/dbname?autoReconnect=true", "user", "pwd", 2, 5); 
     } catch (SQLException sqlEx) { 
      sqlEx.printStackTrace(); 
     } 

     // declare new vertical layout 
     final VerticalLayout layout = new VerticalLayout(); 



     // Declare grid variable 
//  Grid grid = new Grid("Data Grid"); 

     //Sql Grid 
     Grid sqlGrid = new Grid("Users from TXS9316135\\M12QA"); 
     layout.addComponent(sqlGrid); 
     sqlGrid.isImmediate(); 
     sqlGrid.isEnabled(); 
     sqlGrid.setVisible(true); 
     sqlGrid.setSizeFull(); 
     sqlGrid.setEditorEnabled(true); 
     sqlGrid.isEditorActive(); 



     // add layout to UI; add grid to layout; 
     //layout.addComponent(grid); 

     layout.setExpandRatio(sqlGrid, 1); 
     layout.setSizeFull(); 


     //Static Populate 
//  try { 
//   ResultSet rs = ConnectionHelper.getConnection().createStatement().executeQuery("SELECT * FROM EVOLT.DBO.EVOLTTESTGROUPS;"); 
// 
//   ResultSetMetaData md = rs.getMetaData(); 
// 
//   for (int i = 1; i <= md.getColumnCount(); i++) { 
//    sqlGrid.addColumn(md.getColumnName(i)); 
//   } 
// 
//   while (rs.next()) { 
//    String[] values = new String[md.getColumnCount()]; 
//    for (int i = 1; i <= md.getColumnCount(); i++) { 
//     values[i - 1] = rs.getString(i); 
//    } 
//    sqlGrid.addRow(values); 
//   } 
//  } catch (SQLException e) { 
//   // TODO Auto-generated catch block 
//   e.printStackTrace(); 
//  } 

     //SqlContainer Populate 
     TableQuery tq = new TableQuery("tablename", pool); 
//  FreeformQuery ff = new FreeformQuery("SELECT * FROM ABC.DBO.ABCUSERCOM;", connectionPool); 
//  tq.setVersionColumn("UPDATETIMESTAMP"); 
     try { 
      container = new SQLContainer(tq); 
      sqlGrid.setContainerDataSource(container); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // adds layout and therefore grid to ui/layout 
     this.setContent(layout); 

     // TableQuery tq = new TableQuery("EVOLT", "dbo", "EVOLTTESTGROUPS", 
     // connHelper.getConnectionPool(), new MSSQLGenerator()); 
     // tq.setVersionColumn("OPTLOCK"); 
     // 
     // SQLContainer container = new SQLContainer(tq); 

     ScheduledFuture<?> future = Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() { 
      @Override 
      public void run() { 
       access(new Runnable() { 
        @Override 
        public void run() { 
         container.refresh(); 
        } 
       }); 
      } 
     }, 0, 500, TimeUnit.MILLISECONDS); 

    } 
+0

私はそれを理解したかもしれないと思います。プッシュと関係があります。この行は0.5秒でグリッドをリフレッシュしています:}、0、500、TimeUnit.MILLISECONDS);私はそれを500から5000に変更し、更新する前に変更とコミットを行うことができます。 @oscar - bcn:ありがとう、あなたは素早い応答と入力です。 – PeachesToad

0

ScheduledFuture<?> future = Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() { 
     public void run() { 
      access(new Runnable() { 
       public void run() { 
        container.refresh(); 
       } 
      }); 
     } 
    }, 0, 50000, TimeUnit.MILLISECONDS); 
関連する問題