package dq;
连接数据库
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
private String driverClassName = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/bick?useUnicode=true&characterEncoding=utf-8";
private String userName = "root";
private String password = "root";
private static ConnectionFactory connectionFactory=null;
private ConnectionFactory() {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException
{
return DriverManager.getConnection(url, userName, password);
}
public static ConnectionFactory getInstance()
{
if (null==connectionFactory) {
connectionFactory=new ConnectionFactory();
}
return connectionFactory;
}
}
JavaBean
package dq;
public class ArticleList {
private int id;
private String title;
private String desc;
private String author;
private String content;
private String thumb;
private int click;
private int zan;
private String time;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getThumb() {
return thumb;
}
public void setThumb(String thumb) {
this.thumb = thumb;
}
public int getClick() {
return click;
}
public void setClick(int click) {
this.click = click;
}
public int getZan() {
return zan;
}
public void setZan(int zan) {
this.zan = zan;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getCateid() {
return cateid;
}
public void setCateid(int cateid) {
this.cateid = cateid;
}
private int cateid;
}
封装数据库增删改成操作方法
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 dq.Book;
public class BookJdbcDao {
private PreparedStatement ptmt = null;
private PreparedStatement ps=null;
private ResultSet rs = null;
public BookJdbcDao() {
}
=======查询==========
public List<ArticleList> findArticleList(Connection conn) throws SQLException
{
String sql = "select * from bk_article ";
try{
ptmt = conn.prepareStatement(sql);
// 对SQL语句中的第一个占位符赋值
// 执行更新操作
rs= ptmt.executeQuery();
List<ArticleList> list = new ArrayList<ArticleList>();
while (rs.next()) {
ArticleList article = new ArticleList();
article.setId(rs.getInt("id"));
article.setTitle(rs.getString("title"));
article.setDesc(rs.getString("desc"));
article.setThumb(rs.getString("thumb"));
article.setZan(rs.getInt("zan"));
article.setTime(rs.getString("time"));
article.setClick(rs.getInt("click"));
list.add(article);
}
return list;
}finally{
if (null!=ptmt) {
ptmt.close();
}
if (null!=conn) {
conn.close();
}
}
}
=======修改==========
public void update(Connection conn, int id ,String content,String contro) throws SQLException
{
String sql = "update imglist set content=?,contro=? where id=?";
try {
// 获取PreparedStatement
ps = conn.prepareStatement(sql);
// 对SQL语句中的第一个参数赋值
ps.setString(1,content);
ps.setString(2,contro);
ps.setInt(3, id);
// 执行更新操作
ps.executeUpdate();
}finally{
if (null!=ps) {
ps.close();
}
if (null!=conn) {
conn.close();
}
}
}
=======删除==========
public void delete(Connection conn, int id) throws SQLException
{
String sql = "delete from imglist where id=?";
try{
ptmt = conn.prepareStatement(sql);
// 对SQL语句中的第一个占位符赋值
ptmt.setInt(1, id);
// 执行更新操作
ptmt.executeUpdate();
}finally{
if (null!=ptmt) {
ptmt.close();
}
if (null!=conn) {
conn.close();
}
}
}
}
jsp页面输出
<%@page import="java.sql.*"%>
<%@page import="java.util.List"%>
<%@page import="dq.*"%>
<%
BookJdbcDao b=new BookJdbcDao();
Connection conn=ConnectionFactory.getInstance().getConnection();
List<ArticleList> listFind=b.findArticleList(conn);
for (ArticleList list : listFind) {
%>
<div class="xh_post_h_3 ofh">
<div class="xh">
<a target="_blank" href="/lookbike/bmx/240.html" title="Alain Massabova: 40 Years in Paris BMX 视频">
<img src="./images/200.jpg" alt="Alain Massabova: 40 Years in Paris BMX 视频" height="240" width="400">
</a>
</div>
<div class="r ofh">
<h2 class="xh_post_h_3_title ofh" style="height:60px;">
<a target="_blank" href="article.jsp?artid=<%=list.getId()%>" title="<%=list.getTitle()%>"><%=list.getTitle()%></a>
</h2>
<span class="time"><%=list.getTime()%>年05月30日</span>
<div class="xh_post_h_3_entry ofh" style="color:#555;height:80px; overflow:hidden;">
<%=list.getDesc()%>
</div>
<div class="b">
<a href="article.jsp?artid=<%=list.getId()%>" class="xh_readmore" rel="nofollow">read more
</a>
<span title="<%=list.getZan()%>人赞" class="xh_love">
<span class="textcontainer">
<span><%=list.getZan()%></span>
</span>
</span>
<span title="<%=list.getClick()%>人浏览" class="xh_views">
<%=list.getClick()%></span>
</div>
</div>
</div>
<%
}
%>