JDBC之数据连接
:数据库语句:
create database LandDB;
use LandDB;
create table T_BL_CANTON_CODE
(
CTN_CODE int primary key,
CTN_NAME varchar() not null,
GOV_NAME varchar() not null,
LAND_DP_NAME varchar() not null,
PARENT_CODE int not null
);INSERT INTO `landdb`.`t_bl_canton_code`
(`CTN_CODE`,
`CTN_NAME`,
`GOV_NAME`,
`LAND_DP_NAME`,
`PARENT_CODE`)
VALUES(,'长沙市','长沙市人民政府','长沙市国土资源厅','HN');INSERT INTO `landdb`.`t_bl_canton_code`
(`CTN_CODE`,
`CTN_NAME`,
`GOV_NAME`,
`LAND_DP_NAME`,
`PARENT_CODE`)
VALUES(,'北京市','北京市人民政府','北京市国土资源厅','BN'); :将提供的Images,css文件夹拷贝至建立的网站根目录下,也就是下项目的webroot目录中。 3:改写素材文件夹下所有html 文件为jsp文件到项目的webroot目录下。替换校正原来的链接,因为素材文件原来链接指向的是HTML文件,现在更名为了JSP文件,需要把原来指向的HTML改为JSP。 4:按照工程开发的需要,建立命名空间,一般应该有以下命名空间:
放置实体类的空间:entity或者model
放置过滤器的空间:filter
放置数据访问层的空间:dao
放置servlet的空间servlet :建立实体类:Canton,放置到entity命名空间下。
代码如下:
package com.laozhu.entity; public class Canton {
private int ctn_code; //行政区代码
private String ctn_name; //行政区名称
private String gov_name; //政府名称
private String land_dp_name; //国土部门名称
private int parent_code; //上级行政区代码 public int getCtn_code() {
return ctn_code;
}
public void setCtn_code(int ctn_code) {
this.ctn_code = ctn_code;
}
public String getCtn_name() {
return ctn_name;
}
public void setCtn_name(String ctn_name) {
this.ctn_name = ctn_name;
}
public String getGov_name() {
return gov_name;
}
public void setGov_name(String gov_name) {
this.gov_name = gov_name;
}
public String getLand_dp_name() {
return land_dp_name;
}
public void setLand_dp_name(String land_dp_name) {
this.land_dp_name = land_dp_name;
}
public int getParent_code() {
return parent_code;
}
public void setParent_code(int parent_code) {
this.parent_code = parent_code;
}
}
:建立数据库连接类,过程如下:
A:将MYSQL驱动如mysql-connector-java-5.1..jar复制到项目中,找到该文件后右键--build Path --add build path,从而将该驱动包引入到工程中。
B:确保数据库服务器Mysql已经启动而且存在数据库landb并且存在表t_bl_canton_code,该表结果正确。
C:建立数据库连接类,如BuildConn类,代码中的方法openconn()返回一个数据库的连接Connection. :创建字符集过滤器以便过滤字符集。
创建命名空间Filter,在该空间下新建类CharsetFilter,在新建时在接口选项Interfaces中添加Javax.Servlet.Filter接口,這样系统会自动引用相应的接口并且形成相应的方法。
自动形成的代码中有以下代码:
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub }
这就是形成的过滤器的方法,改写该方法;代码如下: arg0.setCharacterEncoding(encoding);
arg1.setContentType("text/html;charset="+encoding);
arg2.doFilter(arg0, arg1);
配置过滤器:代码写好以后打开web.xml,在</web-app>前添加类似以下代码:
<filter>
<filter-name>CharSetfilter</filter-name> //过滤器名
<filter-class>com.laozhu.filter.Charfilter</filter-class> //过滤器类
<init-param>
<param-name>encoding</param-name> //定义参数encoding
<param-value>UTF-</param-value>
</init-param>
</filter>
<filter-mapping> //定义映射。
<filter-name>CharSetfilter</filter-name>
<url-pattern>/*</url-pattern> //映射到目录中的所有文件。也就是针对所有的文件进行过滤。
</filter-mapping> 7:新建数据库访问类:CantonDaoImp,该类实现能对表的增删改查操作,完成对数据库的操作。代码参考例子。 8:新建一个servlet CantonAddServlet,用于执行数据库的插入。也就是新增数据。代码见参考代码。请注意web.xml中针对此servlet的映射路径。 9:打开addCanton.jsp,首先修改执行的页面,也就是代码:<form action="" 改成:<form action="CantonAddServlet" ,注意 CantonAddServlet是你映射的路径。
然后在文件中将参数名与表单中的字段名一一对应。如在CantonAddServlet中的:
int ctn_code = Integer.parseInt(request.getParameter("ctn_code"));
這是要获取ctn_code,就将addCanton.jsp中的城市代码表单的文本框字段名设置为:ctn_code,也就是将原来的代码修改为:<td width="132" height="9"><span class="style2"> 行政区代码</span></td>
<td width="239"><input name="ctn_code" type="text" id="ctn_code" size="20">
其他的表单输入框一样处理。 10:可以调试增加记录了。 11:在servlet命名空间增加servlet ,名字为CantonListServlet,代码参考代码包。 12:修改listCanton.jsp。代码参考项目代码。 13:修改index_tree1.jsp,将原来的链接修改为CantonListServlet<A href="CantonListServlet" target="main1">行政区划</A> 14:测试。
步骤
布局图:
MySQL:
package com.caiduping.Dao;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class BuildConn { public Connection openconn()
{Connection conn=null; try {
//加载Mysql驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String URL="jdbc:mysql://localhost:3306/landdb";
String username="user2";
String userpassword="";
try {
//建立连接
conn=DriverManager.getConnection(URL, username, userpassword);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return conn;
}
}
BuildConn类
package com.caiduping.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import com.caiduping.Dao.BuildConn;
import com.caiduping.entity.Canton;
public class CantonDaoImp { //addCanton方法实现插入数据
public boolean addCanton(Canton canton) throws SQLException {
//设置标识
boolean flag = false;
//执行的sql语句,?为参数
String sql = "insert into T_BL_CANTON_CODE values(?,?,?,?,?)";
//定义连接对象
Connection con = null;
//定义命令对象
PreparedStatement prst = null;
//实例化BuildConn类
BuildConn bd=new BuildConn();
//调用BuildConn的openconn方法返回数据库的链接
con = bd.openconn();
try {
//准备执行sql命令
prst = con.prepareStatement(sql);
//分别对应设置sql语句中的参数,应该在数据类型与参数一一对应
prst.setInt(, canton.getCtn_code());
prst.setString(, canton.getCtn_name());
prst.setString(, canton.getGov_name());
prst.setString(, canton.getLand_dp_name());
prst.setString(, canton.getParent_code());
if(prst.executeUpdate()>) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally { }con.close();
return flag;
} public boolean deleteCanton(int id) {
// TODO Auto-generated method stub
return false;
} public Canton findCanton(int id) {
// TODO Auto-generated method stub
return null;
}
//以下方法返回Canton列表
public List<Canton> listCanton() {
List<Canton> list = new ArrayList<Canton>();
String sql = "select * from T_BL_CANTON_CODE";
Connection con = null;
PreparedStatement prst = null;
ResultSet rest = null;
//实例化BuildConn类
BuildConn bd=new BuildConn();
//调用BuildConn的openconn方法返回数据库的链接
con = bd.openconn();
try {
prst = con.prepareStatement(sql);
//结果集对象rest
rest = prst.executeQuery();
while(rest.next()) {
//定义一个canton对象
Canton canton = new Canton();
canton.setCtn_code(rest.getInt("ctn_code"));
canton.setCtn_name(rest.getString("ctn_name"));
canton.setGov_name(rest.getString("gov_name"));
canton.setLand_dp_name(rest.getString("land_dp_name"));
canton.setParent_code(rest.getString("parent_code"));
//读出的该对象加入到list列表中
list.add(canton);
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//返回列表
return list;
} public boolean updateCanton(Canton canton) {
// TODO Auto-generated method stub
return false;
}
public int delCanton(long cantonID) throws SQLException
{int a=;
Connection con = null;
PreparedStatement prst = null;
//实例化BuildConn类
BuildConn bd=new BuildConn();
con = bd.openconn();
String sql="delete from t_bl_canton_code where CTN_CODE =" + cantonID;
Statement st=con.createStatement();
a=st.executeUpdate(sql);
st.close();
con.close();
return a;
}
public int delCanton(Canton canton) throws SQLException
{int a=;
Connection con = null;
PreparedStatement prst = null;
//实例化BuildConn类
BuildConn bd=new BuildConn();
con = bd.openconn();
String sql="delete from t_bl_canton_code where CTN_CODE =" + canton.getCtn_code();
Statement st=con.createStatement();
a=st.executeUpdate(sql);
st.close();
con.close();
return a;
}
}
CantonDaoImp类
package com.caiduping.entity; public class Canton {
//行政区代码
private int ctn_code;
//行政区名称
private String ctn_name;
//政府名称
private String gov_name;
//国土部门名称
private String land_dp_name;
//上级行政区代码
private String parent_code; public int getCtn_code() {
return ctn_code;
}
public void setCtn_code(int ctn_code) {
this.ctn_code = ctn_code;
}
public String getCtn_name() {
return ctn_name;
}
public void setCtn_name(String ctn_name) {
this.ctn_name = ctn_name;
}
public String getGov_name() {
return gov_name;
}
public void setGov_name(String gov_name) {
this.gov_name = gov_name;
}
public String getLand_dp_name() {
return land_dp_name;
}
public void setLand_dp_name(String land_dp_name) {
this.land_dp_name = land_dp_name;
}
public String getParent_code() {
return parent_code;
}
public void setParent_code(String parent_code) {
this.parent_code = parent_code;
} }
Canton类
CharsetFilter继承Filter(javax.servlet.filter方法):
package com.caiduping.filter; import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class CharsetFilter implements Filter {
//定义编码方式
String encoding=null;
public void destroy() {
// TODO Auto-generated method stub } public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
//设置Request的字符集编码
arg0.setCharacterEncoding(encoding);
//发送response的编码
arg1.setContentType("text/html;charset="+encoding);
//传递给下一过滤器
arg2.doFilter(arg0, arg1);
}
//下面的方法是在过滤器加载时调用,用于初始化过滤器。
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
//读取web.xml中的过滤器参数"encoding"
encoding=arg0.getInitParameter("encoding");
} }
CharsetFilter类
package com.caiduping.servlet; import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.caiduping.Dao.CantonDaoImp;
import com.caiduping.entity.Canton;
public class CantonAddServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; /**
* Constructor of the object.
*/
public CantonAddServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
int ctn_code = Integer.parseInt(request.getParameter("ctn_code"));
String ctn_name = request.getParameter("ctn_name");
String gov_name = request.getParameter("gov_name");
String land_dp_name = request.getParameter("land_dp_name");
String parent_code = request.getParameter("parent_code");
Canton canton = new Canton();
canton.setCtn_code(ctn_code);
canton.setCtn_name(ctn_name);
canton.setGov_name(gov_name);
canton.setLand_dp_name(land_dp_name);
canton.setParent_code(parent_code);
CantonDaoImp cantonadd = new CantonDaoImp();
try {
if(cantonadd.addCanton(canton)) {
request.getRequestDispatcher("CantonListServlet").forward(request, response);
} else {
System.out.println("fail");
request.getRequestDispatcher("../index.jsp");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
out.print(e.getMessage());
e.printStackTrace();
}
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
CantonAddServlet类
package com.caiduping.servlet; import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.caiduping.Dao.CantonDaoImp;
public class CantonDelServlet extends HttpServlet { /**
* Constructor of the object.
*/
public CantonDelServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int ctn_code = Integer.parseInt(request.getParameter("ctn_code"));
CantonDaoImp cdi=new CantonDaoImp();
try {
cdi.delCanton(ctn_code);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("CantonListServlet").forward(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request,response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
CantonDelServlet类
package com.caiduping.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.caiduping.Dao.CantonDaoImp;
import com.caiduping.entity.Canton; import java.util.List;
public class CantonListServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; /**
*
*/ /**
* Constructor of the object.
*/
public CantonListServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);//以便不管提交数据是什么方式均专一到dopost
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//实例化CantonDaoImp,工程中可以写一个接口
CantonDaoImp cantonService = new CantonDaoImp();
//建立一个Canton列表 ,调用CantonDaoImp方法返回這个列表
List<Canton> list = cantonService.listCanton();
//放置這个列表到request对象中
request.setAttribute("list", list);
//转向到listCanton.jsp
request.getRequestDispatcher("listCanton.jsp").forward(request, response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
CantonListServlet类
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>
</title>
<style type="text/css">
<!--
.style1 {color: #FF0000}
.style2 {font-size: 14px}
-->
</style>
</head> <body>
<script language="javascript">
function check(){
if (form1.title.value==""){
alert("请输入新闻标题")
form1.title.focus();
return false;
}
if (form1.content.value==""){
alert("请输入内容新闻内容");
form1.content.focus();
return false;
}
}
</script> <table width="" border="">
<tr><td width="" align="left"><form action="CantonAddServlet" method="post" name="form1">
<table width="" height="" border="" align="left" style="border-collapse:collapse">
<tr align="left">
<td height="" colspan=""><span class="titletxt">行政区划信息录入(以下带<span class="style1">*</span>为必填项)</span></td>
</tr>
<tr>
<td width="" height=""><span class="style2"> 行政区代码</span></td>
<td width=""><input name="ctn_code" type="text" id="ctn_code" size="">
<span class="style1">*</span>
</td>
</tr>
<tr>
<td width="" height=""><span class="style2"> 行政区名称 </span></td>
<td><input name="ctn_name" type="text" id="ctn_name" size="">
<span class="style1">*
</span></td>
</tr>
<tr>
<td width="" height=""><span class="style2"> 政府名称</span></td>
<td><input name="gov_name" type="text" id="gov_name" size=""></td>
</tr>
<tr>
<td width="" height=""><span class="style2"> 国土部门名称</span></td>
<td><input name="land_dp_name" type="text" id="land_dp_name" size=""></td>
</tr>
<tr>
<td width="" height=""><span class="style2"> 上级行政区代码</span></td>
<td><input name="parent_code" type="text" id="parent_code" size=""></td>
</tr>
<tr>
<td> </td>
<td><input name="bt1" type="submit" id="bt1" value="确定"></td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>
addCanton.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<link href="css/style.css" rel=stylesheet type=text/css>
</head>
<body topmargin='' leftmargin=''>
<table cellpadding="" cellspacing="" height='' width='100%' border=''>
<tr>
<td style="background-image: url('images/main_header_left.jpg'); background-repeat: no-repeat" height='' width=''> </td>
<td class='lth' valign='bottom' background='images/main_header_title.jpg' nowrap>当前位置:操作员 > </td>
<td background='images/main_header_center.jpg' height='' width=''> </td>
<td background='images/main_header_center1.jpg' height='' width='100%'></td>
<td background='images/main_header_right.jpg' height='' width='' align='right'> </td>
</tr>
</table>
</body>
</html>
current_site.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<LINK
href="css/content.css" type=text/css rel=stylesheet>
<style type="text/css">
<!--
.STYLE1 {font-size: 24px}
-->
</style>
</head> <body>
<br />
<table width="" border="" align="center" cellpadding="" cellspacing="">
<tr>
<td width="100%" height="" bgcolor="#80C6FF" class="titletxt"><div align="center" class="STYLE1">对不起,页面正在建设中!!</div></td>
</tr>
</table>
<p> </p>
</body>
</html>
error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<LINK href="css/css.css" type=text/css rel=stylesheet>
<SCRIPT language=Javascript> function onChange(i){
childSort=document.all("child" + i);
//theTd=document.all("td" + i);
if(childSort.style.display=="none"){
//theTd.bgcolor="#ffffff";
childSort.style.display="";}
else{
//theTd.bgcolor="#000000";
childSort.style.display="none";}
} function leafF(url){
window.parent.main1.location.href=url;
} </SCRIPT>
</HEAD>
<body MS_POSITIONING="GridLayout" leftmargin="" topmargin="" marginwidth="" marginheight="" bgcolor="#E8E8E8" class='body'>
<TABLE style="BORDER-COLLAPSE: collapse" cellSpacing= cellPadding= width="" border=''>
<TR>
<TD CLOSPAN='' height='' ></TD>
</Tr>
<TR>
<TD width=></TD>
<TD width=> <IMG height= src="data:images/rootmiddle.jpg" width= align=absMiddle border=>
<A href="javascript:onChange(2)">
基础数据设置 </A>
<BR>
<SPAN id=child2 style="DISPLAY: 0" valign='middle'>
<IMG height= src="data:images/line_tri.jpg" width= align=absMiddle border='' ><IMG height= src="data:images/leaf.jpg" width= align=absMiddle border=''>
<A href="error.html" target="main1">
新设申请书 </A>
<BR>
<SPAN id=child2 style="DISPLAY: 0" valign='middle'>
<IMG height= src="data:images/line_tri.jpg" width= align=absMiddle border='' ><IMG height= src="data:images/leaf.jpg" width= align=absMiddle border=''>
<A href="CantonListServlet" target="main1">
行政区划 </A>
<BR>
<SPAN id=child2 style="DISPLAY: 0" valign='middle'>
<IMG height= src="data:images/line_tri.jpg" width= align=absMiddle border='' ><IMG height= src="data:images/leaf.jpg" width= align=absMiddle border=''>
<A href="error.jsp" target="main1">
设备仪器清单 </A>
<BR> <IMG height= src="data:images/roottop.jpg" width= align=absMiddle border=>
<A href="javascript:onChange(1)">
部级资质新设申请 </A>
<BR> </SPAN> <IMG height= src="data:images/rootmiddle.jpg" width= align=absMiddle border=>
<A href="javascript:onChange(3)">
数据导入导出 </A>
<BR> <IMG height= src="data:images/rootmiddle.jpg" width= align=absMiddle border=>
<A href="javascript:leafF('Login.jsp')">
退出
</A>
<BR> </TD></TR></TABLE>
</body>
</HTML>
index_tree1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<link href="css/style.css" rel=stylesheet type=text/css>
<script language="VBScript">
<!--
Sub GoForward()
history.go()
End Sub Sub GoBack()
history.go(-)
End Sub
-->
</script> <SCRIPT language=Javascript> function onChange(i){
childSort=document.all("child" + i);
//theTd=document.all("td" + i);
if(childSort.style.display=="none"){
//theTd.bgcolor="#ffffff";
childSort.style.display="";}
else{
//theTd.bgcolor="#000000";
childSort.style.display="none";}
} function leafF(url){
window.parent.main1.location.href=url;
} </SCRIPT> </head>
<body topmargin='' leftmargin='' background='images/bodybackground.jpg'>
<table border="" cellpadding="" cellspacing="" background='images/header.jpg' align='center' valign='top' width="100%" height="">
<tr>
<td style="background-image: url('images/headerleft.jpg'); background-repeat: no-repeat" width='' height=''>
</td>
<td align='left' width='100%'>
<font style='font-size:12pt;color:#000000;'> 建设用地审批电子报盘管理软件 Ver1.</font>
</td>
</tr>
<tr>
<td height='' colspan='' width='100%' >
<table border="" cellspacing="">
<tr>
<td width=""> </td>
<td width=""><div align="center"><img src="data:images/back.gif" width="" height="" onClick="GoBack()"></div></td>
<td width=""><div align="center"><img src="data:images/next.gif" width="" height="" onClick="GoForward()"></div></td>
<td width=""><div align="center"><a href="index.jsp" target="_top"><img src="data:images/return.gif" width="" height="" border=""></a></div></td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
</tr>
<tr>
<td> </td>
<td><div align="center"><font color="#3074A2" style="font-size:9pt;color:#000000;">向前</font></div></td>
<td><div align="center"><font color="#3074A2" style="font-size:9pt;color:#000000">向后</font></div></td>
<td><div align="center"><font color="#3074A2" style="font-size:9pt;color:#000000">返回</font></div></td>
<td> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""> </td>
<td width=""></td>
<td width=""></td>
<td width=""><div align="center"><div align="center"><font color="#3074A2" style="font-size:9pt;color:#000000">退出</font></div></td>
</tr>
</table>
</td>
</tr>
<tr>
<td background='images/bodybackground.jpg' width='100%' nowrap colspan=''>
<table width='100%' cellpadding="" cellspacing="" border=''>
<tr>
<td background='images/bodybackground.jpg' nowrap width=''>
<font style='font-size:9pt'>导航区菜单</font> </td>
<td width='100%' align='right' height=''>
<iFRAME width='100%' height='100%' scrolling='no' frameborder='' NAME="CurSite" SRC="current_site.jsp"></iframe>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width="100%" cellpadding="" cellspacing="" height="" align="center" topmargin='' border=''>
<tr>
<td width="" background='images/bodybackground.jpg' cellpadding="" cellspacing=""> </td>
<td valign='top' align='left' name="frmTitle1" id=frmTitle1 background="images/bodybackground.jpg" style="width:177">
<iFRAME width='100%' height='100%' scrolling='auto' frameborder='' NAME="index_tree" SRC="index_tree1.jsp" style="HEIGHT: 100%; VISIBILITY: inherit; WIDTH: 177px; Z-INDEX: 2"></iframe>
</td>
<TD background='images/bodybackground.jpg' width="">
<TABLE border= cellPadding= cellSpacing=>
<TBODY>
<TR>
<TD onclick=switchSysBar() style="HEIGHT: 100%;WIDTH: 100%">
<SPAN class=navPoint id=switchPoint title=关闭/打开左栏></SPAN>
</TD>
</TR>
</TBODY>
</TABLE>
</TD>
<TD width=''>
<TABLE border= cellPadding= cellSpacing= height='100%' width="">
<TR>
<TD background='images/main_left_top.jpg' height='' width=''>
</TD>
</TR>
<TR>
<TD background='images/main_left_middle.jpg' height='100%' width=''>
</TD>
</TR>
<TR>
<TD background='images/main_left_bottom.jpg' height='' width=''>
</TD>
</TR>
</TABLE>
</TD>
<TD width="100%" height='100%' valign='bottom'> <table border='' cellpadding= cellspacing= height='100%' width='100%'>
<tr>
<td background='images/main_top_center.jpg' height='' width='' colspan=''></td>
</tr>
<tr>
<td height='100%' width='100%' colspan=''><iframe width='100%' height='100%' scrolling='no' frameborder='' name="main1" ></iframe>
</td>
</tr>
<tr>
<td background='images/main_bottom_center.jpg' height='' valign='bottom' width='100%'></td>
<td background='images/main_bottom_center_right.jpg' height='' width='' valign='bottom'></td>
</tr>
</table></TD>
<TD width="" height='100%'>
<TABLE border= cellPadding= cellSpacing= height='100%'>
<TR>
<TD background='images/main_right_top.jpg' height='' width=''>
</TD>
</TR>
<TR>
<TD background='images/main_right_middle.jpg' height='100%'>
</TD>
</TR>
<TR>
<TD background='images/main_right_bottom.jpg' height=''>
</TD>
</TR>
</TABLE>
</TD> </tr>
<tr>
<td height="" colspan="" align='right' background='images/bodybackground.jpg'><font style='font-size:9pt'> 第一开发小组 </font></td>
</tr>
</table>
</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.caiduping.entity.Canton"%>
<%@ page import="java.util.List"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head> <body>
<table width="" height="" border="" style="border-collapse:collapse">
<tr>
<td height="" colspan="" align="right">
<input name="bt1" type="button" id="bt1" value="新增行政区划" onClick="javascript:window.location.href='addCanton.jsp'">
</td>
</tr>
<tr>
<td width=""><div align="center"> 行政区代码 </div></td>
<td width=""><div align="center">行政区名称</div></td>
<td width=""><div align="center">政府名称</div></td>
<td width=""><div align="center">国土部门名称</div> <div align="center"></div></td>
<td width=""><div align="center">上级行政区代码</div> </td>
<td>操作</td>
</tr><%List<Canton> list=(List<Canton>)request.getAttribute("list"); %> <!-- 定义一个canton 列表,将request中的列表都出来。-->
<%for(Canton ct:list){ %> <!-- 读取列表所有内容输出 -->
<tr>
<td><div align="center"><%=ct.getCtn_code() %></div></td>
<td><div align="center"><SPAN class=content><%=ct.getCtn_name() %></SPAN></div></td>
<td><div align="center"> <%=ct.getGov_name() %></div></td>
<td><div align="center"><%=ct.getLand_dp_name() %></div></td>
<td><div align="center"><%=ct.getParent_code() %></div></td>
<td><a href="cantondel?ctn_code=<%=ct.getCtn_code() %>">删除</a></td>
</tr><%} %> </table>
</body>
</html>
listCanton.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.laozhu.entity.Canton"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>
</title>
<style type="text/css">
<!--
.style1 {color: #FF0000}
.style2 {font-size: 14px}
-->
</style>
</head> <body>
<script language="javascript">
function check(){
if (form1.title.value==""){
alert("请输入新闻标题")
form1.title.focus();
return false;
}
if (form1.content.value==""){
alert("请输入内容新闻内容");
form1.content.focus();
return false;
}
}
</script> <% Canton c=(Canton)request.getAttribute("canton"); %>
<table width="" border="">
<tr><td width="" align="left"><form action="CantonupdateServlet" method="post" name="form1">
<table width="" height="" border="" align="left" style="border-collapse:collapse">
<tr align="left">
<td height="" colspan=""><span class="titletxt">行政区划信息修改(以下带<span class="style1">*</span>为必填项)</span></td>
</tr>
<tr>
<td width="" height=""><span class="style2"> 行政区代码</span></td>
<td width=""><input type="hidden" name="ctn_code" id="ctn_code" value=<%=c.getCtn_code()%>>
<span class="style1">*</span>
</td>
</tr>
<tr>
<td width="" height=""><span class="style2"> 行政区名称 </span></td>
<td><input name="ctn_name" type="text" id="ctn_name" value="<%=c.getCtn_name() %>" size="">
<span class="style1">*
</span></td>
</tr>
<tr>
<td width="" height=""><span class="style2"> 政府名称</span></td>
<td><input name="gov_name" type="text" id="gov_name" size="" value="<%=c.getGov_name()%>"></td>
</tr>
<tr>
<td width="" height=""><span class="style2"> 国土部门名称</span></td>
<td><input name="land_dp_name" type="text" id="land_dp_name" size="" value="<%=c.getLand_dp_name()%>"></td>
</tr>
<tr>
<td width="" height=""><span class="style2"> 上级行政区代码</span></td>
<td><input name="parent_code" type="text" id="parent_code" size="" value=<%=c.getParent_code() %>></td>
</tr>
<tr>
<td> </td>
<td><input name="bt1" type="submit" id="bt1" value="确定"></td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>
editcanton.jsp
package com.laozhu.servlet; import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.laozhu.Dao.CantonDaoImp;
import com.laozhu.entity.Canton; public class CantonshowServlet extends HttpServlet { /**
* Constructor of the object.
*/
public CantonshowServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);//以便不管提交数据是什么方式均专一到dopost
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long a=Integer.parseInt(request.getParameter("ctn_code"));
CantonDaoImp cantonService = new CantonDaoImp(); //实例化CantonDaoImp,工程中可以写一个接口。
Canton c = cantonService.showCanton(a); //建立一个Canton列表 ,调用CantonDaoImp方法返回這个列表
request.setAttribute("canton", c);//放置這个列表到request对象中
request.getRequestDispatcher("editcanton.jsp").forward(request, response);//转向到listCanton.jsp
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
CantonshowServlet类
package com.laozhu.servlet; import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.laozhu.Dao.CantonDaoImp;
import com.laozhu.entity.Canton; public class CantonUpdateServlet extends HttpServlet { /**
* Constructor of the object.
*/
public CantonUpdateServlet() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);//以便不管提交数据是什么方式均专一到dopost
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long a=Integer.parseInt(request.getParameter("ctn_code"));
CantonDaoImp cantonService = new CantonDaoImp(); //实例化CantonDaoImp,工程中可以写一个接口。
Canton c = new Canton();
c.setCtn_code(Integer.parseInt(request.getParameter("ctn_code")));
c.setCtn_name(request.getParameter("ctn_name"));
c.setGov_name(request.getParameter("gov_name"));
c.setLand_dp_name(request.getParameter("land_dp_name"));
c.setParent_code(request.getParameter("parent_code"));
//建立一个Canton列表,调用CantonDaoImp方法返回這个列表
try {
cantonService.updateCanton(c);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("CantonListServlet").forward(request, response);//转向到listCanton.jsp } /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
CantonUpdateServlet类
运行界面图:
JDBC之数据连接的更多相关文章
- 帆软报表FineReport中数据连接的JDBC连接池属性问题
连接池原理 在帆软报表FineReport中,连接池主要由三部分组成:连接池的建立.连接池中连接使用的治理.连接池的关闭.下面就着重讨论这三部分及连接池的配置问题. 1. 连接池原理 连接池技术的核心 ...
- jdbc数据连接池dbcp要导入的jar包
jdbc数据连接池dbcp要导入的jar包 只用导入commons-dbcp-x.y.z.jarcommons-pool-a.b.jar
- 帆软报表FineReport中数据连接之Weblogic配置JNDI连接
1. 制作报表的原理 在帆软报表FineReport设计器中先用JDBC连接到数据库,建立数据库连接,然后用SQL或者其他方法创建数据集,使用数据集制作报表,然后把建立的数据库连接从JDBC连接改成J ...
- 帆软报表FineReport中数据连接之Jboss配置JNDI连接
使用sqlsever 2000数据库数据源来做实例讲解,帆软报表FineReport数据连接中Jboss配置JNDI大概的过程和WEBSPHERE以及WEBLOGIC基本相同,用JDBC连接数据库制作 ...
- 帆软报表FineReport中数据连接之Websphere配置JNDI连接
以oracle9i数据源制作的模板jndi.cpt为例来说明如何在FineReport中的Websphere配置JNDI连接.由于常用服务器的JNDI驱动过大,帆软报表FineReport中没有自带, ...
- 帆软报表FineReport中数据连接之Tomcat配置JNDI连接
1. 问题描述 在帆软报表FineReport中,通过JNDI方式定义数据连接,首先在Tomcat服务器配置好JNDI,然后在设计器中直接调用JNDI的名字,即可成功使用JNDI连接,连接步骤如下: ...
- Netbeans 中创建数据连接池和数据源步骤(及解决无法ping通问题)
1.启动glassfish服务器, 在浏览器的地址栏中输入 http://localhost:4848 2.首先建立JDBC Connection Pools: 3.new 一个Connectio P ...
- jdbc事务处理和连接池
JDBC: * JDBC概念:Java DataBase Connectivity(Java数据库连接) SUN公司提供的一组连接数据库API. * JDBC开发步骤: * 1.注册驱动. * 2.获 ...
- jdbc基础 (五) 连接池与数据源 DBCP以及C3P0的使用
一.连接池的概念和使用 在实际应用开发中,特别是在WEB应用系统中,如果JSP.Servlet或EJB使用JDBC直接访问数据库中的数据,每一次数据访问请求都必须经历建立数据库连接.打开数据库.存取数 ...
随机推荐
- ZendFramework2 与MongoDB的整合
从网上找了很多文章,先是直接搜关键字找zf2与mongoDB的文章,然后回到源头先学习了一下mongoDB是什么,以及纯PHP环境下怎么用,又从github上找了几个mongoDB的zf2模块,还FQ ...
- 序列化对象C++对象的JSON序列化与反序列化探索
新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正 一:背景 作为一名C++开发人员,我始终很期待能够像C#与JAVA那样,可以省力的进行对象的序列化与反序列化,但到现在为止,还没有找 ...
- Apache Httpd通过mod_jk连接多个Tomcat
一个tomcat能够配置多个web apps,这是众所周知的.当更改了一个web app,想要又一次启动的时候.因为全部的web apps都是放在同一个tomcat下的,所以别的web apps也在重 ...
- Android短信彩信收发流程(应用层)
下图为ComposeMessageActivity中confirmSendMessageIfNeeded部分的信息发送流程.主要以接收者有效性的确认为主,然后转向sendMessage方法进行发送. ...
- oracle internal :VIEW: X$KCBLDRHIST - Direct Read HISTory
WebIV:View NOTE:159900.1 Note (Sure) - Note Mods - Note Refs Error ORA 600 TAR TAR-Info Bug B ...
- ios5 xcode 4.2 中 release显示编译警告或错误的解决方法
转自:http://lizi464789754.blog.163.com/blog/static/1689370852011924113245778/ 由于 iOS5 xcode4.2 引入了ARC ...
- c语言字符串实例
例子:涉及字符串.字符.指针.++等 例一:字符串与字符 #include <stdio.h> void reverse(char *str) { char *end=str; print ...
- struts2简单示例
今天写一个struts2的例子,目的是为了让大家明白struts2的基本流程,其实框架没有大家想象的那么难,说白了struts2的本质就是一个大的Servlet,即原本需要提交到Servlet处理的部 ...
- 线程NSThread的使用
// // ZYThreadViewController.h // Thread // // Created by yejiong on 15/11/4. // Copyright © 2015年 z ...
- Http Statis 500 -错误笔记
HTTP Status 500 - type Exception report message description The server encountered an internal error ...