Code in index_map.jsp:
Copy codeThe Code is as follows:
<% @ 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"/>
Name: <input type = "text" name = "name" value = "aaa"/> </br>
Voltage Level: <input type = "text" name = "voltage_level" value = "110kv"/>
Longitude: <input type = "text" name = "lon" value = "121."/>
Latitude: <input type = "text" name = "lat" value = "28."/>
</Br>
<Input type = "submit" value = "submit"/>
</Form>
</Body>
</Html>
Insert_map.jsp code:
Copy codeThe Code is as follows:
<% @ 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"); // obtain from the form
String name = request. getParameter ("name"); // obtain from the form
String voltage_level = request. getParameter ("voltage_level"); // obtain from the form
String lon = request. getParameter ("lon"); // obtain from the form
String lat = request. getParameter ("lat"); // obtain from the form
Java. util. Date date = new java. util. Date ();
String datetime = new Timestamp (date. getTime (). toString ();
Try
{
/** Database connection parameters **/
String driverName = "com. mysql. jdbc. Driver"; // Driver name
String DBUser = "root"; // mysql user name
String DBPasswd = "123456"; // mysql password
String DBName = "map_db"; // Database Name
String connUrl = "jdbc: mysql: // localhost/" + DBName + "? User = "+ DBUser +" & password = "+ DBPasswd;
Class. forName (driverName). newInstance ();
Connection conn = DriverManager. getConnection (connUrl );
Statement stmt = conn. createStatement ();
Stmt.exe cuteQuery ("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.exe cute (insert_ SQL );
} Catch (Exception e ){
E. printStackTrace ();
}
Try {
ResultSet rs = stmt.exe cuteQuery (query_ SQL );
While (rs. next ()){
%>
ID: <% = rs. getString ("id") %> </br>
Name: <% = rs. getString ("name") %> </br>
Voltage Level: <% = rs. getString ("voltage_level") %> </br>
Longitude and latitude: <% = rs. getString ("the_geom") %> </br>
<%
}
} Catch (Exception e ){
E. printStackTrace ();
}
// Rs. close ();
Stmt. close ();
Conn. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
%>
</Body>
</Html>
Access index_map.jsp:
Database value before submission:
Enter the test value and submit it. The following page is displayed:
Database changes are as follows:
We can see that the latitude and longitude are garbled on the submitted page. The reason is that the_geom is of the POINT type and is a subclass of the geometry class in the ry class. It has a specific format, this format may not be recognized by the browser. The solution I am considering is to strip the_geom data and get two values and latitude and longitude. However, this may not be necessary because it is not necessary to display the two values.
In general, the success is half done! Very happy!