📄 downloadado.java
字号:
package com.x3408.download;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import com.x3408.database.CNProvider;
import com.x3408.office.Constants;
public class DownLoadADO {
public static boolean fileInsert(FileInfo fileInfo) {
Connection conn = CNProvider.getConnection();
PreparedStatement pstat = null;
if (fileInfo.isValid()) {
try {
pstat = conn
.prepareStatement("insert into downLoad(name,upLoadPath,caption,introduce,upLoadTime)values(?,?,?,?,?)");
pstat.setString(1, fileInfo.getName());
pstat.setString(2, fileInfo.getUpLoadPath());
pstat.setString(3, fileInfo.getCaption());
pstat.setString(4, fileInfo.getIntroduce());
pstat.setString(5, fileInfo.getUpLoadTime());
pstat.executeUpdate();
return true;
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} finally {
CNProvider.release(pstat, conn);
}
}
return false;
}
public static boolean fileDelete(String pFileID) {
int fileID = 0;
Connection conn = CNProvider.getConnection();
PreparedStatement pstat = null;
if (pFileID == null || "".equals(pFileID)) {
return false;
}
try {
fileID = Integer.parseInt(pFileID);
} catch (Exception e) {
e.printStackTrace();
System.out.print("在DownLoadADO中的fileDelete方法中将字符串转换为整型数据时操作失败");
return false;
}
FileInfo fileInfo=fileQuery(pFileID);
if(fileInfo!=null&&fileID!=0){
try {
pstat = conn
.prepareStatement("delete from downLoad where fileID=?");
pstat.setInt(1, fileID);
pstat.executeUpdate();
java.io.File myFile=new java.io.File(fileInfo.getUpLoadPath()+fileID+"_"+fileInfo.getName());
myFile.delete();
System.out.println("文件:"+fileID+"_"+fileInfo.getUpLoadPath()+fileInfo.getName()+" 已删除");
return true;
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} finally {
CNProvider.release(pstat, conn);
}
}
return false;
}
public static FileInfo fileQuery(String pFileID) {
int fileID = 0;
FileInfo fileInfo = null;
Connection conn = CNProvider.getConnection();
PreparedStatement pstat = null;
ResultSet rs = null;
try {
fileID = Integer.parseInt(pFileID);
} catch (Exception e) {
e.printStackTrace();
System.out.print("在DownLoadADO中的fileQuery方法中将字符串转换为整型数据时操作失败");
}
try {
pstat = conn
.prepareStatement("select * from downLoad where fileID=?");
pstat.setInt(1, fileID);
rs = pstat.executeQuery();
if (rs.next()) {
fileInfo = new FileInfo(rs.getString("fileID"), rs
.getString("name"), rs.getString("upLoadPath"), rs
.getString("caption"), rs.getString("introduce"),rs.getString("upLoadTime"));
}
return fileInfo;
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} finally {
CNProvider.release(rs, pstat, conn);
}
return null;
}
public static String getNeedFileID() {
Connection conn = CNProvider.getConnection();
PreparedStatement pstat = null;
ResultSet rs = null;
try {
pstat = conn.prepareStatement("select @@IDENTITY AS 'fileID'");
rs = pstat.executeQuery();
if (rs.next()) {
return new Integer(rs.getInt("fileID")).toString();
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} finally {
CNProvider.release(rs, pstat, conn);
}
return null;
}
public static Vector allFileQuery() {
FileInfo fileInfo = null;
Vector<FileInfo> fileList = new Vector<FileInfo>();
Connection conn = CNProvider.getConnection();
ResultSet rs = null;
PreparedStatement pstat = null;
try {
pstat = conn.prepareStatement("select * from downLoad order by fileID desc");
rs = pstat.executeQuery();
while (rs.next()) {
fileInfo = new FileInfo(rs.getString("fileID"), rs
.getString("name"), rs.getString("upLoadPath"), rs
.getString("caption"), rs.getString("introduce"),rs.getString("upLoadTime"));
fileList.addElement(fileInfo);
}
return fileList.size()<1?null:fileList;
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} finally {
CNProvider.release(rs, pstat, conn);
}
return null;
}
public static int newFileCount(){
Connection conn=CNProvider.getConnection();
PreparedStatement pstat=null;
ResultSet rs=null;
int count=0;
try {
pstat=conn.prepareStatement("select count(*) from download where datediff(day,upLoadTime,getdate())<?");
pstat.setInt(1, Constants.NEWFILEDAYS);
rs=pstat.executeQuery();
if(rs.next()){
count=rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
CNProvider.release(rs,pstat,conn);
}
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -