How to refresh Vaadin table related with SQLContainer using ComboBox? - java

I need help in a simple application based on Vaadin.
I need to have a table bound to SQL query results. SQL query has a parameter which value user chooses from combobox. What I need is to table be refreshed when user change the combobox value.
That is what I have ():
Table table;
JDBCConnectionPool pool;
String query = "select products.product_code, products.name as product_name, clients.client_code, clients.name as client_name from products, clients where products.client_id = clients.id";
FreeformQuery q = new FreeformQuery(query, pool);
SQLContainer container = new SQLContainer(q);
table.setContainerDataSource(container);
So, this simple code selects all data from products and clients tables and puts it to the table. But how can I add filtering by clients.client_id selected from combobox, for example? To implement next query:
select products.product_code, products.name as product_name, clients.client_code, clients.name as client_name from products, clients where products.client_id = clients.id where client_id = ?;
Thank you for your help.

You could add a Property.ValueChangeListener that would change your query parameters:
comboBox.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
String customQuery = query.replace(":clientId", ((Client)(event.getProperty()).getId(), pks);
table.setContainerDataSource(new SQLContainer(new FreeformQuery(customQuery, pool)));
}
});
And the query would hold the following value: select products.product_code, products.name as product_name, clients.client_code, clients.name as client_name from products, clients where products.client_id = clients.id where client_id = :clientId
But be careful with query.replace, if Id is int there is nothing to worrier about, but if it is string, please addSlashes in order to avoid SQLInjection.

Related

NamedParameterJdbcTemplate - Select Id from

I am trying to use Java SQL namedParameterJdbcTemplate to get a List of ID Numbers.
private static final String Customer_Query = "SELECT Customer_Id From dbo.Customers WHERE Customer_Name = :Customer_Name";
MapSqlParameterSource customer_parameters = new MapSqlParameterSource();
customer_parameters.addValue( "Customer_Name", "Joe");
List<Long> Customer_Id = namedParameterJdbcTemplate.query(Customer_Query, customer_parameters);
Error: Cannot resolve method 'query(java.lang.String, org.springframework.jdbc.core.namedparam.MapSqlParameterSource)'
This resource gets the Whole Table row, I just need one column. How can this be done?
NamedParameterJdbcTemplate - Select * from
Trying to be efficient in querying for performance tuning and acquiring data, there are lot of sql columns in the table .

spring boot, jdbcTemplate, Java

I have a query that takes one column row from database and I want to set this data to my model. My model name is Pictures and my method is follow:
#Override
public Pictures getPictureList() throws Exception {
JdbcTemplate jdbc = new JdbcTemplate(datasource);
String sql= "select path from bakery.pictures where id=1";
Pictures pcList = jdbc.query(sql, new BeanPropertyRowMapper<Pictures>(Pictures.class));
return pcList;
}
This method returns "a query was Inferred to list".
How can I solve it?
Use JdbcTemplate.queryForObject() method to retrieve a single row by it's primary key.
Pictures p = jdbc.queryForObject(sql,
new BeanPropertyRowMapper<Pictures>(Pictures.class));
JdbcTemplate.query() will return multiple rows which makes little sense if you are querying by primary key.
List<Pictures> list = jdbc.query(sql,
new BeanPropertyRowMapper<Pictures>(Pictures.class));
Picture p = list.get(0);

Want to Parse raw string coming from Java UI upon user selection to SQL query String?

I am wondering how can I parse raw string coming from webapp Java UI upon user selection to SQL query String in Java !
For Example : On UI, I have 100 companies (and also a database having info about these companies) and if suppose user selected two companies from UI and clicked search button then It should return all information about these two companies comp1 and comp2 and relationships among these two companies and fetch following query from sql database :
The table CompanyData contains all general information about companies like ID, Name, CEO, Stablishment year, Awards won, global ranks, etc....
Table ComapanyDomain contains information about the domains, technologies and expertise of companies,
SELECT * FROM CompanyData
WHERE ID IN (SELECT ID FROM CompnayDomain
WHERE companyName = "comp1"
and ID IN ( SELECT ID FROM CompnayDomain
WHERE companyName = "comp2"))
the string to be parsed It would look like " Company comp1 and comp2 " in above scenario
Thank you
I would approach it like this (I didn't use your sql query because it didn't make any sense at all, it would return always an empty set unless 2 companies have the same ID)
String input = "Company comp1 and comp2";
String data = input.substring(8, input.length());
String [] companies = data.split("\\b and \\b");
String sql = "SELECT * FROM CompanyData WHERE ID IN (SELECT ID FROM CompanyDomain WHERE companyName IN (";
for(int i = 0; i < companies.length - 1; i++) {
sql += "?,"
}
sql += "?)";
for(int i = 0; i < companies.length; i++) {
// Use prepared statement and insert the values here
}

PostgreSQL: Update my user table via Java

In PostgreSQL user is a reserved keyword that is used in an internal table, however I also have a separate user table in my own database that I need to use. Whenever I try to execute INSERT or UPDATE statements on the table, it generates the following error: The column name 'id' was not found in this ResultSet.
This is the Java code I am currently using:
PreparedStatement stat1 = conn.prepareStatement("SELECT id FROM user;");
PreparedStatement stat2 = conn.prepareStatement("UPDATE user SET date_created = ? , last_updated = ? , uuid = ? WHERE id = ?;");
ResultSet rs = stat1.executeQuery();
while(rs.next()){
UUID uuid = UUID.randomUUID();
String tempId = uuid.toString();
stat2.setTimestamp(1, curDate);
stat2.setTimestamp(2, curDate);
stat2.setString(3, tempId);
stat2.setLong(4,rs.getLong("id"));
stat2.executeUpdate();
}
So my question is, how could I insert or update the values in my personal user table without interfering with the keyword restriction?
Use this:
prepareStatement("UPDATE \"user\" set date_created = ?")
Or, better yet, rename your user table to something else, like users:
ALTER TABLE "user" RENAME TO users;
Escape the table name like this
select * from "user";

Java oracle search

I have dropdown menu that has list of column names from certain table in the database and I have textbox where user can type what one is looking for. User first selects the column from dropdown menu and types on textbox what s/he is looking for. I am kind of lost how can i match those dropdown column name and textbox string to look under specific column in oracle database.. Any sample code or suggestion would be helpful.. Thank you
String colname = request.getParameter("colname");//get the column name from teh dropdown
String value = request.getParameter("value");//get the value that the user entered
PreparedStatement searchQuery = null;
String searchString = String.format("Select * from yourtable where %s = ?", colname);
//create a connection , say con
searchQuery = con.prepareStatement(searchString);
searchQuery.setString(1, value);
ReultSet rs = searchQuery.executeQuery();
This solution is only basic a idea so you have to modify to suit. If the columns that you are searching are of different types then you have to cater for that.

Categories

Resources