简体   繁体   中英

javafx how to populate an listview with sql query result?

I am new in java programming, so I wanna execute oracle sql statement and i wanna show the results in a listview , I'll put here the sql statemnt code,

I ve declared my list in other class...how shoult I call the list for putting the results.

thank a lot

public class ControllerBD {

public ControllerBD(){

}

public void Execute(){



    String dbUrl = "jdbc:odbc:test";
    String user = "SYSTEM";
    String password = "hpf101";
    try{
        Class.forName("oracle.jdbc.OracleDriver");
    }
    catch(ClassNotFoundException e) {
        e.printStackTrace();    
        System.out.println("Eroare incarcare driver!\n" + e);
    }
    try{
        Connection c=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", user, password);
        Statement s= c.createStatement();
        ResultSet  r =
            s.executeQuery(
                " SELECT titlu FROM carti");


        while (r.next()) {
            System.out.println (
                r.getString ("titlu"));



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

            }
}

            public void getvalues (){

            }

}

If you create your listview you can create method in that listview, that add data to list. Then create instance of your listview in Execute class and then add data to this instance in while (r.next()) { cycle like this:

Example

while (r.next()) {
  listviewInstance.addData(r.getString ("titlu")));
}

All variable names and method names is example, you need to create yours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM