package sample.Model;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class SampleModel {
public final SimpleIntegerProperty id;
public final SimpleStringProperty name;
public final SimpleStringProperty address;
public final SimpleStringProperty memo;
public final SimpleStringProperty created_at;
public final SimpleStringProperty updated_at;
// コンストラクタ
public SampleModel(int id, String name, String address,String memo,String created_at,String updated_at) {
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.address = new SimpleStringProperty(address);
this.memo = new SimpleStringProperty(memo);
this.created_at = new SimpleStringProperty(created_at);
this.updated_at = new SimpleStringProperty(updated_at);
}
// idプロパティ
public int getId() {
return this.id.get();
}
public void setId(int id) {
this.id.set(id);
}
public IntegerProperty IdProperty(){
return id;
}
// Nameプロパティ
public String getName() {
return this.name.get();
}
public void setName(String name) {
this.name.set(name);
}
public StringProperty NameProperty() {
return name;
}
// Addressプロパティ
public String getAddress() {
return this.address.get();
}
public void setAddress(String address) {
this.address.set(address);
}
public StringProperty AddressProperty() {
return address;
}
// Memoプロパティ
public String getMemo() {
return this.memo.get();
}
public void setMemo(String memo) {
this.memo.set(memo);
}
public StringProperty MemoProperty() {
return memo;
}
// Created_atプロパティ
public String getCreated_at() {
return this.created_at.get();
}
public void setCreated_at(String created_at) {
this.created_at.set(created_at);
}
public StringProperty Created_atProperty() {
return created_at;
}
// Updated_atプロパティ
public String getUpdated_at() {
return this.updated_at.get();
}
public void setUpdated_at(String updated_at) {
this.updated_at.set(updated_at);
}
public StringProperty Updated_atProperty() {
return updated_at;
}
}
package sample.DBJ;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import sample.db.SqliteDb;
import sample.Model.SampleModel;
public class SampleDBJ {
public static ObservableList data;
// 登録
public static void insertData (String name, String address, String memo) throws SQLException, ClassNotFoundException {
String insertStmt;
// SQL作成
insertStmt = "INSERT INTO sampleTbl" +
"(name, address, memo)" +
"VALUES('" + name + "','" + address + "','" + memo + "')";
try {
// SqliteDBクラスへSQLを発行
SqliteDb.dbUpdate(insertStmt);
} catch (SQLException ex) {
System.out.println("insert Error!." + ex );
throw ex;
}
}
// 全件検索SQL作成
public static ObservableList<SampleModel> searchAllData() throws SQLException, ClassNotFoundException {
// SQL作成
String sql = "SELECT * FROM sampleTbl";
try {
ResultSet rs = SqliteDb.dataQuery(sql);
ObservableList data = searchAllDataList(rs);
return data;
} catch (SQLException ex) {
System.out.println("search Error!." + ex);
throw ex;
}
}
// 全件検索
private static ObservableList searchAllDataList(ResultSet rs) throws SQLException, ClassNotFoundException {
ObservableList data = FXCollections.observableArrayList();
while (rs.next()){
data.add(new SampleModel(
rs.getInt("id"),
rs.getString("name"),
rs.getString("address"),
rs.getString("memo"),
rs.getString("created_at"),
rs.getString("updated_at")));
}
return data;
}
// name検索SQL作成
public static ObservableList searchName(String sql) throws SQLException,ClassNotFoundException {
String names = "SELECT * FROM sampleTbl WHERE name LIKE '%" + sql + "%'";
try {
// SqliteDbクラスへSQLを発行して結果を取得
ResultSet rs = SqliteDb.dataQuery(names);
// データ取得
data = searchNameData(rs);
return data;
} catch (SQLException ex) {
System.out.println("Search Error!.");
throw ex;
}
}
// name検索
private static ObservableList searchNameData(ResultSet rs) throws SQLException {
ObservableList namedata = FXCollections.observableArrayList();
// 特定項目の検索にLIKE文を使用する場合はwhile。
while (rs != null && rs.next()) {
namedata.add(new SampleModel(
rs.getInt("id"),
rs.getString("name"),
rs.getString("address"),
rs.getString("memo"),
rs.getString("created_at"),
rs.getString("updated_at")
));
}
return namedata;
}
// 更新
public static boolean dataUpdate (String id, String address,String memo) throws SQLException, ClassNotFoundException {
// 日時更新
String updatedTime = "datetime(CURRENT_TIMESTAMP,'localtime')";
// SQL作成
String upSql;
upSql = "UPDATE sampleTbl SET updated_at = " + updatedTime + ", address = '" + address + "', memo = '" + memo + "' WHERE id = " + id;
try {
SqliteDb.dbUpdate(upSql);
return true;
} catch (SQLException ex) {
System.out.print("Update Error!.");
throw ex;
}
}
// 削除
public static boolean dataDelete (int id) throws SQLException, ClassNotFoundException {
Alert del_alert = new Alert (Alert.AlertType.CONFIRMATION);
del_alert.setTitle("JavaFXSampleFxmlApp");
del_alert.setHeaderText("削除確認");
del_alert.setContentText(id + "番のデータを削除します。\n"
+ "間違いなければOKボタンを押してください。");
Optional result = del_alert.showAndWait();
if(result.get() != ButtonType.OK){
return false;
}
// SQL作成
String delSql;
delSql = "DELETE FROM sampleTbl" + " WHERE id = " + id;
try {
SqliteDb.dbUpdate(delSql);
return true;
} catch (SQLException ex) {
System.out.print("Delete Error!." + ex);
throw ex;
}
}
}
package sample.db;
import java.sql.*;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
public class SqliteDb {
private static Connection con = null;
public static void dbCon() throws SQLException, ClassNotFoundException {
Statement stmt = null;
try {
// sqlite JDBCドライバーのロード
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException ex) {
System.out.println("JDBCドライバーが見つかりません?。\n" + ex);
throw ex;
}
try {
// データベース接続
con = DriverManager.getConnection("jdbc:sqlite:c:/SQLite/sample.DB");
// ステートメントの生成
stmt = con.createStatement();
// テーブル作成
stmt.executeUpdate("create table if not exists sampleTbl(id integer primary key,name text,address text,memo text,created_at timestamp NOT NULL default (datetime(CURRENT_TIMESTAMP,'localtime')), updated_at timestamp NOT NULL default (datetime(CURRENT_TIMESTAMP,'localtime')))");
} catch (SQLException ex) {
System.out.println("DB接続に失敗しました!。\n" + ex);
throw ex;
}
}
// 検索
public static ResultSet dataQuery(String queryStmt) throws SQLException, ClassNotFoundException {
// CachedRowSetはDBから取得したデータを、メモリ上に格納した状態でもデータの操作が可能。
RowSetFactory rowSetFactory = RowSetProvider.newFactory();
CachedRowSet rowSet = rowSetFactory.createCachedRowSet();
Statement stmt = null;
ResultSet resultSet = null;
try {
dbCon();
stmt = con.createStatement();
resultSet = stmt.executeQuery(queryStmt);
// ResultSetオブジェクトのデータをpopulateメソットを使用して読み込む。
rowSet.populate(resultSet);
} catch (SQLException ex) {
System.out.println("検索に失敗しました!。\n" + ex);
throw ex;
} finally {
if (resultSet != null) {
resultSet.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
return rowSet;
}
// 追加・更新・削除
public static void dbUpdate(String sqlStmt) throws SQLException, ClassNotFoundException {
Statement stmt = null;
try {
dbCon();
stmt = con.createStatement();
stmt.executeUpdate(sqlStmt);
} catch (SQLException ex) {
System.out.println("データの処理に失敗しました!。\n" + ex);
} finally {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
}
}
package sample.view;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.io.IOException;
import static javafx.application.Application.launch;
import sample.Model.SampleModel;
import sample.controller.EditController;
import sample.controller.SeeController;
public class Main extends Application {
public static Main instance;
private Stage primaryStage;
private Scene scene;
private BorderPane rootPane;
// コンストラクタ
public Main() {
instance = this;
}
// アプリケーション起動
public static void main(String[] args) {
launch(args);
}
// ステージを作成・アプリケーションを表示
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("JavaFXSampleFxmlApp");
rootView();
indexView();
}
// Root
public void rootView() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("Root.fxml"));
rootPane = (BorderPane) loader.load();
scene = new Scene(rootPane,960,570);
rootPane.getStyleClass().add("rootPane");
scene.getStylesheets().add("css/Sample.css");
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
}
}
// Index
public void indexView() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("Index.fxml"));
AnchorPane anchorpane = (AnchorPane) loader.load();
rootPane.setCenter(anchorpane);
} catch (IOException e) {
}
}
// Select AllView
public void selectAllView() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("SelectAll.fxml"));
AnchorPane anchorpane = (AnchorPane) loader.load();
rootPane.setCenter(anchorpane);
} catch (IOException e) {
}
}
// Create
public void create() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("Create.fxml"));
AnchorPane anchorpane = (AnchorPane) loader.load();
rootPane.setCenter(anchorpane);
} catch (IOException e) {
}
}
// Search
public void search() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("Search.fxml"));
AnchorPane anchorpane = (AnchorPane) loader.load();
rootPane.setCenter(anchorpane);
} catch (IOException e) {
}
}
// edit Link
public void edit(SampleModel editItems) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("Edit.fxml"));
AnchorPane anchorpane = (AnchorPane) loader.load();
rootPane.setCenter(anchorpane);
EditController editcontroller = loader.<EditController>getController();
editcontroller.editItem(editItems);
} catch (IOException e) {
}
}
// see Link
public void see(SampleModel seeItems) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("See.fxml"));
AnchorPane anchorpane = (AnchorPane) loader.load();
rootPane.setCenter(anchorpane);
SeeController seecontroller = loader.<SeeController>getController();
seecontroller.seeItem(seeItems);
} catch (IOException e) {
}
}
// コントローラからコール、メインのインスタンスを返す
public static Main getInstance(){
return instance;
}
}