Created
August 5, 2019 14:48
-
-
Save mstahv/b22fb63791132a3e4b33d2aef17a294b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.annotation.PostConstruct; | |
import javax.sql.DataSource; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import com.vaadin.flow.component.grid.Grid; | |
import com.vaadin.flow.component.orderedlayout.VerticalLayout; | |
import com.vaadin.flow.router.Route; | |
@Route | |
public class MainView extends VerticalLayout { | |
@Autowired | |
DataSource dataSource; | |
private List<String> headers; | |
private List<List<String>> queryResults; | |
@PostConstruct | |
void init() throws SQLException { | |
initExampleDatabase(); | |
runTheQuery(); | |
// now do the actual grid and connect in to the Persons table | |
Grid<List<String>> datagrid = new Grid<>(); | |
for (int i = 0; i < headers.size(); i++) { | |
int columnIndex = i; | |
datagrid.addColumn(listOfStrings -> listOfStrings.get(columnIndex)).setHeader(headers.get(columnIndex)); | |
} | |
datagrid.setItems(queryResults); | |
add(datagrid); | |
} | |
private void runTheQuery() { | |
Connection con = null; | |
Statement pstmt; | |
try { | |
// do the query and read the data to two-dim string array and headers to another | |
// array | |
con = dataSource.getConnection(); | |
pstmt = con.createStatement(); | |
// put your more complex query here | |
ResultSet rs = pstmt.executeQuery("SELECT * FROM Persons"); | |
int columnCount = rs.getMetaData().getColumnCount(); | |
headers = new ArrayList<>(); | |
for (int i = 0; i < columnCount; i++) { | |
String columnName = rs.getMetaData().getColumnName(i + 1); | |
headers.add(columnName); | |
} | |
queryResults = new ArrayList<>(); | |
while (rs.next()) { | |
ArrayList<String> row = new ArrayList<String>(); | |
for (int i = 0; i < columnCount; i++) { | |
row.add(rs.getObject(i + 1).toString()); | |
} | |
queryResults.add(row); | |
} | |
pstmt.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
if (con != null) | |
try { | |
con.close(); | |
} catch (SQLException e) { | |
} | |
} | |
} | |
private void initExampleDatabase() throws SQLException { | |
Connection con = null; | |
PreparedStatement pstmt; | |
try { | |
con = dataSource.getConnection(); | |
con.setAutoCommit(false); | |
pstmt = con.prepareStatement( | |
"CREATE TABLE Persons (PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255),City varchar(255));"); | |
pstmt.executeUpdate(); | |
pstmt.close(); | |
pstmt = con.prepareStatement("INSERT INTO Persons VALUES(1,'Tahvonen','Matti','Ruukinkatu','Turku');"); | |
pstmt.executeUpdate(); | |
pstmt.close(); | |
con.commit(); | |
con.close(); | |
} catch (Exception e) { | |
// NOP, table exists... | |
} finally { | |
if (con != null) | |
con.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment