sqlite

public class FileToMeta {
public static void main(String[] args) {
File file = new File(“C:\Users\bit\Desktop\mysearcher\mysearcher.db”);
System.out.println(file.getName());
System.out.println(file.getAbsoluteFile());
System.out.println(file.isDirectory());
System.out.println(file.length());
System.out.println(file.lastModified());

    Date date = new Date(file.lastModified());
    System.out.println(date);
}

}package com.zyh.demo;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class GetClassPath {
public static void main(String[] args) throws UnsupportedEncodingException {
String classesPath = GetClassPath.class.getProtectionDomain()
.getCodeSource().getLocation().getFile();
System.out.println(classesPath);
String decode = URLDecoder.decode(classesPath, “UTF-8”);
System.out.println(decode);
File classesDir = new File(decode);
System.out.println(classesDir.getAbsoluteFile());
}
}

package com.zyh.demo;

import org.sqlite.SQLiteDataSource;

import javax.sql.DataSource;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ScanService {
private static final DataSource dataSource;

static {
    SQLiteDataSource sqLiteDataSource = new SQLiteDataSource();

    try {
        String classesPath = GetClassPath.class.getProtectionDomain()
                .getCodeSource().getLocation().getFile();
        String decode = URLDecoder.decode(classesPath, "UTF-8");
        File classesDir = new File(decode);
        String dbPath = classesDir.getParent() + "/test.db";

        sqLiteDataSource.setUrl("jdbc:sqlite://" + dbPath);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    dataSource = sqLiteDataSource;
}

public static void main(String[] args) throws SQLException {
    //createTable();

    scan();
}

private static List<File> scanResult = new ArrayList<File>();
private static List<File> queryResult = new ArrayList<File>();

private static void scan() throws SQLException {
    File root = new File("D:\\课程\\2021-1-17-春招冲刺班-项目1");

    scanResult.clear();
    scanDir(root);

    queryResult.clear();
    queryDir(root);


    System.out.println(scanResult);
    System.out.println(queryResult);

    // scanResult - queryResult: INSERT
    // queryResult - scanResult: DELETE
}

private static void queryDir(File root) throws SQLException {
    String sql = "SELECT name, path FROM file_meta WHERE path LIKE ?";
    try (Connection connection = dataSource.getConnection()) {
        try (PreparedStatement s = connection.prepareStatement(sql)) {
            s.setString(1, root.getAbsolutePath() + "%");

            try (ResultSet rs = s.executeQuery()) {
                while (rs.next()) {
                    String name = rs.getString(1);
                    String path = rs.getString(2);

                    File file = new File(path);
                    queryResult.add(file);
                }
            }
        }
    }
}

private static void scanDir(File file) {
    scanResult.add(file);

    if (!file.isDirectory()) {
        return;
    }

    File[] files = file.listFiles();
    if (files == null) {
        return;
    }

    for (File child : files) {
        scanDir(child);
    }
}

private static void createTable() throws SQLException {
    String sql = "CREATE TABLE IF NOT EXISTS file_meta (\n" +
            "    id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
            "    name VARCHAR(50) NOT NULL,\n" +
            "    path VARCHAR(1000) NOT NULL,\n" +
            "    is_directory BOOLEAN NOT NULL,\n" +
            "    pinyin VARCHAR(50) NOT NULL,\n" +
            "    pinyin_first VARCHAR(50) NOT NULL,\n" +
            "    size BIGINT NOT NULL,\n" +
            "    last_modified TIMESTAMP NOT NULL\n" +
            ");";

    try (Connection connection = dataSource.getConnection()) {
        try (PreparedStatement s = connection.prepareStatement(sql)) {
            s.executeUpdate();
        }
    }
}

}

package com.zyh.demo;

import org.sqlite.SQLiteDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SqliteDemo {
public static void main(String[] args) throws SQLException {
// 创建 DataSource(区别)
// 获取 Connection
// 操作
SQLiteDataSource sqLiteDataSource = new SQLiteDataSource();
sqLiteDataSource.setUrl(“jdbc:sqlite://E:/qq/java/项目一”);

    DataSource dataSource = sqLiteDataSource;

    Connection connection = dataSource.getConnection();

// PreparedStatement show_tables = connection.prepareStatement(“INSERT INTO file_meta (id) VALUES (1), (2)”);
// show_tables.executeUpdate();

    PreparedStatement s = connection.prepareStatement("SELECT* FROM file_meta");
    ResultSet resultSet = s.executeQuery();
    while (resultSet.next()) {
        System.out.println(resultSet.getString(1));
    }

    connection.close();
}

}

//public class SqliteDemo {
// public static void main(String[] args) throws SQLException {
// // 创建 DataSource(区别)
// // 获取 Connection
// // 操作
//
// SQLiteDataSource sqLiteDataSource = new SQLiteDataSource();
// sqLiteDataSource.setUrl(“jdbc:sqlite://D:/课程/2021-1-17-春招冲刺班-项目1/放数据库文件的位置/test.db”);
//
// DataSource dataSource = sqLiteDataSource;
//
// Connection connection = dataSource.getConnection();
//
PreparedStatement s = connection.prepareStatement(“CREATE TABLE file_meta (id int)”);
s.executeUpdate();
//
// PreparedStatement show_tables = connection.prepareStatement(“INSERT INTO file_meta (id) VALUES (1), (2)”);
// show_tables.executeUpdate();
//
// PreparedStatement s = connection.prepareStatement(“SELECT* FROM file_meta”);
// ResultSet resultSet = s.executeQuery();
// while (resultSet.next()) {
// System.out.println(resultSet.getString(1));
// }
//
// connection.close();
// }
//}

package com.zyh.demo;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.Arrays;

public class ToPinYin {
public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
HanyuPinyinOutputFormat format;
format = new HanyuPinyinOutputFormat();
format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
String s = “动词打次打”;

    for (char ch : s.toCharArray()){
        String[] r = PinyinHelper.toHanyuPinyinStringArray(ch, format);
        System.out.println(Arrays.toString(r));
    }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值