1. 添加依赖
<dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.7.2</version> </dependency>
2. 完成!
Class.forName("org.sqlite.JDBC"); PropertiesUtil propertiesUtil = new PropertiesUtil(); String sqlite = "jdbc:sqlite:" + propertiesUtil.getPath("test.db");//获取项目下test.db的路径 Connection conn = DriverManager.getConnection(sqlite, "", ""); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("select * from students"); while(rs.next()){ String name = rs.getString("name"); System.out.println(name); }
3. 补充
public class PropertiesUtil { public String getPath(String fileName){ String path = this.getClass().getResource("/").getPath() + fileName; return path; } }
4. 注意
test.db文件可以通过以下步骤创建: