在jsp中使用JavaBean连接数据库

发布网友

我来回答

3个回答

懂视网

技术分享技术分享
一文件上传:利用jspSmartUpload.jar 技术分享
shangchuan.jsp

<%@ page contentType="text/html; charset=gbk" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="config.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>上传文件</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
extArray = new Array(".rar", ".zip",".txt");
function LimitAttach(form, file) {
allowSubmit = false;
if (!file)
 return;
while (file.indexOf("\") != -1)
file = file.slice(file.indexOf("\") + 1);
ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { 
allowSubmit = true;
 break;
 }
}
if (allowSubmit)
 form.submit();
else
alert("对不起,只能上传以下格式的文件: " 
+ (extArray.join(" ")) + "
请重新选择符合条件的文件"
+ "再上传.");
}
// End -->
/*
	验证大小
*/
 var isIE = /msie/i.test(navigator.userAgent) && !window.opera;  
 function fileChange(target) { 
 
 var fileSize = 0;  
 if (isIE && !target.files) { 
 var filePath = target.value; 
 var fileSystem = new ActiveXObject("Scripting.FileSystemObject");  
 var file = fileSystem.GetFile (filePath); 
 fileSize = file.Size; 
 } else { 
 fileSize = target.files[0].size; 
 } 
 var size = fileSize / 1024; 
 if(size>5000){ 
 alert("附件不能大于5M"); 
  
  
 } 
 
} 
</script>
<link rel="stylesheet" type="text/css" href="styles.css">


 <style type="text/css">
<!--
body {
	background-color:#6F00D2;
	background-image: url(image/aaa.jpg);
}

.ww1 {
	height: 30px;
	width: 180px;
}

</style> 
</head>

<body>
<div align="center">
 <h1 ><font color="#FF0066">文件上传</font></h1> 
</div>
<table width="810" height="241" border="1" align="center">
 <tr>
 <td align="center">
 <form method=post name=upform action="http://127.0.0.1:8080/Zhu/shangchuanb.jsp" enctype="multipart/form-data">
 <input type=file name=uploadfile onchange="fileChange(this);"/> 
 
 <p>
 
 
 <button onclick="LimitAttach(this.form, this.form.uploadfile.value)" style="background: #2BA82E; padding: 7px 10px; border-radius: 4px; border: 1px solid #2BA82E; border-image: none; color: rgb(255, 255, 255); font-weight: bold;" ><FONT size="-3">上传</FONT></button>
 
 </p>
 
 </form></td>
 </tr>
 	
</table>
</body>
</html>
shangchuanb.jsp(了上传的大小为5M)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="config.jsp"%>

<%@ page import="java.util.*,com.jspsmart.upload.SmartUpload"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 
 
 <title>My JSP 'shangchuan2.jsp' starting page</title>
 
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0"> 
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

 </head>
 
 <body>
 <%
 	String id = (String)session.getAttribute("user");
	// 新建一个SmartUpload对象
	SmartUpload su = new SmartUpload();
	// 上传初始化
	su.initialize(pageContext);
	// 设定上传
	// 1.每个上传文件的最大长度。
	su.setMaxFileSize(5000000);
	// 2.总上传数据的长度。
	su.setTotalMaxFileSize(150000000);
	// 3.设定允许上传的文件(通过扩展名)。
	su.setAllowedFilesList("rar,zip");
	// 4.设定禁止上传的文件(通过扩展名),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
	su.setDeniedFilesList("exe,bat,jsp,htm,html,asp,php,com");
	// 上传文件
	su.upload();
	// 将上传文件全部保存到指定目录
 
	String cqutroot = dirPath.replace('\', '/');

	su.save(cqutroot + "zhu/"+id);
	//su.save("zhu/"+id);
	su.save(su.getRequest().getParameter("zhu"));
	out.print("<script>");
	out.print("alert('文件上传成功!');");
	out.print("location.href='chuandi.jsp';");
	out.print("</script>");
%>
 </body>
</html>
上传成功后,显示上传的文件chuandi.jsp
<%@ page language="java" contentType="text/html; charset=gbk"
	pageEncoding="GB18030" import="java.sql.*,java.util.*,java.io.*"%>

<%@include file="config.jsp"%>

<%	
		String id = (String)session.getAttribute("user");
	String Save_Location=getServletContext().getRealPath("/")+"zhu//";
 try{
	if (!(new java.io.File(Save_Location).isDirectory())) {//如果文件夹不存在
	new java.io.File(Save_Location).mkdir(); //不存在 文件夹,则建立此文件夹
	new java.io.File((Save_Location)+id+"//").mkdir(); //创建文件夹,命名为当前用户的名字
 }
	else {//存在excel文件夹,则直接建立此文件夹 
	new java.io.File((Save_Location)+id+"//").mkdir(); //创建文件夹,命名为当前用户的名字
	}
	}catch(Exception e){
	e.printStackTrace(); //创建文件夹失败 
	out.print("error");
	return;
 }
 File userBox=new File((Save_Location)+id+"//");
 File userBoxfile[]=userBox.listFiles();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

		<title>欢迎使用网络硬盘</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<link rel="stylesheet" type="text/css" href="styles.css">
		<style type="text/css">
<!--
body 
{
	background-color: #D6D6D6;
	background-image: url(image/e.jpg);
}

.ww1 {
	height: 30px;
	width: 180px;
}
#apDiv1 {
	position: absolute;
	left: 657px;
	top: 70px;
	width: 408px;
	height: 247px;
	z-index: 1;
}
-->
</style>
	</head>
<body>

	
	 
	<h3 align="right"> <a href="update.jsp?userName=<%=session.getAttribute("user")%>">修改密码</a> <a href="out.jsp">退出登录</a></h3> 

<h1 class="headerLogo"><a title="后台管理系统" target="_blank" href="welcome.jsp"><img alt="logo" src="images/333.jpg" width="183" height="200"></img> <br /></a></h1>
		<br />
	<table width="825" height="204" border="1" align="center">
		<tr>
			<td height="26" align="left">
				<font color="red">欢迎
				<%=session.getAttribute("user")%>的到来!</font><font color="#0000ff">                                        </font>        
				
				<br>
		 </td>
	 </tr>
		<tr>
			<td height="58" align="left" valign="top"><a href="shangchuan.jsp"><img src="image/ppp.png" width="100" height="35"></a>
				<% for(int i=0;i<userBoxfile.length;i++){%>
 <tr><td height="22">
 
 
					<p>
					 <img alt="logo" src="image/yasuo.png" width="50" height="43"></img>
					     
						<font color="#FF00FF" size="7"><%=userBoxfile[i].getName()%></font></p></td><td height="25">
							<span style="font-size: 9pt">
										 
						
																
			a href="dowload.jsp?filename=<%=userBoxfile[i].getName()%>"><img src="image/pp.png" width="60" height="25"></a>
										
										
							</span></td>
										
										
										
					<td height="25"><span style="font-size: 9pt">
											
		<A style="background: #2BA82E; padding: 7px 10px; border-radius: 4px; border: 2px solid #2BA82E; border-image: none; color: rgb(255, 255, 255); font-weight: bold;" 

href="shanchu.jsp?action=confirm&filename=<%=userBoxfile[i].getName()%>" target=_top> <font size="-2" color="#FFFFFF">删除</font></A> 
											</a>
											
											
											</span>
										</td>
				
	 </tr>
					<%} %>
			</td>
		</tr>
 </table>


	</body>
</html>
最后项目展示: 技术分享技术分享 如有需要请留言!!

Jsp+javaBean+mysql的网络云盘

标签:

热心网友

这个是连接access的javaBean,首先配置access数据源,数据源名称设为info.mdb,此文件放在test文件夹下,编译后将生成的infoBean.class放在claeese下的test文件夹下即可。用access写个数据库info.mdb里面写个表student,两个字段id name。
package test;
import java.sql.*;
public class infoBean{
private String ab="sun.jdbc.odbc.JdbcOdbcDriver";
private String ac="JDBC:odbc:info.mdb";
Connection conn=null;
ResultSet rs=null;
Statement stmt;
public infoBean(){
try{
Class.forName(ab);}
catch(java.lang.ClassNotFoundException e){
System.out.println("infoBean():"+e.getMessage());}
}
public ResultSet executeQuery(String sql){
rs=null;
try{
conn=DriverManager.getConnection(ac);
stmt=conn.createStatement();
rs = stmt.executeQuery(sql);}
catch(SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());}
return rs;
}
public void executeUpdate(String sql)
{stmt=null;
rs=null;
try{
conn=DriverManager.getConnection(ac);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();}
catch(SQLException e1)
{System.err.println("executeUpdate:" + e1.getMessage()); }
}
public void closeStmt()
{
try{
stmt.close();
}
catch (SQLException e2)
{
e2.printStackTrace();
}
}

public void closeConn()
{
try{
conn.close();
}
catch (SQLException e3)
{
e3.printStackTrace();
}
}
}

在jsp页面中引用时,显示数据库内容:
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body><table>
<tr><th>11</th><th>11</th></tr>
<%
ResultSet rs=inBean.executeQuery("SELECT * FROM student");
while rs.next(){
%>
<tr><td><%=rs.getString(1)%></td><td><%=rs.getString(2)%></td></tr>
<%}%>
</table></body></html>
在jsp中删除,
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body>
<%
String id=request.getParameter("id");
inBean.executeUpdate("Delete from student where id='"+id+"'");
%>
</body></html>
其中id是上一个页面传过来的;
jsp中修改:
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body>
<%
String id=request.getParameter("id");
String name=request.getParameter("name");

inBean.executeUpdate("update student set name='"+name+"' where id='"+id+"'");
%>
</body></html>
我还有javaBean连接mysql,sql server的例子,也有关于javaBean完整简单的小项目,你要的话,给邮箱,发给你

热心网友

给你我自己写的一个简单的例子吧:
<%@ page contentType="text/html; charset=gb18030" language="java" import="java.util.*,java.sql.*,com.ncu.cn.model.*,com.ncu.cn.service.*" errorPage="" pageEncoding="GB18030"%>
<jsp:useBean id="s" class="com.ncu.cn.service.studentmanage" scope="request"></jsp:useBean>
<jsp:useBean id="stu" class="com.ncu.cn.model.student" scope="request"></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030" />
<title>学生基本信息管理</title>
<style type="text/css">
<!--
.STYLE1 {color: #3366FF}
-->
</style>
<%
if(session.getAttribute("username")==null){
response.sendRedirect("/StudentManage/index.jsp");
}
%>
</head>

<body>
<table width="100%" border="1" cellspacing="0">
<tr>
<td width="12%"><div align="center" class="STYLE1">学号</div></td>
<td width="12%"><div align="center" class="STYLE1">姓名</div></td>
<td width="12%"><div align="center" class="STYLE1">性别</div></td>
<td width="8%"><div align="center" class="STYLE1">年龄</div></td>
<td width="16%"><div align="center" class="STYLE1">学院</div></td>
<td width="18%"><div align="center" class="STYLE1">班级</div></td>
<td width="22%"><div align="center" class="STYLE1">修改/删除</div></td>
</tr>
<%
ArrayList<student> l = s.getAllstudentinfo();
ListIterator i = l.listIterator();
while(i.hasNext()){
stu = (student)i.next();
out.println("<tr>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSno()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSname()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSsex()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSage()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSdept()+"</td>");
out.println("<td><div align='center' class='STYLE5'>"+stu.getSclass()+"</td>");
out.println("<td><div align='center' class='STYLE5'><a href='stuUpdate.jsp?Sno="+stu.getSno()+"'>更改</a> <a href='stuDel.jsp?Sno="+stu.getSno()+"'>删除</a></td></tr>");
}
%>
</table>
<p align="center"><span class="STYLE7"><a href="stuadd.jsp">增加学生</a></span></p>

</body>
</html>

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com