首页 > 其他分享 >servlet实现

servlet实现

时间:2022-12-22 14:14:33浏览次数:47  
标签:String getString rs 实现 request getParameter null servlet

续上篇相关代码如下:

<%@ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="lei.lei" %>
<%@page import="java.sql.*" %>
<%@page import="lian.lian" %>
<head>

<meta charset="UTF-8" >
<title>人口普查系统</title>
</head>
<body>

<%
//request.setCharacterEncoding("utf-8");
String zhuti=request.getParameter("zhuti");
String mude=request.getParameter("mude");
String leixing=request.getParameter("leixing");
String shijian=request.getParameter("shijian");
String didian=request.getParameter("didian");
String duixiang=request.getParameter("duixiang");
String neirong=request.getParameter("neirong");
String richenganpai=request.getParameter("richenganpai");

lei shu=new lei(zhuti,mude,leixing,shijian,didian,duixiang,neirong,richenganpai);
lian lian=new lian();
int result=lian.login(shu);
if(result>0){

out.print("添加成功");

%>
<br/>
<%

}else{
out.print("信息添加有误");

%>
<a href="fabu.jsp">返回上一页面</a>
<br/>
<%
}

%>
<%
String URL = "jdbc:mysql://localhost:3306/shetuan ";
String USERNAME = "root";
String PWD = "PENGsuoqun123";

//jsp就是在html中嵌套的java代码,因此java代码可以写在jsp中(《%%》)
PreparedStatement pstmt = null;
ResultSet rs=null;
Connection connection = null;

try {

// a.导入驱动,加载具体驱动类

Class.forName("com.mysql.cj.jdbc.Driver");//注册// 加载具体的驱动类
// b.与数据库建立链接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
String sql = "select * from xinxi";

pstmt=connection.prepareStatement(sql);
rs= pstmt.executeQuery();// 反回值,增删改了几条数据增删改用update



%>
<table align="center" border="1px">
<tr><td align="center">活动主题</td>
<td align="center">活动目的</td>
<td align="center">活动类型</td>
<td align="center">活动时间</td>
<td align="center">活动地点</td>
<td align="center">活动对象</td>
<td align="center">活动内容</td>
<td align="center">活动日程安排</td>

</tr>
<%
while(rs.next()) {

String zhuti1=rs.getString("zhuti");
String mude1=rs.getString("mude");
String leixing1=rs.getString("leixing");
String shijian1=rs.getString("shijian");
String didian1=rs.getString("didian");
String duixiang1=rs.getString("duixiang");
String neirong1=rs.getString("neirong");
String richenganpai1=rs.getString("richenganpai");

%>






<tr>
<td align="center"><%out.print(zhuti1);%></td>
<td align="center"><%out.print(mude1);%></td>
<td align="center"><%out.print(leixing1);%></td>
<td align="center"><%out.print(shijian1);%></td>
<td align="center"><%out.print(didian1);%></td>
<td align="center"><%out.print(duixiang1);%></td>
<td align="center"><%out.print(neirong1);%></td>
<td align="center"><%out.print(richenganpai1);%></td>

</tr>
<%} %>
</table>


<%



}
catch (ClassNotFoundException e) {
e.printStackTrace();

}
catch (SQLException e) {
e.printStackTrace();

}
catch (Exception e) {
e.printStackTrace();

}

finally {
// 关闭数据库
try{//先开的后关
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();// 对象.方法

if(connection!=null)connection.close();
}
catch( SQLException e){
e.printStackTrace();

}


}


%>


</body>

 

标签:String,getString,rs,实现,request,getParameter,null,servlet
From: https://www.cnblogs.com/pengsuoqun123/p/16998523.html

相关文章