package shop.alien.util.database;

import java.sql.*;

/**
 * 连接Sqlite并取值
 *
 * @author ssk
 * @version 1.0
 * @date 2020/4/13 15:59
 */
public class Sqlite {

    public static void connect() {
        //声明数据库连接对象
        Connection conn = null;
        try {
            //定义连接数据库的url(url:访问数据库的URL路径),wc.db为数据库名称
            String url = "jdbc:sqlite:D:\\SoftWareData\\SQLite\\Test.db";
            //获取数据库连接
            conn = DriverManager.getConnection(url);
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("select * from test");
            while (rs.next()) {
                String col1 = rs.getString("id");
                String col2 = rs.getString("root");
                String col3 = rs.getString("uuid");
                System.out.println("col1 = " + col1 + "  col2 = " + col2 + "  col3 = " + col3);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }

    }


    public static boolean insert() {
        boolean flag = false;
        try {
            Connection conn = null;
            String url = "jdbc:sqlite:D:\\SoftWareData\\SQLite\\Test.db";
            //获取数据库连接
            conn = DriverManager.getConnection(url);
            Statement statement = conn.createStatement();
            boolean execute = statement.execute("insert into test (id,root,uuid) values (5,'4','4')");
            System.out.println(execute);
            return execute;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

    public static boolean update() {
        boolean flag = false;
        try {
            Connection conn = null;
            String url = "jdbc:sqlite:D:\\SoftWareData\\SQLite\\Test.db";
            //获取数据库连接
            conn = DriverManager.getConnection(url);
            Statement statement = conn.createStatement();
            int i = statement.executeUpdate("update test set root = '1' where id = 1");
            System.out.println(i);
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }


    public static void main(String[] args) {
//        connect();
        insert();
//        update();
    }
}
