?? newsmgr.java
字號:
package com.kjlm.beans;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
public class NewsMgr {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public Boolean exist(News news) {
// TODO Auto-generated method stub
Boolean flag = false;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/news?user=root&password=3819568";
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
String title = news.getTitle();
String sql = "select * from news where title ='" + title + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
flag = true;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
public void insert(News news) {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/news?user=root&password=3819568";
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
String title = news.getTitle();
String content = news.getContent();
int categoryId = news.getCategoryId();
String sql = "insert into news values(null,'" + title + "','"
+ content + "','" + categoryId + "',now() )";
stmt.executeUpdate(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public int getCount(int categoryId) {
int count = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/news?user=root&password=3819568";
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
String sql = "select count(*) from news where categoryId = "
+ categoryId ;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
count = rs.getInt(1);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return count;
}
public Collection getAllNewsByCategoryId(int page, int pageSize,
int categoryId, int pageTotalNum) {
Collection<News> c = new ArrayList<News>();
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/news?user=root&password=3819568";
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
// String sql = "select * from news limit " + (page - 1) * pageSize
// + "," + pageSize + " where category = '" + categoryName
// + "'";
if(page>pageTotalNum){
page =pageTotalNum;
}
String sql = "select * from news where categoryId = " + categoryId + " limit " + (page-1)*pageSize
+ "," + pageSize;
ResultSet rs = stmt.executeQuery(sql);
News news = null;
while (rs.next()) {
news = new News();
news.setId(rs.getInt("id"));
news.setTitle(rs.getString("title"));
news.setContent(rs.getString("content"));
news.setCategoryId(categoryId);
c.add(news);
news = null;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return c;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -