populate Second Combobox with first combobox String value in Sql





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I want to pass a String value of a First ComboBox in a Query to populate a Second ComboBox, therefore the condition in the query is this: SELECT NIT FROM ENTIDAD WHERE NOMBRE='+FirstComboboxStringValue+', so I have two Methods, in the first one I populate Name Value, in the Second one a Nit values, I need to pass the Name value from the first ComboBoxin that query, there are the methods:



public void llenadocombobox2() {
Connection conn=null;
try {
ObservableList<String> listacombonombre= FXCollections.observableArrayList();
String consulta = "select nombre from entidad";
conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
PreparedStatement ps =conn.prepareStatement(consulta);
ResultSet rs = ps.executeQuery();
while ( rs.next() )
{
listacombonombre.add(rs.getString("nombre"));
}
entidad.setItems(listacombonombre);
} catch (SQLException e) {
e.printStackTrace();
}
}


public void llenadocombobox3() {
llenadocombobox2();
String FirstComboboxStringValue= entidad.getSelectionModel().getSelectedItem();
Connection conn=null;
try {
ObservableList<String> listacombonit= FXCollections.observableArrayList();
String consulta = "select nit from entidad where nombre='"+FirstComboboxStringValue+"'";
conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
PreparedStatement ps =conn.prepareStatement(consulta);
ResultSet rs = ps.executeQuery();
while ( rs.next() )
{
listacombonit.add(rs.getString("nit"));
}

nit.setItems(listacombonit);

} catch (SQLException e) {
e.printStackTrace();
}

}


if anyone can give an orientation here could be helpful. regards.










share|improve this question

























  • You should create an MCVE demoing what you have. How to create a Minimal, Complete, and Verifiable example.

    – Sedrick
    Nov 16 '18 at 16:23











  • Also, have a look at this project to get a better understanding of how to separate your database logic from the rest of your code. It will make your code easier to read and refactor.

    – Sedrick
    Nov 16 '18 at 16:32











  • Sedrick but that's the code i need

    – Sistem07
    Nov 16 '18 at 16:42


















1















I want to pass a String value of a First ComboBox in a Query to populate a Second ComboBox, therefore the condition in the query is this: SELECT NIT FROM ENTIDAD WHERE NOMBRE='+FirstComboboxStringValue+', so I have two Methods, in the first one I populate Name Value, in the Second one a Nit values, I need to pass the Name value from the first ComboBoxin that query, there are the methods:



public void llenadocombobox2() {
Connection conn=null;
try {
ObservableList<String> listacombonombre= FXCollections.observableArrayList();
String consulta = "select nombre from entidad";
conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
PreparedStatement ps =conn.prepareStatement(consulta);
ResultSet rs = ps.executeQuery();
while ( rs.next() )
{
listacombonombre.add(rs.getString("nombre"));
}
entidad.setItems(listacombonombre);
} catch (SQLException e) {
e.printStackTrace();
}
}


public void llenadocombobox3() {
llenadocombobox2();
String FirstComboboxStringValue= entidad.getSelectionModel().getSelectedItem();
Connection conn=null;
try {
ObservableList<String> listacombonit= FXCollections.observableArrayList();
String consulta = "select nit from entidad where nombre='"+FirstComboboxStringValue+"'";
conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
PreparedStatement ps =conn.prepareStatement(consulta);
ResultSet rs = ps.executeQuery();
while ( rs.next() )
{
listacombonit.add(rs.getString("nit"));
}

nit.setItems(listacombonit);

} catch (SQLException e) {
e.printStackTrace();
}

}


if anyone can give an orientation here could be helpful. regards.










share|improve this question

























  • You should create an MCVE demoing what you have. How to create a Minimal, Complete, and Verifiable example.

    – Sedrick
    Nov 16 '18 at 16:23











  • Also, have a look at this project to get a better understanding of how to separate your database logic from the rest of your code. It will make your code easier to read and refactor.

    – Sedrick
    Nov 16 '18 at 16:32











  • Sedrick but that's the code i need

    – Sistem07
    Nov 16 '18 at 16:42














1












1








1








I want to pass a String value of a First ComboBox in a Query to populate a Second ComboBox, therefore the condition in the query is this: SELECT NIT FROM ENTIDAD WHERE NOMBRE='+FirstComboboxStringValue+', so I have two Methods, in the first one I populate Name Value, in the Second one a Nit values, I need to pass the Name value from the first ComboBoxin that query, there are the methods:



public void llenadocombobox2() {
Connection conn=null;
try {
ObservableList<String> listacombonombre= FXCollections.observableArrayList();
String consulta = "select nombre from entidad";
conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
PreparedStatement ps =conn.prepareStatement(consulta);
ResultSet rs = ps.executeQuery();
while ( rs.next() )
{
listacombonombre.add(rs.getString("nombre"));
}
entidad.setItems(listacombonombre);
} catch (SQLException e) {
e.printStackTrace();
}
}


public void llenadocombobox3() {
llenadocombobox2();
String FirstComboboxStringValue= entidad.getSelectionModel().getSelectedItem();
Connection conn=null;
try {
ObservableList<String> listacombonit= FXCollections.observableArrayList();
String consulta = "select nit from entidad where nombre='"+FirstComboboxStringValue+"'";
conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
PreparedStatement ps =conn.prepareStatement(consulta);
ResultSet rs = ps.executeQuery();
while ( rs.next() )
{
listacombonit.add(rs.getString("nit"));
}

nit.setItems(listacombonit);

} catch (SQLException e) {
e.printStackTrace();
}

}


if anyone can give an orientation here could be helpful. regards.










share|improve this question
















I want to pass a String value of a First ComboBox in a Query to populate a Second ComboBox, therefore the condition in the query is this: SELECT NIT FROM ENTIDAD WHERE NOMBRE='+FirstComboboxStringValue+', so I have two Methods, in the first one I populate Name Value, in the Second one a Nit values, I need to pass the Name value from the first ComboBoxin that query, there are the methods:



public void llenadocombobox2() {
Connection conn=null;
try {
ObservableList<String> listacombonombre= FXCollections.observableArrayList();
String consulta = "select nombre from entidad";
conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
PreparedStatement ps =conn.prepareStatement(consulta);
ResultSet rs = ps.executeQuery();
while ( rs.next() )
{
listacombonombre.add(rs.getString("nombre"));
}
entidad.setItems(listacombonombre);
} catch (SQLException e) {
e.printStackTrace();
}
}


public void llenadocombobox3() {
llenadocombobox2();
String FirstComboboxStringValue= entidad.getSelectionModel().getSelectedItem();
Connection conn=null;
try {
ObservableList<String> listacombonit= FXCollections.observableArrayList();
String consulta = "select nit from entidad where nombre='"+FirstComboboxStringValue+"'";
conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-4JA6SFR:1433;databaseName=GLOSASNINO", "sa", "123");
PreparedStatement ps =conn.prepareStatement(consulta);
ResultSet rs = ps.executeQuery();
while ( rs.next() )
{
listacombonit.add(rs.getString("nit"));
}

nit.setItems(listacombonit);

} catch (SQLException e) {
e.printStackTrace();
}

}


if anyone can give an orientation here could be helpful. regards.







java javafx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 16:33







Sistem07

















asked Nov 16 '18 at 16:09









Sistem07Sistem07

133




133













  • You should create an MCVE demoing what you have. How to create a Minimal, Complete, and Verifiable example.

    – Sedrick
    Nov 16 '18 at 16:23











  • Also, have a look at this project to get a better understanding of how to separate your database logic from the rest of your code. It will make your code easier to read and refactor.

    – Sedrick
    Nov 16 '18 at 16:32











  • Sedrick but that's the code i need

    – Sistem07
    Nov 16 '18 at 16:42



















  • You should create an MCVE demoing what you have. How to create a Minimal, Complete, and Verifiable example.

    – Sedrick
    Nov 16 '18 at 16:23











  • Also, have a look at this project to get a better understanding of how to separate your database logic from the rest of your code. It will make your code easier to read and refactor.

    – Sedrick
    Nov 16 '18 at 16:32











  • Sedrick but that's the code i need

    – Sistem07
    Nov 16 '18 at 16:42

















You should create an MCVE demoing what you have. How to create a Minimal, Complete, and Verifiable example.

– Sedrick
Nov 16 '18 at 16:23





You should create an MCVE demoing what you have. How to create a Minimal, Complete, and Verifiable example.

– Sedrick
Nov 16 '18 at 16:23













Also, have a look at this project to get a better understanding of how to separate your database logic from the rest of your code. It will make your code easier to read and refactor.

– Sedrick
Nov 16 '18 at 16:32





Also, have a look at this project to get a better understanding of how to separate your database logic from the rest of your code. It will make your code easier to read and refactor.

– Sedrick
Nov 16 '18 at 16:32













Sedrick but that's the code i need

– Sistem07
Nov 16 '18 at 16:42





Sedrick but that's the code i need

– Sistem07
Nov 16 '18 at 16:42












1 Answer
1






active

oldest

votes


















0














Here is a demo that covers your problem. You should query the DB based on the first ComboBox selection. Use the results from the query to populate the second ComboBox. <- This part should happen in the first ComboBox valueProperty change listener.



import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ComboBoxExperiments extends Application
{

@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("ComboBox Experiment 1");

ComboBox<String> comboBox = new ComboBox();
comboBox.getItems().add("Choice 1");
comboBox.getItems().add("Choice 2");
comboBox.getItems().add("Choice 3");

ComboBox<String> comboBox2 = new ComboBox();

comboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
comboBox2.getItems().setAll(queryFakeDB(newValue));
comboBox2.getSelectionModel().selectFirst();
});

comboBox.getSelectionModel().selectFirst();
comboBox2.getSelectionModel().selectFirst();

VBox vBox = new VBox(comboBox, comboBox2);
Scene scene = new Scene(vBox, 200, 120);
primaryStage.setScene(scene);
primaryStage.show();

}

public static void main(String args)
{
Application.launch(args);
}

public List<String> queryFakeDB(String comboBox1Selection)
{
//Connect to fake database
//Query fake database
//Store results in a List
//This simulates getting results from db based on combobox1 selection
List<String> results = new ArrayList();
switch (comboBox1Selection) {
case "Choice 1":
results.add("A");
results.add("B");
results.add("C");
break;
case "Choice 2":
results.add("a");
results.add("b");
results.add("c");
break;
case "Choice 3":
results.add("X");
results.add("Y");
results.add("Z");
break;
}

//return result(s)
return results;
}
}





share|improve this answer
























  • can you give me a similar example of my code? Sedrick?

    – Sistem07
    Nov 16 '18 at 19:11












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341583%2fpopulate-second-combobox-with-first-combobox-string-value-in-sql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Here is a demo that covers your problem. You should query the DB based on the first ComboBox selection. Use the results from the query to populate the second ComboBox. <- This part should happen in the first ComboBox valueProperty change listener.



import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ComboBoxExperiments extends Application
{

@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("ComboBox Experiment 1");

ComboBox<String> comboBox = new ComboBox();
comboBox.getItems().add("Choice 1");
comboBox.getItems().add("Choice 2");
comboBox.getItems().add("Choice 3");

ComboBox<String> comboBox2 = new ComboBox();

comboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
comboBox2.getItems().setAll(queryFakeDB(newValue));
comboBox2.getSelectionModel().selectFirst();
});

comboBox.getSelectionModel().selectFirst();
comboBox2.getSelectionModel().selectFirst();

VBox vBox = new VBox(comboBox, comboBox2);
Scene scene = new Scene(vBox, 200, 120);
primaryStage.setScene(scene);
primaryStage.show();

}

public static void main(String args)
{
Application.launch(args);
}

public List<String> queryFakeDB(String comboBox1Selection)
{
//Connect to fake database
//Query fake database
//Store results in a List
//This simulates getting results from db based on combobox1 selection
List<String> results = new ArrayList();
switch (comboBox1Selection) {
case "Choice 1":
results.add("A");
results.add("B");
results.add("C");
break;
case "Choice 2":
results.add("a");
results.add("b");
results.add("c");
break;
case "Choice 3":
results.add("X");
results.add("Y");
results.add("Z");
break;
}

//return result(s)
return results;
}
}





share|improve this answer
























  • can you give me a similar example of my code? Sedrick?

    – Sistem07
    Nov 16 '18 at 19:11
















0














Here is a demo that covers your problem. You should query the DB based on the first ComboBox selection. Use the results from the query to populate the second ComboBox. <- This part should happen in the first ComboBox valueProperty change listener.



import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ComboBoxExperiments extends Application
{

@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("ComboBox Experiment 1");

ComboBox<String> comboBox = new ComboBox();
comboBox.getItems().add("Choice 1");
comboBox.getItems().add("Choice 2");
comboBox.getItems().add("Choice 3");

ComboBox<String> comboBox2 = new ComboBox();

comboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
comboBox2.getItems().setAll(queryFakeDB(newValue));
comboBox2.getSelectionModel().selectFirst();
});

comboBox.getSelectionModel().selectFirst();
comboBox2.getSelectionModel().selectFirst();

VBox vBox = new VBox(comboBox, comboBox2);
Scene scene = new Scene(vBox, 200, 120);
primaryStage.setScene(scene);
primaryStage.show();

}

public static void main(String args)
{
Application.launch(args);
}

public List<String> queryFakeDB(String comboBox1Selection)
{
//Connect to fake database
//Query fake database
//Store results in a List
//This simulates getting results from db based on combobox1 selection
List<String> results = new ArrayList();
switch (comboBox1Selection) {
case "Choice 1":
results.add("A");
results.add("B");
results.add("C");
break;
case "Choice 2":
results.add("a");
results.add("b");
results.add("c");
break;
case "Choice 3":
results.add("X");
results.add("Y");
results.add("Z");
break;
}

//return result(s)
return results;
}
}





share|improve this answer
























  • can you give me a similar example of my code? Sedrick?

    – Sistem07
    Nov 16 '18 at 19:11














0












0








0







Here is a demo that covers your problem. You should query the DB based on the first ComboBox selection. Use the results from the query to populate the second ComboBox. <- This part should happen in the first ComboBox valueProperty change listener.



import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ComboBoxExperiments extends Application
{

@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("ComboBox Experiment 1");

ComboBox<String> comboBox = new ComboBox();
comboBox.getItems().add("Choice 1");
comboBox.getItems().add("Choice 2");
comboBox.getItems().add("Choice 3");

ComboBox<String> comboBox2 = new ComboBox();

comboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
comboBox2.getItems().setAll(queryFakeDB(newValue));
comboBox2.getSelectionModel().selectFirst();
});

comboBox.getSelectionModel().selectFirst();
comboBox2.getSelectionModel().selectFirst();

VBox vBox = new VBox(comboBox, comboBox2);
Scene scene = new Scene(vBox, 200, 120);
primaryStage.setScene(scene);
primaryStage.show();

}

public static void main(String args)
{
Application.launch(args);
}

public List<String> queryFakeDB(String comboBox1Selection)
{
//Connect to fake database
//Query fake database
//Store results in a List
//This simulates getting results from db based on combobox1 selection
List<String> results = new ArrayList();
switch (comboBox1Selection) {
case "Choice 1":
results.add("A");
results.add("B");
results.add("C");
break;
case "Choice 2":
results.add("a");
results.add("b");
results.add("c");
break;
case "Choice 3":
results.add("X");
results.add("Y");
results.add("Z");
break;
}

//return result(s)
return results;
}
}





share|improve this answer













Here is a demo that covers your problem. You should query the DB based on the first ComboBox selection. Use the results from the query to populate the second ComboBox. <- This part should happen in the first ComboBox valueProperty change listener.



import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ComboBoxExperiments extends Application
{

@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("ComboBox Experiment 1");

ComboBox<String> comboBox = new ComboBox();
comboBox.getItems().add("Choice 1");
comboBox.getItems().add("Choice 2");
comboBox.getItems().add("Choice 3");

ComboBox<String> comboBox2 = new ComboBox();

comboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
comboBox2.getItems().setAll(queryFakeDB(newValue));
comboBox2.getSelectionModel().selectFirst();
});

comboBox.getSelectionModel().selectFirst();
comboBox2.getSelectionModel().selectFirst();

VBox vBox = new VBox(comboBox, comboBox2);
Scene scene = new Scene(vBox, 200, 120);
primaryStage.setScene(scene);
primaryStage.show();

}

public static void main(String args)
{
Application.launch(args);
}

public List<String> queryFakeDB(String comboBox1Selection)
{
//Connect to fake database
//Query fake database
//Store results in a List
//This simulates getting results from db based on combobox1 selection
List<String> results = new ArrayList();
switch (comboBox1Selection) {
case "Choice 1":
results.add("A");
results.add("B");
results.add("C");
break;
case "Choice 2":
results.add("a");
results.add("b");
results.add("c");
break;
case "Choice 3":
results.add("X");
results.add("Y");
results.add("Z");
break;
}

//return result(s)
return results;
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 17:00









SedrickSedrick

6,64432340




6,64432340













  • can you give me a similar example of my code? Sedrick?

    – Sistem07
    Nov 16 '18 at 19:11



















  • can you give me a similar example of my code? Sedrick?

    – Sistem07
    Nov 16 '18 at 19:11

















can you give me a similar example of my code? Sedrick?

– Sistem07
Nov 16 '18 at 19:11





can you give me a similar example of my code? Sedrick?

– Sistem07
Nov 16 '18 at 19:11




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341583%2fpopulate-second-combobox-with-first-combobox-string-value-in-sql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python