package cn.edu.bzu.DAO;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
//增删改查的实现类
public class CURDRealize implements CURDInterface{
PreparedStatement pstate=null;
ResultSet set=null;
DBConnection dbcon=new DBConnection();
//注册新博客
public int create(BlogUser bloguser) throws Exception {
String order="insert into bloginfor(username,upassword,blogname,blogdiscription,blogheadimage) values(?,?,?,?,?)";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,bloguser.getUsername());
pstate.setString(2,bloguser.getUpassword());
pstate.setString(3,bloguser.getBlogname());
pstate.setString(4,bloguser.getBlogdiscription());
pstate.setString(5,bloguser.getHeadimagepath());
int n=pstate.executeUpdate();
dbcon.closeConnection();
return n;
}
//用户登录时,用于用户查询
public BlogUser retrive(LoginUser loginuser) throws Exception {
String order="select * from bloginfor where username=? and upassword=?";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,loginuser.getUsername());
pstate.setString(2,loginuser.getUpassword());
BlogUser bloguser=null;
set=pstate.executeQuery();
while(set.next()){
bloguser=new BlogUser();
bloguser.setBlogname(set.getString("blogname"));
bloguser.setBlogdiscription(set.getString("blogdiscription"));
bloguser.setHeadimagepath(set.getString("blogheadimage"));
}
return bloguser;
}
//添加日志
public int create(Story story)throws Exception{
String order="insert into storyinfor values(?,?,?,?)";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,story.getUsername());
pstate.setString(2,story.getStorytitle());
pstate.setString(3,story.getStorycontent());
pstate.setString(4,story.getCreatetime());
int n=pstate.executeUpdate();
pstate.close();
dbcon.closeConnection();
return n;
}
//查询日志、媒体信息
@SuppressWarnings("null")
public List<Story> retriveStory(String username)throws Exception{
String order="select * from storyinfor where username=?";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,username);
set=pstate.executeQuery();
List<Story> storylist=new ArrayList<Story>();
while(set.next()){
Story story=new Story();
story.setStorytitle(set.getString("storytitle"));
story.setStorycontent(set.getString("storycontent"));
story.setCreatetime(set.getString("createtime"));
storylist.add(story);
}
return storylist;
}
//上传图片
public int createFile(FileInfor fileinfor,String loginuser)throws Exception{
//
//System.out.println("当前用户名:"+loginuser);
//
String order="insert into photoinfor values(?,?,?,?)";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,loginuser);
pstate.setString(2,fileinfor.getTargetdir()+"\\"+fileinfor.getTargetFilename());
pstate.setString(3,fileinfor.getFileDiscription());
pstate.setString(4,fileinfor.getCreatetime());
//
//System.out.println(" 数据库文件路径:"+fileinfor.getTargetdir()+fileinfor.getTargetFilename());
//System.out.println("数据库文件描述:"+fileinfor.getFileDiscription());
//
int n=pstate.executeUpdate();
pstate.close();
dbcon.closeConnection();
return n;
}
//根据登录的用户名,查询其空间中图片
public List<FileInfor> retriveFile(LoginUser loginuser) throws Exception {
String order="select * from photoinfor where username=?";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,loginuser.getUsername());
set=pstate.executeQuery();
List<FileInfor> filelist=new ArrayList<FileInfor>();
FileInfor fileinfor=null;
while(set.next()){
fileinfor=new FileInfor();
fileinfor.setTargetdir(set.getString("photopath"));
fileinfor.setFileDiscription(set.getString("photodiscription"));
fileinfor.setCreatetime(set.getString("createtime"));
filelist.add(fileinfor);
}
return filelist;
}
//传递视频路径至数据库
public int createMedia(Media media)throws Exception{
String order="insert into mediainfor values(?,?,?,?)";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,media.getUsername());
pstate.setString(2,media.getMediapath());
pstate.setString(3,media.getVediodiscription());
pstate.setString(4,media.getCreatetime());
int n=pstate.executeUpdate();
pstate.close();
dbcon.closeConnection();
return n;
}
//查询视频信息
public List<Media> retriveMedia(String username)throws Exception{
String order="select * from mediainfor where username=?";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,username);
set=pstate.executeQuery();
List<Media> medialist=new ArrayList<Media>();
while(set.next()){
Media media=new Media();
media.setVediodiscription(set.getString("mediadiscription"));
media.setMediapath(set.getString("mediapath"));
media.setCreatetime(set.getString("createtime"));
medialist.add(media);
}
return medialist;
}
//存储音频文件
public int createRadio(FileInfor fileinfor,String username)throws Exception{
String order="insert into radioinfor values(?,?,?,?)";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,username);
pstate.setString(2,fileinfor.getTargetdir()+"\\"+fileinfor.getTargetFilename());
pstate.setString(3,fileinfor.getFileDiscription());
pstate.setString(4, fileinfor.getCreatetime());
int n=pstate.executeUpdate();
pstate.close();
dbcon.closeConnection();
return n;
}
//查询音频
public List<FileInfor> retriveRadio(String username)throws Exception{
String order="select * from radioinfor where username=?";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,username);
set=pstate.executeQuery();
List<FileInfor> radiolist=new ArrayList<FileInfor>();
while(set.next()){
FileInfor fileinfor=new FileInfor();
fileinfor.setTargetdir(set.getString("radiopath"));
fileinfor.setFileDiscription(set.getString("radiodiscription"));
fileinfor.setCreatetime(set.getString("createtime"));
radiolist.add(fileinfor);
}
return radiolist;
}
//根据title查询文章
public String retirveStoryforEditor(String titlewords)throws Exception{
String order="select storycontent from storyinfor where storytitle=?";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,titlewords);
String storycontent=null;
set=pstate.executeQuery();
if(set.next())
storycontent=set.getString("storycontent");
pstate.close();
dbcon.closeConnection();
return storycontent!=null ? storycontent:null;
}
//更新日志
public int updateStory(Story story)throws Exception{
String order="update storyinfor set username=?,storycontent=?,createtime=? where storytitle=?";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,story.getUsername());
pstate.setString(2,story.getStorycontent());
pstate.setString(3,story.getCreatetime());
pstate.setString(4,story.getStorytitle());
int n=pstate.executeUpdate();
pstate.close();
dbcon.closeConnection();
System.out.println(story.getUsername());
System.out.println("storycontent:"+story.getStorycontent());
System.out.println("storytime:"+story.getCreatetime());
System.out.println("storytitle:"+story.getStorytitle());
System.out.println("受影响的行数:"+n);
return n;
}
//删除日志
public int deleteStory(String storytitle)throws Exception{
String order="delete from storyinfor where storytitle=?";
pstate=dbcon.getConnection().prepareStatement(order);
pstate.setString(1,storytitle);
int n=pstate.executeUpdate();
pstate.