资源描述
软件开发文档.docx
《软件开发文档.docx》由会员分享,可在线阅读,更多相关《软件开发文档.docx(21页珍藏版)》请在冰豆网上搜索。
软件开发文档
软件开发文档
一、数据库连接池
1、配置文件context.xml
-->
name="jdbc/cs"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:
oracle:
thin:
@192.168.3.81:
1521:
orcl"
username="rryycs"
password="rryycs"
maxActive="20"
maxIdle="10"
maxWait="-1"/>
2、web.xml
OracleDatasourceexample
jdbc/cs
javax.sql.DataSource
Container
3、创建数据库连接池
packagemon;
importjavax.naming.Context;
importjavax.naming.InitialContext;
importjavax.naming.NamingException;
importjavax.sql.DataSource;
publicclassConnectionPool{
privatestaticDataSourceds=null;
publicstaticDataSourcegetDataSource(){
ContextinitContext;
try{
initContext=newInitialContext();
ContextenvContext=(Context)initContext.lookup("java:
/comp/env");
ds=(DataSource)envContext.lookup("jdbc/cs");
}catch(NamingExceptione){
}
returnds;
}
}
4、取得连接工具类
packagemon;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
publicclassDBUtils{
privatestaticThreadLocalconns=newThreadLocal();
privatestaticConnectionconn=null;
privateDBUtils(){
}
//获取连接
publicstaticsynchronizedConnectiongetConn(){
conn=conns.get();
if(conn==null){
//判断连接池是否为空为空则手动创建连接
if(ConnectionPool.getDataSource()!
=null){
try{
conn=ConnectionPool.getDataSource().getConnection();
}catch(SQLExceptione){
e.printStackTrace();
}
}else{
try{
Class.forName(Constants.DBDRIVER);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
try{
conn=DriverManager.getConnection(Constants.DBURL,
Constants.DBUSER,Constants.DBPASS);
}catch(SQLExceptione){
e.printStackTrace();
}
}
System.out.println("---get:
"+conn.toString());
conns.set(conn);
}
returnconn;
}
//获取事务管理类
publicstaticsynchronizedTransactionManagergetTranManger(){
returnnewTransactionManager(getConn());
}
//关闭结果集、数据库连接
publicstaticvoidclose(ResultSetres,PreparedStatementpre,Connectionconn){
booleanflag=false;
//关闭结果集
if(res!
=null){
try{
res.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
//关闭PreparedStatement
if(pre!
=null){
try{
pre.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
//关闭数据库连接
try{
//判断是否开启了事务如果开启了事务则不关闭连接连接会在事务提交或回滚是关闭
if(conn.getAutoCommit()){
if((conn!
=null)&&(!
conn.isClosed())){
System.out.println("---close:
"+conn.toString());
conn.close();
}
flag=true;
}
}catch(SQLExceptione){
e.printStackTrace();
}finally{
if(flag){
conns.remove();
}
}
}
//关闭数据库连接
publicstaticvoidclose(){
conn=conns.get();
try{
if((conn!
=null)&&(!
conn.isClosed())){
System.out.println("---close:
"+conn.toString());
conn.close();
}
}catch(SQLExceptione){
e.printStackTrace();
}finally{
conns.remove();
}
}
}
5、开启事物
packagemon;
importjava.sql.Connection;
importjava.sql.SQLException;
publicclassTransactionManager{
privateConnectionconn;
protectedTransactionManager(Connectionconn){
this.conn=conn;
}
//开启事务
publicvoidbeginTransation(){
try{
//设置事务自动提交
if(conn.getAutoCommit()){
conn.setAutoCommit(false);
}
}catch(SQLExceptione){
e.printStackTrace();
}
}
//提交事务并关闭连接
publicvoidcommitAndClose(){
try{
if(conn!
=null){
mit();
}
}catch(SQLExceptione){
e.printStackTrace();
}finally{
if(conn!
=null){
DBUtils.close();
}
}
}
//回滚事务并关闭连接
publicvoidrollbackAndClose(){
try{
conn.rollback();
}catch(SQLExceptione){
e.printStackTrace();
}finally{
DBUtils.close();
}
}
}
二、文件上传预览
1、表单
none"/>
2、上传预览脚本
functionsetImagePreview(){
vardocObj=document.getElementById("doc");
varimgObjPreview=document.getElementById("preview");
if(docObj.files&&docObj.files[0]){//火狐下,直接设img属性
imgObjPreview.style.display="block";
imgObjPreview.style.width="100px";
imgObjPreview.style.height="100px";
//imgObjPreview.src=docObj.files[0].getAsDataURL();
//火狐7以上版本不能用上面的getAsDataURL()方式获取,需要一下方式
imgObjPreview.src=window.URL.createObjectURL(docObj.files[0]);
}else{//IE下,使用滤镜
docObj.select();
varimgSrc=document.selection.createRange().text;
varlocalImagId=document.getElementById("localImag");//必须设置初始大小
localImagId.style.width="100px";
localImagId.style.height="100px";//图片异常的捕捉,防止用户修改后缀来伪造图片
try{
localImagId.style.filter="progid:
DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src=imgSrc;
}catch(e){
alert("您上传的图片格式不正确,请重新选择!
");
returnfalse;
}
imgObjPreview.style.display='none';
document.selection.empty();
}
returntrue;
}
3、FileUpload文件
packagecom.rryy.util;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Random;
importjava.util.Set;
importjavax.servlet.http.HttpServletRequest;
importmons.fileupload.FileItem;
importmons.fileupload.disk.DiskFileItemFactory;
importmons.fileupload.servlet.ServletFileUpload;
publicclassFileUploadUtil{
privateHttpServletRequestrequest=null;
privateListitems=null;
privateMap>params=newHashMap>();
privateMapfiles=newHashMap();
publicFileUploadUtil(HttpServletRequestrequest,intmaxSize,StringtempDir)throwsException{
this.request=request;
//创建磁盘工厂
DiskFileItemFactoryfactory=newDiskFileItemFactory();
if(tempDir!
=null){
factory.setRepository(newFile(tempDir));
}
//创建处理工具
ServletFileUploadupload=newServletFileUpload(factory);
if(maxSize>0){
//设置上传文件an大小
upload.setFileSizeMax(maxSize);
}
//接受全部内容
this.items=upload.parseRequest(request);
this.init();
}
privatevoidinit(){
Iteratoriter=items.iterator();
while(iter.hasNext()){
FileItemitem=iter.next();
if(item.isFormField()){
Stringname=item.getFieldName();
Stringvalue=item.getString();
Listtemp=null;
if(this.params.containsKey(name)){
temp=this.params.get(name);
}else{
temp=newArrayList();
}
temp.add(value);
this.params.put(name,temp);
}else{
StringfileName=getFileName()+"."+item.getName().split("\\.")[1];
this.files.put(fileName,item);
}
}
}
privateStringgetFileName(){
StringBufferfileName=newStringBuffer();
SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMddHHmmssSSS");
fileName.append(sdf.format(newDate()));
Randomrandom=newRandom();
for(inti=0;i<3;i++){
fileName.append(random.nextInt(10));
}
returnfileName.toString();
}
publicStringgetParameters(Stringname){
Stringret=null;
Listitem=this.params.get(name);
if(item!
=null){
ret=item.get(0);
}
returnret;
}
publicString[]getParameterValues(Stringname){
String[]ret=null;
Listitem=this.params.get(name);
if(item!
=null){
ret=item.toArray(newString[]{});
}
returnret;
}
publicMapgetUploadFiles(){
returnthis.files;
}
publicListsaveAll(StringsaveDir)throwsException{
Listnames=newArrayList();
if(this.files.size()>0){
Setkeys=this.files.keySet();
Iteratorite=keys.iterator();
FilesaveFile=null;
InputStreamin=null;
OutputStreamout=null;
while(ite.hasNext()){
FileItemitem=this.files.get(ite.next());
StringfielName=getFileName()+"."+item.getName().split("\\.")[1];
saveFile=newFile(saveDir+fielName);
names.add(fielName);
try{
in=item.getInputStream();
out=newFileOutputStream(saveFile);
byte[]b=newbyte[1024];
inttemp=0;
while((temp=in.read(b))!
=-1){
out.write(b,0,1024);
}
}catch(Exceptione){
throwe;
}finally{
in.close();
out.close();
}
}
}
returnnames;
}
}
三、常用正则表达式
1、常用正则表达式
空行:
/^\s*$/g
Email地址:
/[a-za-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+/
四、常用正则表达式
1、导入的jQuery.js与jquery.validate.js等文件,注意顺序不要颠倒
2、指定哪个(或哪些)表单要在提交前先进行验证
$(function(){
$("#testForm").validate();
});
3、指定每个字段的验证规则
名称*
class="{required:
true,accept:
'gif',mess