Jsp插入数据库数据,并且读取出来显示:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*,java.text.*,java.util.*"%>
<!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=utf-8">
<title>本页面提交</title>
</head>
<body>
<%
//以下是从数据库里面读取出来的数据
Connection connl=null;
PreparedStatement psl=null;
ResultSet rsl=null;
int pagesize=5;
int pagenum=1;
if(request.getParameter("pagenum")!=null){
pagenum=Integer.parseInt(request.getParameter("pagenum"));
}
try{
String url="jdbc:oracle:thin:@192.168.0.26:1521:orcl";
connl=DriverManager.getConnection(url,"zhang","zhang");
String sql="select * from ( select t.*,rownum rn from (select * from sub order by createtime desc)t where rownum<=?) where rn>?";
psl=connl.prepareStatement(sql);
psl.setInt(1,pagesize*(pagenum+1));
psl.setInt(2,pagesize*(pagenum));
rsl=psl.executeQuery();
while(rsl.next()){
out.println(rsl.getInt("id")+":");
out.println(rsl.getString("username")+":");
out.println(rsl.getString("age")+":");
out.println(rsl.getString("introduce")+":");
out.println(rsl.getString("createtime")+":");
out.println("<br/>");
}
out.println("<a href='sub.jsp?pagenum="+(pagenum+1)+"'>"+"上一页"+"</a>"+"|");
out.println("<a href='sub.jsp?pagenum="+(pagenum-1)+"'>"+"下一页"+"</a>");
out.println("<br/>");
}catch(Exception e){
e.printStackTrace();
}finally{
if(rsl!=null){
rsl.close();
}
if(psl!=null){
psl.close();
}
if(connl!=null){
connl.close();
}
}
%>
<form id="iform" method="post" action="subm.jsp">
用户名:<br/>
<input type="text" name="username" /><br/>
年龄<br/>
<input type="text" name="age" /><br/>
个人介绍<br/>
<br/>
<textarea name="introduce" cols="10" rows="5"></textarea>
<!-- <input type="text" name="introduce" /> -->
<input type="submit" value="提交"/>
</form>
<%
// Calendar cal=Calendar.getInstance();
java.util.Date date=new java.util.Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
out.println(sdf.format(date));
%>
</body>
</html>
//subm.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.sql.*,java.text.*,java.util.*"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String username=request.getParameter("username");
int age=Integer.parseInt(request.getParameter("age"));
String introduce=request.getParameter("introduce");
Connection conn=null;
PreparedStatement ps=null;
// ResultSet rs=null;
try{
String url="jdbc:oracle:thin:@192.168.0.26:1521:orcl";
conn=DriverManager.getConnection(url,"zhang","zhang");
String sql="insert into sub (username,age,introduce) values (?,?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1,username);
ps.setInt(2,age);
ps.setString(3,introduce);
ps.executeUpdate();
response.sendRedirect("sub.jsp");
}catch(Exception e){
e.printStackTrace();
}finally{
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
}
%>
</body>
</html>