全部
This commit is contained in:
@ -12,11 +12,11 @@ import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.sqlite.JDBC;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -96,7 +96,7 @@ public class PoiInfoController {
|
||||
}
|
||||
String path = poiInfo.getPath();
|
||||
// 构建查询SQL
|
||||
StringBuilder dataSql = new StringBuilder("SELECT id, name, address, lng, lat FROM tbl_pois WHERE 1=1");
|
||||
StringBuilder dataSql = new StringBuilder("SELECT id, name, lng, lat FROM data WHERE 1=1");
|
||||
List<Object> params = new ArrayList<>();
|
||||
// 名称搜索条件
|
||||
if (name != null && !name.trim().isEmpty()) {
|
||||
@ -106,7 +106,7 @@ public class PoiInfoController {
|
||||
// 分页条件
|
||||
dataSql.append(" LIMIT ? OFFSET ?");
|
||||
// 执行查询
|
||||
try (Connection connection = SQLiteUtil.getConnection(path);
|
||||
try (Connection connection = getConnectionByAbsolutePath(path);
|
||||
PreparedStatement dataPs = connection.prepareStatement(dataSql.toString())) {
|
||||
// 设置参数
|
||||
int paramIndex = 1;
|
||||
@ -122,7 +122,6 @@ public class PoiInfoController {
|
||||
PoiVo poi = new PoiVo();
|
||||
poi.setId(dataRs.getLong("id"));
|
||||
poi.setName(dataRs.getString("name"));
|
||||
poi.setAddress(dataRs.getString("address"));
|
||||
poi.setLng(dataRs.getString("lng"));
|
||||
poi.setLat(dataRs.getString("lat"));
|
||||
poiList.add(poi);
|
||||
@ -134,4 +133,33 @@ public class PoiInfoController {
|
||||
return ApiResponse.failure("POI数据查询失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据绝对路径获取SQLite数据库连接
|
||||
*/
|
||||
public static Connection getConnectionByAbsolutePath(String absolutePath) throws SQLException {
|
||||
if (absolutePath == null || absolutePath.trim().isEmpty()) {
|
||||
throw new SQLException("数据库文件路径不能为空");
|
||||
}
|
||||
File dbFile = new File(absolutePath.trim());
|
||||
if (!dbFile.exists()) {
|
||||
throw new SQLException("数据库文件不存在:" + absolutePath);
|
||||
}
|
||||
if (!dbFile.isFile()) {
|
||||
throw new SQLException("路径指向的不是文件:" + absolutePath);
|
||||
}
|
||||
if (!dbFile.canRead()) {
|
||||
throw new SQLException("没有权限读取数据库文件:" + absolutePath);
|
||||
}
|
||||
try {
|
||||
Class.forName(JDBC.class.getName());
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new SQLException("SQLite驱动加载失败、请检查依赖是否正确", e);
|
||||
}
|
||||
|
||||
String jdbcUrl = "jdbc:sqlite:" + absolutePath;
|
||||
Connection connection = DriverManager.getConnection(jdbcUrl);
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user