├── .gitignore ├── README.md ├── sqlite-jdbc-3.8.11.2.jar ├── sqlitedb-example.iml └── src ├── SQLiteJDBC.java ├── fxmlController.java ├── index.fxml └── test.db /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | out/ 3 | /out 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # javafx-sqlite-example 2 | Example of using javafx and sqlite to make a basic application using mvc principles 3 | 4 | sqlite-jdbc-3.8 obtained from https://github.com/xerial/sqlite-jdbc 5 | -------------------------------------------------------------------------------- /sqlite-jdbc-3.8.11.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SirGoose3432/javafx-sqlite-example/399dee7a78f14db0e19c166603b9e72a892c9c54/sqlite-jdbc-3.8.11.2.jar -------------------------------------------------------------------------------- /sqlitedb-example.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/SQLiteJDBC.java: -------------------------------------------------------------------------------- 1 | import java.sql.*; 2 | 3 | public class SQLiteJDBC { 4 | 5 | public static void main(String args[]) { 6 | SQLiteJDBC test = new SQLiteJDBC(); 7 | 8 | // test.createTable(); 9 | test.insertIntoTable(); 10 | test.selectFromDB(); 11 | // test.updateIntoTable(); 12 | // test.deleteFromTable(); 13 | // test.selectFromDB(); 14 | } 15 | 16 | private void openDB() { 17 | Connection c = null; 18 | Statement stmt = null; 19 | try { 20 | Class.forName("org.sqlite.JDBC"); 21 | c = DriverManager.getConnection("jdbc:sqlite:src/test.db"); 22 | 23 | } catch (Exception e) { 24 | System.err.println(e.getClass().getName() + ": " + e.getMessage()); 25 | System.exit(0); 26 | } 27 | System.out.println("Opened database successfully"); 28 | } 29 | 30 | private void createTable() { 31 | Connection c = null; 32 | Statement stmt = null; 33 | try { 34 | Class.forName("org.sqlite.JDBC"); 35 | c = DriverManager.getConnection("jdbc:sqlite:src/test.db"); 36 | System.out.println("Opened database successfully"); 37 | 38 | stmt = c.createStatement(); 39 | String sql = "CREATE TABLE COMPANY" + 40 | "(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + 41 | " NAME TEXT NOT NULL," + 42 | " AGE INT NOT NULL," + 43 | " ADDRESS CHAR(50)," + 44 | " SALARY REAL)"; 45 | stmt.executeUpdate(sql); 46 | stmt.close(); 47 | c.close(); 48 | } catch (Exception e) { 49 | System.err.println(e.getClass().getName() + ": " + e.getMessage()); 50 | System.exit(0); 51 | } 52 | System.out.println("Table created successfully"); 53 | } 54 | 55 | private void insertIntoTable() { 56 | Connection c = null; 57 | Statement stmt = null; 58 | 59 | try { 60 | Class.forName("org.sqlite.JDBC"); 61 | c = DriverManager.getConnection("jdbc:sqlite:src/test.db"); 62 | c.setAutoCommit(false); 63 | System.out.println("Opened database successfully"); 64 | 65 | stmt = c.createStatement(); 66 | String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" + 67 | "VALUES (null , 'Steve No Jobs', 20, 'California', 50000.00);"; 68 | stmt.executeUpdate(sql); 69 | 70 | // sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + 71 | // "VALUES (2, 'Allen', 25, 'Texas', 15000.00 );"; 72 | // stmt.executeUpdate(sql); 73 | // 74 | // sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + 75 | // "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );"; 76 | // stmt.executeUpdate(sql); 77 | // 78 | // sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + 79 | // "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );"; 80 | // stmt.executeUpdate(sql); 81 | 82 | stmt.close(); 83 | c.commit(); 84 | c.close(); 85 | } catch (Exception e) { 86 | System.err.println(e.getClass().getName() + ": " + e.getMessage()); 87 | System.exit(0); 88 | } 89 | } 90 | 91 | private void selectFromDB() { 92 | Connection c = null; 93 | Statement stmt = null; 94 | try { 95 | Class.forName("org.sqlite.JDBC"); 96 | c = DriverManager.getConnection("jdbc:sqlite:src/test.db"); 97 | c.setAutoCommit(false); 98 | System.out.println("Opened database successfully"); 99 | 100 | stmt = c.createStatement(); 101 | 102 | ResultSet rs = stmt.executeQuery(" SELECT * FROM COMPANY;"); 103 | 104 | while (rs.next()) { 105 | int id = rs.getInt("id"); 106 | String name = rs.getString("name"); 107 | int age = rs.getInt("age"); 108 | String address = rs.getString("address"); 109 | float salary = rs.getFloat("salary"); 110 | System.out.println("ID: " + id); 111 | System.out.println("NAME: " + name); 112 | System.out.println("AGE: " + age); 113 | System.out.println("ADDRESS: " + address); 114 | System.out.println("SALARY: " + salary); 115 | System.out.println(); 116 | } 117 | 118 | rs.close(); 119 | stmt.close(); 120 | c.close(); 121 | } catch (Exception e) { 122 | System.err.println(e.getClass().getName() + ": " + e.getMessage()); 123 | System.exit(0); 124 | } 125 | System.out.println("Operation done successfully"); 126 | } 127 | 128 | private void updateIntoTable() { 129 | Connection c = null; 130 | Statement stmt = null; 131 | 132 | try { 133 | Class.forName("org.sqlite.JDBC"); 134 | c = DriverManager.getConnection("jdbc:sqlite:src/test.db"); 135 | c.setAutoCommit(false);http://google.github.io/lovefield/error_lookup/src/error_lookup.html?c=201&p0=Item.pkItem&p1=100 136 | System.out.println("Opened database successfully"); 137 | 138 | stmt = c.createStatement(); 139 | String sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1;"; 140 | stmt.executeUpdate(sql); 141 | c.commit(); 142 | 143 | stmt.close(); 144 | c.close(); 145 | } catch (Exception e) { 146 | System.err.println(e.getClass().getName() + ": " + e.getMessage()); 147 | System.exit(0); 148 | } 149 | System.out.println("Operation done successfully"); 150 | } 151 | 152 | private void deleteFromTable() { 153 | Connection c = null; 154 | Statement stmt = null; 155 | 156 | try { 157 | Class.forName("org.sqlite.JDBC"); 158 | c = DriverManager.getConnection("jdbc:sqlite:src/test.db"); 159 | c.setAutoCommit(false); 160 | System.out.println("Opened database successfully"); 161 | 162 | stmt = c.createStatement(); 163 | String sql = "DELETE from COMPANY where ID=2;"; 164 | stmt.executeUpdate(sql); 165 | c.commit(); 166 | 167 | stmt.close(); 168 | c.close(); 169 | } catch (Exception e) { 170 | System.err.println(e.getClass().getName() + ": " + e.getMessage()); 171 | System.exit(0); 172 | } 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/fxmlController.java: -------------------------------------------------------------------------------- 1 | import javafx.fxml.FXML; 2 | 3 | public class fxmlController { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/index.fxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/test.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SirGoose3432/javafx-sqlite-example/399dee7a78f14db0e19c166603b9e72a892c9c54/src/test.db --------------------------------------------------------------------------------