package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.entity.Article;
/**
* 文章
* @author tanzh
*
*/
public class ArticleDao {
/**
* 保存
* @param var1
* @param var2
* @param var3
* @param var4
* @param var5
*/
public void save(String var1String var2String var3String var4String var5){
String sql = “insert into article(var1var2var3var4var5) values(?????)“;
Connection conn = DatabaseUtils.getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1 var1);
pstmt.setString(2 var2);
pstmt.setString(3 var3);
pstmt.setString(4 var4);
pstmt.setString(5 var5);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 根据id查询
* @param id
* @return
*/
public Article find(Integer id){
Article a = null;
String sql=“select * from (select a.ida.var1g.var1 var2a.var3a.var4a.var5 from article a left join genre g ON a.var2=g.id)e where e.id=“+id;
Connection conn = DatabaseUtils.getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
a=new Article();
a.setVar1(rs.getString(“var1“));
a.setVar2(rs.getString(“var2“));
a.setVar3(rs.getString(“var3“));
a.setVar4(rs.getString(“var4“));
a.setVar5(rs.getString(“var5“));
a.setId(rs.getInt(“id“));
}
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
/**
* 查询自己所有的文章
* @param userid
* @return
*/
public List findList(Integer userid){
List list=new ArrayList();
String sql = “select * from (select a.ida.var1g.var1 var2a.var3a.var4a.var5 from article a left join genre g ON a.var2=g.id)e“;
if(userid!=null){
sql=sql +“ where e.var1=‘“+String.valueOf(userid)+“‘“;
}
Connection conn = DatabaseUtils.getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
Article a = new Article();
a.setVar1(rs.getString(“var1“));
a.setVar2(rs.getString(“var2“));
a.setVar3(rs.getString(“var3“));
a.setVar4(rs.getString(“var4“));
a.setVar5(rs.getString(“var5“));
a.setId(rs.getInt(“id“));
list.add(a);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 根据id删除
* @param id
*/
public void del(Integer id){
String sql = “delete from article where id=?“;
Connection conn = DatabaseUtils.getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1 id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2019-01-03 11:40 blog_news\
文件 1202 2019-01-01 23:16 blog_news\.classpath
目录 0 2018-11-21 19:22 blog_news\.myeclipse\
文件 300 2018-11-21 18:33 blog_news\.myme
tadata
文件 1752 2017-01-06 20:47 blog_news\.project
目录 0 2019-01-03 11:40 blog_news\.settings\
文件 500 2017-01-06 20:46 blog_news\.settings\.jsdtscope
文件 247 2019-01-01 23:16 blog_news\.settings\com.genuitec.eclipse.migration.prefs
文件 70 2019-01-02 22:16 blog_news\.settings\org.eclipse.core.resources.prefs
文件 395 2017-01-06 20:46 blog_news\.settings\org.eclipse.jdt.core.prefs
文件 553 2019-01-01 23:16 blog_news\.settings\org.eclipse.wst.common.component
文件 359 2019-01-01 23:16 blog_news\.settings\org.eclipse.wst.common.project.facet.core.xml
文件 49 2017-01-06 20:46 blog_news\.settings\org.eclipse.wst.jsdt.ui.superType.container
文件 6 2017-01-06 20:46 blog_news\.settings\org.eclipse.wst.jsdt.ui.superType.name
目录 0 2019-01-03 11:40 blog_news\src\
目录 0 2019-01-03 11:40 blog_news\src\com\
目录 0 2019-01-03 11:40 blog_news\src\com\dao\
文件 3019 2017-01-06 20:46 blog_news\src\com\dao\ArticleDao.java
文件 2857 2017-01-06 20:46 blog_news\src\com\dao\CommentDao.java
文件 2607 2017-01-06 20:47 blog_news\src\com\dao\DatabaseUtils.java
文件 1581 2017-01-06 20:46 blog_news\src\com\dao\GenreDao.java
文件 3816 2017-01-06 20:46 blog_news\src\com\dao\UserDao.java
目录 0 2019-01-03 11:40 blog_news\src\com\entity\
文件 1251 2017-01-06 20:46 blog_news\src\com\entity\Article.java
文件 1082 2017-01-06 20:46 blog_news\src\com\entity\Comment.java
文件 544 2017-01-06 20:46 blog_news\src\com\entity\Genre.java
文件 1067 2017-01-06 20:46 blog_news\src\com\entity\User.java
目录 0 2019-01-03 11:40 blog_news\src\com\servlet\
文件 2914 2017-01-06 20:46 blog_news\src\com\servlet\ArticleServlet.java
文件 1958 2017-01-06 20:46 blog_news\src\com\servlet\CommentServlet.java
文件 1576 2017-01-06 20:46 blog_news\src\com\servlet\GenreServlet.java
............此处省略86个文件信息
评论
共有 条评论