mirror of https://github.com/dunwu/db-tutorial.git
sqlite java api demo
parent
d47921803b
commit
e37e5b4caa
|
@ -0,0 +1,51 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.1.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<groupId>io.github.dunwu.db</groupId>
|
||||
<artifactId>sqlite-demo</artifactId>
|
||||
<name>SQLite Demo</name>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>3.25.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
package io.github.dunwu.db;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
|
||||
/**
|
||||
* @author Zhang Peng
|
||||
* @date 2019-03-05
|
||||
*/
|
||||
public class SqliteApplication implements CommandLineRunner {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(SqliteApplication.class).run(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
SqliteDemo.main(null);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,166 @@
|
|||
package io.github.dunwu.db;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* @author Zhang Peng
|
||||
* @date 2019-03-05
|
||||
*/
|
||||
public class SqliteDemo {
|
||||
|
||||
public static void createTable() {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db");
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
String sql = new StringBuilder().append("CREATE TABLE COMPANY ").append("(ID INT PRIMARY KEY NOT NULL,")
|
||||
.append(" NAME TEXT NOT NULL, ")
|
||||
.append(" AGE INT NOT NULL, ")
|
||||
.append(" ADDRESS CHAR(50), ").append(" SALARY REAL)")
|
||||
.toString();
|
||||
statement.executeUpdate(sql);
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
||||
System.exit(0);
|
||||
}
|
||||
System.out.println("Create table successfully.");
|
||||
}
|
||||
|
||||
public static void dropTable() {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db");
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
String sql = new StringBuilder().append("DROP TABLE IF EXISTS COMPANY;").toString();
|
||||
statement.executeUpdate(sql);
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
||||
System.exit(0);
|
||||
}
|
||||
System.out.println("Drop table successfully.");
|
||||
}
|
||||
|
||||
public static void insert() {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db");
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
|
||||
+ "VALUES (1, 'Paul', 32, 'California', 20000.00 );";
|
||||
statement.executeUpdate(sql);
|
||||
|
||||
sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (2, 'Allen', 25, 'Texas', 15000.00 );";
|
||||
statement.executeUpdate(sql);
|
||||
|
||||
sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " + "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );";
|
||||
statement.executeUpdate(sql);
|
||||
|
||||
sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
|
||||
+ "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";
|
||||
statement.executeUpdate(sql);
|
||||
|
||||
statement.close();
|
||||
connection.commit();
|
||||
connection.close();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
||||
System.exit(0);
|
||||
}
|
||||
System.out.println("Insert table successfully.");
|
||||
}
|
||||
|
||||
public static void delete() {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db");
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
String sql = "DELETE from COMPANY where ID=2;";
|
||||
statement.executeUpdate(sql);
|
||||
|
||||
String sql2 = "DELETE from COMPANY where ID=3;";
|
||||
statement.executeUpdate(sql2);
|
||||
|
||||
String sql3 = "DELETE from COMPANY where ID=4;";
|
||||
statement.executeUpdate(sql3);
|
||||
connection.commit();
|
||||
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
||||
System.exit(0);
|
||||
}
|
||||
System.out.println("Delete table successfully.");
|
||||
}
|
||||
|
||||
public static void update() {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db");
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
String sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1;";
|
||||
statement.executeUpdate(sql);
|
||||
connection.commit();
|
||||
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
||||
System.exit(0);
|
||||
}
|
||||
System.out.println("Update table successfully.");
|
||||
}
|
||||
|
||||
public static void select() {
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
Connection connection = DriverManager.getConnection("jdbc:sqlite:test.db");
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery("SELECT * FROM COMPANY;");
|
||||
while (resultSet.next()) {
|
||||
int id = resultSet.getInt("id");
|
||||
String name = resultSet.getString("name");
|
||||
int age = resultSet.getInt("age");
|
||||
String address = resultSet.getString("address");
|
||||
float salary = resultSet.getFloat("salary");
|
||||
String format = String
|
||||
.format("ID = %s, NAME = %s, AGE = %d, ADDRESS = %s, SALARY = %f", id, name, age, address, salary);
|
||||
System.out.println(format);
|
||||
}
|
||||
resultSet.close();
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SqliteDemo.dropTable();
|
||||
SqliteDemo.createTable();
|
||||
SqliteDemo.insert();
|
||||
SqliteDemo.select();
|
||||
SqliteDemo.delete();
|
||||
SqliteDemo.select();
|
||||
SqliteDemo.update();
|
||||
SqliteDemo.select();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/boot/logging/logback/base.xml"/>
|
||||
<logger name="io.github.dunwu" level="DEBUG"/>
|
||||
</configuration>
|
Loading…
Reference in New Issue