下载驱动jar
下载地址:https://jdbc.postgresql.org/download.html
问题1:导入jar包,写测试,报错
测试内容:
Connection connection =null;
Statement statement = null;
try {
String url="jdbc:postgresql://127.0.0.1:5432/yuxinyue";
String user="postgres";
String psw = "123456";
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(url,user,psw);
System.out.println("是否成功连接pg数据库"+connection);
String sql = "SELECT id, name,geom,x,y, ST_DistanceSphere( \n" +
" geom, \n" +
" ST_GeometryFromText('POINT(116.47894796832 39.909841393006)')) distance \n" +
"FROM mylocation ORDER BY distance asc;";
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
String result = resultSet.getString(0);
System.out.println(result);
}
}catch (Exception e){
e.printStackTrace();
}
报错内容:
HTTP Status 500 - Handler processing failed; nested exception is java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 52.0 (unable to load class org.postgresql.Driver)
查资料发现是版本不对的原因
J2SE 7 = 51 (0x33 hex),
J2SE 6.0 = 50 (0x32 hex),
J2SE 5.0 = 49 (0x31 hex),
JDK 1.4 = 48 (0x30 hex),
JDK 1.3 = 47 (0x2F hex),
JDK 1.2 = 46 (0x2E hex),
JDK 1.1 = 45 (0x2D hex).
所以下载版本需要根据自己的jdk来决定。
问题2:
org.postgresql.util.PSQLException: 栏位索引超过许可范围:0,栏位数:6
查询得知:
ResultSet获取内容应该从1开始,不应该从0开始。
到此,全部成功,接着研究后面的问题,如何用Java操作postgreSql 实现判断点是否在某个多边形内。如有大神知道~求分享求分享~~