mysql point类型 java_java语言实现将mysql的linestring、point 数据类型解析成double类型
时间: 2024-03-07 20:47:22 浏览: 190
对于 MySQL 的 Point 和 LineString 类型,Java 中可以使用 JDBC 驱动程序来将其解析为 double 类型。
这里提供一个示例代码,使用 JDBC 驱动程序连接 MySQL 数据库,查询 Point 类型的数据并解析为 double 类型:
```java
import java.sql.*;
public class MySQLExample {
public static void main(String[] args) {
// MySQL 数据库连接信息
String url = "jdbc:mysql://localhost:3306/testdb";
String username = "root";
String password = "password";
try (Connection conn = DriverManager.getConnection(url, username, password)) {
// 查询 Point 类型的数据
String sql = "SELECT point_column FROM table_name WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 1);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
// 解析 Point 类型的数据
String pointStr = rs.getString("point_column");
String[] coordinates = pointStr.split("\\s+");
double x = Double.parseDouble(coordinates[0].substring(6));
double y = Double.parseDouble(coordinates[1].substring(0, coordinates[1].length() - 1));
System.out.println("x: " + x + ", y: " + y);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
这个示例代码中,首先连接 MySQL 数据库,然后查询 Point 类型的数据。接着,通过 split() 方法将 Point 类型的字符串分割成坐标数组,再使用 Double.parseDouble() 方法将坐标字符串转换为 double 类型的坐标值。
对于 LineString 类型的数据,也可以使用类似的方法进行解析。不过需要注意的是,LineString 类型的数据是由多个 Point 类型的坐标构成的,需要对每个 Point 进行解析。
阅读全文
相关推荐















