DecebalICT

Displaying a SQL table with qtjambi

Feb 14th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.33 KB | None | 0 0
  1.     public QTableViewExample(QWidget parent) throws Exception
  2.     {
  3.         super(parent);
  4.  
  5.         Connection         conn;
  6.         String[]           header = { "Quote", "Author"};
  7.         int                i;
  8.         QStandardItemModel model  = new QStandardItemModel();
  9.         ResultSet          rs;
  10.         Statement          stat;
  11.  
  12.         Class.forName("org.h2.Driver");
  13.         conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/~/citaten/quotes", "sa", "");
  14.         stat = conn.createStatement();
  15.  
  16.         setSelectionMode(SelectionMode.ContiguousSelection);
  17.         setSelectionBehavior(SelectionBehavior.SelectRows);
  18.  
  19.         model.setHorizontalHeaderLabels(Arrays.asList(header));
  20.  
  21.         i = 0;
  22.         rs = stat.executeQuery("SELECT quote, author FROM quotes, authors WHERE quotes.authorID = authors.authorID");
  23.         while (rs.next()) {
  24.             QStandardItem quote  = new QStandardItem(rs.getString("quote"));
  25.             QStandardItem author = new QStandardItem(rs.getString("author"));
  26.  
  27.             quote.setEditable(false);
  28.             author.setEditable(false);
  29.             model.setItem(i, 0, quote);
  30.             model.setItem(i, 1, author);
  31.             ++i;
  32.         }
  33.         setModel(model);
  34.         setColumnWidth(0, 750);
  35.         setColumnWidth(1, 175);
  36.         resize(1000, 600);
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment