(编辑:jimmy 日期: 2024/12/29 浏览:2)
index_map.jsp中的代码:
复制代码 代码如下:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
%>
<html>
<head>
</head>
<body>
<form action="insert_map.jsp" method="post">
ID :<input type = "text" name="id" value="0"/>
名称 :<input type = "text" name="name" value="aaa"/> </br>
电压等级 :<input type = "text" name="voltage_level" value="110kv"/>
经度 :<input type = "text" name="lon" value="121."/>
纬度 :<input type = "text" name="lat" value="28."/>
</br>
<input type = "submit" value="提交"/>
</form>
</body>
</html>
insert_map.jsp的代码:
复制代码 代码如下:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
%>
<HTML>
<HEAD>
<TITLE>add message into table </TITLE>
</HEAD>
<BODY>
<%
String id=request.getParameter("id"); //从表单获得
String name=request.getParameter("name"); //从表单获得
String voltage_level=request.getParameter("voltage_level"); //从表单获得
String lon=request.getParameter("lon"); //从表单获得
String lat=request.getParameter("lat"); //从表单获得
java.util.Date date=new java.util.Date();
String datetime=new Timestamp(date.getTime()).toString();
try
{
/** 连接数据库参数 **/
String driverName = "com.mysql.jdbc.Driver"; //驱动名称
String DBUser = "root"; //mysql用户名
String DBPasswd = "123456"; //mysql密码
String DBName = "map_db"; //数据库名
String connUrl = "jdbc:mysql://localhost/" + DBName + "?user=" + DBUser + "&password=" + DBPasswd;
Class.forName(driverName).newInstance();
Connection conn = DriverManager.getConnection(connUrl);
Statement stmt = conn.createStatement();
stmt.executeQuery("SET NAMES UTF8");
String insert_sql = "INSERT INTO biandianzhan_point_tb VALUES('" + id + "','" + name + "','" + voltage_level + "',GeometryFromText('POINT (" + lon + " " + lat + ")') )";
String query_sql = "select * from biandianzhan_point_tb";
try {
stmt.execute(insert_sql);
}catch(Exception e) {
e.printStackTrace();
}
try {
ResultSet rs = stmt.executeQuery(query_sql);
while(rs.next()) {
%>
ID:<%=rs.getString("id")%> </br>
名称:<%=rs.getString("name")%> </br>
电压等级:<%=rs.getString("voltage_level")%> </br>
经纬度:<%=rs.getString("the_geom")%> </br> </br>
<%
}
}catch(Exception e) {
e.printStackTrace();
}
//rs.close();
stmt.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
访问index_map.jsp:
提交前数据库的值:
输入测试值并提交,得到如下页面:
数据库的变化如下:
可以看出,在上面的提交后的页面上经纬度是乱码的,原因是the_geom的数据类型是POINT类型,是几何数据类geometry类中的一个子类,有特定的格式,可能这种特定的格式没能被浏览器识别。我正在考虑的解决方案是将the_geom数据进行剥离,得到两个数值及经纬度。不过可能没这个必要,因为没必要显示这两个值。
总的来说,大功告成了一半吧,呵呵,今天收获很大啊!很开心!