my function cant get called in JavaFx - java

I have created a list view in FXML with the id of "lwAllUserGrp" I can use that id and the following function to populate it :
public void populateAllUserList() {
lwAllUserGrp.getItems().clear();
///////////////////////////////////////////////
String filter = txtSearch.getText();
String Query = "Select user_name, user_login_name, user_state from dm_user where r_is_group = 0 and user_state = 0 and (user_name not like 'dm%' and user_name not like 'svc%' and user_name not like 'lexo%' ) order by user_name";
ArrayList<String> AllUser = GetDataWithDql(_session, Query, "user_name");
for (String x : AllUser) {
if (x.toLowerCase().contains(filter.toLowerCase())){
lwAllUserGrp.getItems().add(x);
}
}
}
But when I add the following function to see which row is selected :
public void selectedItemFromListView(){
// System.out.println("blabla");
selected = lwAllUserGrp.getSelectionModel().getSelectedItem();
// System.out.println(selected);
String Query = "select user_name , user_state , user_address, user_login_name , user_web_page from dm_user where user_name ='#aclname'";
Query = Query.replace("#aclname",selected );
ArrayList<User> allUserNames = GetDataWithDqlpro(_session, Query, "user_name","user_address","user_state","user_login_name","user_web_page");
for (int i = 0 ; i < allUserNames.size() ; i++ ){
if (selected.compareToIgnoreCase(allUserNames.get(i).getUsername() ) == 0){
username.setText(selected);
login_Name.setText(allUserNames.get(i).getuserLoginName());
state.setText(allUserNames.get(i).getState());
address.setText(allUserNames.get(i).getAddress());
WP.setText(allUserNames.get(i).getuserWP());
}
}
}
my function doesnt get called at all ? any one knows the reason why ?

If you aren't calling the function, it isnt going to call itself :) You'd have to add it as a listener or whatever. In your case,, you were missing the listener assignment in your FXML (I had a hunch that's why i asked). As you discovered in comments, adding the appropriate onMouseClicked specifier in your FXML should take care of it.

Related

How to find the selected id in my List<String> ids arraylist?

Here is my code. I am trying to use JUnit to test the deleteUsers() method, but everytime I write my test, it deletes all the users that I have in the database. How can i delete a single user? Below is the code for the method and for the test.
#Override
public boolean deleteUsers(List<String> ids) throws Exception {
StringBuilder sql = new StringBuilder();
sql.append("delete from user where ");
for (String id : ids) {
sql.append(" id = ? or");
}
String strSql = sql.toString().substring(0, sql.length() - 2) + ";";
PreparedStatement preparedStatement = this.connection.prepareStatement(strSql);
for (int i = 0; i < ids.size(); i++) {
preparedStatement.setInt(1 + i, Integer.parseInt(ids.get(i)));
}
int lines = preparedStatement.executeUpdate();
preparedStatement.close();
return lines > 0;
}
You're missing a check for empty input. In your test you pass an empty list to deleteUsers which results in this SQL statement:
delete from user wher;
I'd expect that the DBMS would reject this as invalid SQL but perhaps there are some where this is interpreted as delete from user which simply deletes all users. (As #SteveBosman pointed out the wher is interpreted as table alias as it is - due to the missing last e - no reserved word anymoere)
Basically you have 2 options. Either deleting all users by passing an empty list is a valid use case - in which case you should handle it properly by producing proper SQL. Or this is not expected and you should adapt your code to throw an Exception if ids is empty.
#Override
public boolean deleteUsers(List<String> ids) throws Exception {
if (ids == null || ids.size() == 0) {
throw new IllegalArgumentException("List of IDs must not be empty");
}
...
}
You could of course return false in case of an empty input as well to indicate no users were deleted.
To pass values to the deleteUsers method in your test you need to add values to the used list:
userDAOImpl.addUser("admin3", "111222");
final List<String> idsToDelete = new ArrayList<>();
idsToDelete.add("111222");
userDAOImpl.deleteUsers(idsToDelete);
The problem is caused by how the SQL is built. When deleteUsers is passed an empty list then the generated SQL will be:
delete from user wher
which will result in all data being deleted (the table user is given the alias "wher"). I highly recommend checking at the start of the method if the collection is empty and either raising an exception or returning.
Add the following check
if (ids == null || ids.isEmpty()) {
throw new IllegalArgumentException("ids must not be empty");
}
StringBuilder sql = new StringBuilder();
sql.append("delete from user where");
String orClause = "";
for (String id : ids) {
sql.append(orClause);
sql.append(" id = ?");
orClause = " or";
}

In the table i need to store the values of the Id which is selected but not able to do that Any Leads

Scenario:
I select Two Ids and gets added in the Table which can been seen in the below screen shot.
Over Here i need to Store the value in a Method and then call that method in other Place.
The Problem is iam not able to get the Values .
Code:
public boolean checkselectedDemandId()
{
String cDemand = "";
cDemand = "//table//tr//td[contains(#class,'mat-column-demandId')]";
List<WebElement> rows = driver.findElements(By.xpath(cDemand));`
for(WebElement row:rows)`
{if(cDemand.length()>0)
{
selectedDemandId = cDemand;
return true;
}`else
{
return false;
}
public String getselectedCreatedDemandId()
{
return selectedDemandId;
}
HTML of the Table:
4384SELTESTTECHMTESSINGAPOREHONGKONG2030-04-092040-06-152055-12-30delete 4383SELTESTTECHMTESSINGAPOREHONGKONG2030-04-092040-06-152055-12-30delete
If you are sure about number of rows, use the following code
List<WebElement> rows = driver.findElements(By.xpath("//table//tr//td[contains(#class,'mat-column-demandId')]"));
String rowOne = rows.get(0).getText();
String rowTwo = rows.get(1).getText();

SQL Update in another class

Thanks for helping me out with my problem.
So I tried to insert a SQL Update using a method in a class named Functions, the main program doesn't give any error, and whenever I use the function, it just don't change anything in the SQL table after refreshing.
Here is my updateSQL method in class Functions :
public static void updateSQL(String sql, String updatenames[])
{
Connection connected = null;
PreparedStatement prepared = null;
connected = connexionmysql.connexionDB();
try
{
prepared = connected.prepareStatement(sql);
for(int i = 1; i < updatenames.length+1 ; i++) {
prepared.setString(i, updatenames[i-1]);
}
prepared.execute();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
And here is how I call this function in my main class :
String datan[] = { inputPASS, type1, inputEMAIL, inputNAME };
Functions.updateSQL("UPDATE users SET userPASS = ? ,userTYPE = ? ,userEMAIL = ? WHERE userNAME = ? ", datan);
I did solve the problem. The functions I did write are working as expected, but the problem was in the way I was calling the method.
Instead of calling the function in a static way, I was calling it in an "usual" way so Eclipse didn't give me any error in that line.

Bugged by de'bug

An unexpected token error occurs near the Class Database, yet everything in the syntax looks fine. I guess I made the error in the way I called the value from the class?
import de.bezier.data.sql.*;
PostgreSQL pgsql;
Database
void setup()
{
size( 100, 100 );
String user = "user";
String pass = "pass";
String database = "db";
pgsql = new PostgreSQL( this, "127.0.0.1", database, user, pass );
println ("ok");
}
void draw()
{
val1.update();
}
Token error here
Class Database
{
Float val;
database (Float col) {
val = col;
}
void update( )
{
//sets up database
pgsql = new PostgreSQL( this, "127.0.0.1", database, user, pass );
if ( pgsql.connect() )
{
pgsql.query( "SELECT col FROM table ORDER BY col DESC LIMIT 1; " );
return( pgsql.getFloat("col") );
}
else
{
return float (col = 0);
}
}
}
Some text here....
This line
pgsql = new PostgreSQL( this, "127.0.0.1", database, user, pass )
is invoking a constructor. From the documentation you need to extend processing.core.PApplet. "this" refers to the current instance of "this" class.

Challenging token error

I improved the structure after a good observation from [http://stackoverflow.com/users/1690199/v-k] I am still getting a token error even though the syntax looks correct to me. More comments and critiques will be useful and acknowledged here.
import de.bezier.data.sql.*;
PostgreSQL pgsql;
Float val;
void setup()
{
size( 100, 100 );
println(val);
}
Token error identified in Processing 2 at Class Database.
Class Database
{
String user = "user";
String pass = "pass";
String database = "db";
Float val;
Database (Float col) {
val = col;
}
void database_connection( col )
{
//sets up database
pgsql = new PostgreSQL( this, "127.0.0.1", database, user, pass );
if ( pgsql.connect() )
{
pgsql.query( "SELECT col FROM table ORDER BY col DESC LIMIT 1; " );
return( pgsql.getFloat("col") );
}
else
{
println ("failed to connect to the database");
}
}
}
OLD ISSUE: Class structure addressed after a great observation from [http://stackoverflow.com/users/1690199/v-k]
import de.bezier.data.sql.*;
.....
.....
Old code removed for clarity of this issue.
Classes don't take arguments. Also it is class not Class... Am i missing something? Look, a general sample:
class Database {
String user = "user";
String pass = "pass";
String database = "db";
float val; //by convention no Caps for vars...
// a constructor, which get partameters
Database (float v) {
val = v;
}
// a method
void database_setup() {
//whateverq
}
}//end of Database class
First, you should create a new question if you have a second question. Second, you never create the variable pgsql, you just start using it immediately. Move this line:
PostgreSQL pgsql;
to this group of lines:
String user = "user";
String pass = "pass";
String database = "db";
float val;
It's a variable that gets used in that class, so put it in that class. Also, use "float" with a lowercase "f" :)

Categories