Last active
September 10, 2018 18:27
-
-
Save KANE-99/8fce1e1bb39eea40a0b2d6dd64aea019 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package library; | |
import java.net.URL; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ResourceBundle; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.beans.value.ObservableValue; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableColumn.CellDataFeatures; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.util.Callback; | |
/** | |
* FXML Controller class | |
* | |
* @author HP | |
*/ | |
public class DatabaseController implements Initializable { | |
@FXML | |
private TableView myTable; | |
Connection mycon=null; | |
Statement mystat=null; | |
ResultSet rs=null; | |
/** | |
* Initializes the controller class. | |
*/ | |
@Override | |
public void initialize(URL url, ResourceBundle rb) { | |
/****THE SIMPLER CODE THAT NEEDS A CLASS STUDENT | |
/*Student s=new Student("101","102","25-03-2018"); | |
final ObservableList<Student> data; | |
data = FXCollections.observableArrayList(s,s); | |
studentID.setCellValueFactory(new PropertyValueFactory<>("studentID")); | |
bookID.setCellValueFactory(new PropertyValueFactory<>("bookID")); | |
dateOfIssue.setCellValueFactory(new PropertyValueFactory<>("dateOfIssue")); | |
myTable.setItems(data);*/ | |
try { | |
mycon=DriverManager.getConnection("jdbc:derby://localhost:1527/TestDatabase","kane", "Kirtesh@1661"); | |
mystat=mycon.createStatement(); | |
} catch (SQLException ex) { | |
Logger.getLogger(Empty_pageController.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
String[] colName= {"Student ID","Book ID","Date of Issue"}; | |
try{ | |
//SQL FOR SELECTING ALL OF CUSTOMER | |
String SQL = "SELECT * from DATABASE"; | |
//ResultSet | |
rs = mystat.executeQuery(SQL); | |
/********************************** | |
* TABLE COLUMN ADDED DYNAMICALLY * | |
**********************************/ | |
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) { | |
//We are using non property style for making dynamic table | |
final int j = i; | |
TableColumn col = new TableColumn(colName[i]); | |
col.setPrefWidth(((i==0)?185:(i==1)?302:145)); | |
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() { | |
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) { | |
return new SimpleStringProperty(param.getValue().get(j).toString()); | |
} | |
}); | |
myTable.getColumns().addAll(col); | |
System.out.println("Column [" + i + "] "); | |
} | |
final ObservableList<ObservableList> data; | |
data = FXCollections.observableArrayList(); | |
/******************************** | |
* Data added to ObservableList * | |
********************************/ | |
while(rs.next()){ | |
//Iterate Row | |
ObservableList<String> row = FXCollections.observableArrayList(); | |
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){ | |
//Iterate Column | |
row.add(rs.getString(i)); | |
} | |
System.out.println("Row [1] added "+row ); | |
data.add(row); | |
} | |
//FINALLY ADDED TO TableView | |
myTable.setItems(data); | |
}catch(Exception e){ | |
e.printStackTrace(); | |
System.out.println("Error on Building Data"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment