javafx database content output to tableview table

First, create Fxml file, edit the page with Javafx Scene Builder, create tableview (tables) and tablecolum (column of the table), and to set fxid;

Second, the generation control fxml class files;

Third, creating a database connection class (using JDBC driver);

Connect.java

Import java.io.IOException;
 Import a java.io.InputStream;
 Import the java.sql.Connection;
 Import the java.sql.DriverManager;
 Import java.sql.SQLException;
 Import the java.util.Properties; 

public  class Connect { 

    // connect to the database URL 
    static String URL;
     // Create properties object 
    static properties info = new new properties (); 

    // driver is loaded 
    static {
         // get attribute file input stream 
        the InputStream iNPUT = Connect. class .getResourceAsStream ( "the config.properties"); 

        The try {
             // load the contents of the properties file Properties object 
            info.load (INPUT);
             // taken from a properties file url 
            url = info.getProperty ( "url" );
             // remove the driver from a properties file 
            String driverClassName = info.getProperty ( "driver" ); 
            the Class.forName (driverClassName); 
            System.out.println ( "drivers loaded successfully ..." ); 
            
        } the catch (a ClassNotFoundException E) { 
            System.out.println ( "drivers are loaded failure ... " ); 
        } the catch (IOException E) { 
            System.out.println ("A properties file failed ..." ); 
        } 
    } 
    // get database connection 
    public  static Connection the getConnection () {
         // create a database connection 
        Connection Conn = null ;
         the try { 
            Conn = the DriverManager.getConnection (URL, info);
            // System.out.println ( "database connection success!"); 
        } the catch (SQLException E) { 
            System.out.println ( "database connection failed!" ); 
            System.out.println (URL); 
            System.out.println ( info); 
        } 
        
        return  Conn;
    } 
}

 

Fourth, the need to control the class inherits the abstract class initable, implement the abstract method implementation;

Fifth, the database connection:

Connection conn = Connect.getConnection();

Write database queries:

String sql="select * from Test";

使用prepareStatement  prep = new prepareStatement(sql);

Execute database statements.

(1) If the database query statements is executed, it will return query results

  To execute the query and query results into res used Resultset res = prep.executeQuery ()

(2) If the non-execution of a database query statements (such as insert, delete, etc.), it does not return results

  Use Resultset res = prep.execute () is performed.

Wrong error.

 

六、创建ObservableList<Data> cellData = FxCollections.observableArraylist();

Data class is the need to insert a table data type tableview

Each row of data are inserted into the table as a string, the Data Class should define variables:

SimpleStringProperty str1 = new SimpleStringProperty();

And create access control (getStr) and modifier (setStr) variable

Data and create a constructor of this class (string).

 

Seven, using cellData.add (new Data (string)) into the data padding method in cellData.

  tableview.setCellvalueFactory(cellData->cellData.getvalue().getStr())

  Finally, use tableview.SetItem (cellData) to bind the data to the table

Guess you like

Origin www.cnblogs.com/fangmr/p/11429914.html