|
| 1 | +package io.github.dunwu.db; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.ResultSet; |
| 6 | +import java.sql.Statement; |
| 7 | + |
| 8 | +/** |
| 9 | + * @author Zhang Peng |
| 10 | + * @date 2019-03-05 |
| 11 | + */ |
| 12 | +public class SqliteDemo { |
| 13 | + |
| 14 | + public static void createTable() { |
| 15 | + try { |
| 16 | + Class.forName("org.sqlite.JDBC"); |
| 17 | + Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db"); |
| 18 | + |
| 19 | + Statement statement = connection.createStatement(); |
| 20 | + String sql = new StringBuilder().append("CREATE TABLE COMPANY ").append("(ID INT PRIMARY KEY NOT NULL,") |
| 21 | + .append(" NAME TEXT NOT NULL, ") |
| 22 | + .append(" AGE INT NOT NULL, ") |
| 23 | + .append(" ADDRESS CHAR(50), ").append(" SALARY REAL)") |
| 24 | + .toString(); |
| 25 | + statement.executeUpdate(sql); |
| 26 | + statement.close(); |
| 27 | + connection.close(); |
| 28 | + } catch (Exception e) { |
| 29 | + System.err.println(e.getClass().getName() + ": " + e.getMessage()); |
| 30 | + System.exit(0); |
| 31 | + } |
| 32 | + System.out.println("Create table successfully."); |
| 33 | + } |
| 34 | + |
| 35 | + public static void dropTable() { |
| 36 | + try { |
| 37 | + Class.forName("org.sqlite.JDBC"); |
| 38 | + Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db"); |
| 39 | + |
| 40 | + Statement statement = connection.createStatement(); |
| 41 | + String sql = new StringBuilder().append("DROP TABLE IF EXISTS COMPANY;").toString(); |
| 42 | + statement.executeUpdate(sql); |
| 43 | + statement.close(); |
| 44 | + connection.close(); |
| 45 | + } catch (Exception e) { |
| 46 | + System.err.println(e.getClass().getName() + ": " + e.getMessage()); |
| 47 | + System.exit(0); |
| 48 | + } |
| 49 | + System.out.println("Drop table successfully."); |
| 50 | + } |
| 51 | + |
| 52 | + public static void insert() { |
| 53 | + try { |
| 54 | + Class.forName("org.sqlite.JDBC"); |
| 55 | + Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db"); |
| 56 | + connection.setAutoCommit(false); |
| 57 | + |
| 58 | + Statement statement = connection.createStatement(); |
| 59 | + String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " |
| 60 | + + "VALUES (1, 'Paul', 32, 'California', 20000.00 );"; |
| 61 | + statement.executeUpdate(sql); |
| 62 | + |
| 63 | + sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (2, 'Allen', 25, 'Texas', 15000.00 );"; |
| 64 | + statement.executeUpdate(sql); |
| 65 | + |
| 66 | + sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );"; |
| 67 | + statement.executeUpdate(sql); |
| 68 | + |
| 69 | + sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " |
| 70 | + + "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );"; |
| 71 | + statement.executeUpdate(sql); |
| 72 | + |
| 73 | + statement.close(); |
| 74 | + connection.commit(); |
| 75 | + connection.close(); |
| 76 | + } catch (Exception e) { |
| 77 | + System.err.println(e.getClass().getName() + ": " + e.getMessage()); |
| 78 | + System.exit(0); |
| 79 | + } |
| 80 | + System.out.println("Insert table successfully."); |
| 81 | + } |
| 82 | + |
| 83 | + public static void delete() { |
| 84 | + try { |
| 85 | + Class.forName("org.sqlite.JDBC"); |
| 86 | + Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db"); |
| 87 | + connection.setAutoCommit(false); |
| 88 | + |
| 89 | + Statement statement = connection.createStatement(); |
| 90 | + String sql = "DELETE from COMPANY where ID=2;"; |
| 91 | + statement.executeUpdate(sql); |
| 92 | + |
| 93 | + String sql2 = "DELETE from COMPANY where ID=3;"; |
| 94 | + statement.executeUpdate(sql2); |
| 95 | + |
| 96 | + String sql3 = "DELETE from COMPANY where ID=4;"; |
| 97 | + statement.executeUpdate(sql3); |
| 98 | + connection.commit(); |
| 99 | + |
| 100 | + statement.close(); |
| 101 | + connection.close(); |
| 102 | + } catch (Exception e) { |
| 103 | + System.err.println(e.getClass().getName() + ": " + e.getMessage()); |
| 104 | + System.exit(0); |
| 105 | + } |
| 106 | + System.out.println("Delete table successfully."); |
| 107 | + } |
| 108 | + |
| 109 | + public static void update() { |
| 110 | + try { |
| 111 | + Class.forName("org.sqlite.JDBC"); |
| 112 | + Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db"); |
| 113 | + connection.setAutoCommit(false); |
| 114 | + |
| 115 | + Statement statement = connection.createStatement(); |
| 116 | + String sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1;"; |
| 117 | + statement.executeUpdate(sql); |
| 118 | + connection.commit(); |
| 119 | + |
| 120 | + statement.close(); |
| 121 | + connection.close(); |
| 122 | + } catch (Exception e) { |
| 123 | + System.err.println(e.getClass().getName() + ": " + e.getMessage()); |
| 124 | + System.exit(0); |
| 125 | + } |
| 126 | + System.out.println("Update table successfully."); |
| 127 | + } |
| 128 | + |
| 129 | + public static void select() { |
| 130 | + try { |
| 131 | + Class.forName("org.sqlite.JDBC"); |
| 132 | + Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db"); |
| 133 | + connection.setAutoCommit(false); |
| 134 | + |
| 135 | + Statement statement = connection.createStatement(); |
| 136 | + ResultSet resultSet = statement.executeQuery("SELECT * FROM COMPANY;"); |
| 137 | + while (resultSet.next()) { |
| 138 | + int id = resultSet.getInt("id"); |
| 139 | + String name = resultSet.getString("name"); |
| 140 | + int age = resultSet.getInt("age"); |
| 141 | + String address = resultSet.getString("address"); |
| 142 | + float salary = resultSet.getFloat("salary"); |
| 143 | + String format = String |
| 144 | + .format("ID = %s, NAME = %s, AGE = %d, ADDRESS = %s, SALARY = %f", id, name, age, address, salary); |
| 145 | + System.out.println(format); |
| 146 | + } |
| 147 | + resultSet.close(); |
| 148 | + statement.close(); |
| 149 | + connection.close(); |
| 150 | + } catch (Exception e) { |
| 151 | + System.err.println(e.getClass().getName() + ": " + e.getMessage()); |
| 152 | + System.exit(0); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public static void main(String[] args) { |
| 157 | + SqliteDemo.dropTable(); |
| 158 | + SqliteDemo.createTable(); |
| 159 | + SqliteDemo.insert(); |
| 160 | + SqliteDemo.select(); |
| 161 | + SqliteDemo.delete(); |
| 162 | + SqliteDemo.select(); |
| 163 | + SqliteDemo.update(); |
| 164 | + SqliteDemo.select(); |
| 165 | + } |
| 166 | +} |
0 commit comments