package com.kgc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;
public class NewsDao {
Connection cntn=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement pstmt=null;
// 公共代码:连接数据库
public void getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/kgcnews";
cntn=DriverManager.getConnection(url, "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//查询全部新闻 可以使用 Statement 或者 PreparedStatement
public void queryAll(){
try {
//加载mysql驱动
Class.forName("com.mysql.jdbc.Driver");
//获得连接Connection对象
String url="jdbc:mysql://localhost:3306/kgcnews";
cntn=DriverManager.getConnection(url,"root","root");
//创建 statement对象 或者 preparedStatement 对象
stmt=cntn.createStatement();
//调用Statement对象的 执行方法 executeQuery
String sql="SELECT id , categoryId,title,summary,content,author,createDate FROM news_detail";
rs=stmt.executeQuery(sql);
//控制台 输出 查询数据
while(rs.next()){
int id=rs.getInt("id");
int categoryId=rs.getInt("categoryId");
String title=(String)rs.getObject("title");
String summary=rs.getString("summary");
String content=rs.getString("content");
String author=rs.getString("author");
Date createDate=rs.getDate("createDate");
System.out.println(id+"\t"+categoryId+"\t"+title+"\t"+summary+"\t"+content+"\t"+author+"\t"+new Timestamp(createDate.getTime()));
}
} 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(cntn!=null){
try {
cntn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//查询部分新闻 使用 PreparedStatement ,更安全 不要使用 Statement
public void queryTitle(String title){
try {
//加载mysql 数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获得Connection对象
String url="jdbc:mysql://localhost:3306/kgcnews";
cntn=DriverManager.getConnection(url,"root","root");
//获得PreparedStatement对象
// where title=' 开课啦'or '1'='1 ' 为真
String sql="select id, title from news_detail where title=?";
pstmt=cntn.prepareStatement(sql);
pstmt.setString(1, title);
rs=pstmt.executeQuery();
while(rs.next()){
int id=rs.getInt("id");
String title1=rs.getString("title");
System.out.println(id+"\t"+title1);
}
} 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(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cntn!=null){
try {
cntn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//增加新闻信息
public void addNews(int id, int categoryId, String title,
String summary, String content,String author,Date createDate){
try {
getConnection();
String sql="insert into news_detail(id,categoryId,title,summary,content,author,createDate) "
+ "values(?,?,?,?,?,?,?)";
pstmt=cntn.prepareStatement(sql);
pstmt.setInt(1,id);
pstmt.setInt(2,categoryId);
pstmt.setString(3,title);
pstmt.setString(4,summary);
pstmt.setString(5, content);
pstmt.setString(6, author);
pstmt.setTimestamp(7, new Timestamp(createDate.getTime()) );
int i=pstmt.executeUpdate();
if(i>0){
System.out.println("添加成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cntn!=null){
try {
cntn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//删除新闻信息
public void deleteNews(int id){
getConnection();
String sql="delete from news_detail where id=?";
try {
pstmt=cntn.prepareStatement(sql);
pstmt.setInt(1, id);
int i=pstmt.executeUpdate();
if(i>0){
System.out.println("删除成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cntn!=null){
try {
cntn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//修改新闻信息
public void updateNews(String title,int id){
getConnection();
String sql="UPDATE news_detail SET title=? WHERE id=?";
try {
pstmt=cntn.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setInt(2, id);
int i=pstmt.executeUpdate();
if(i>0){
System.out.println("修改成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cntn!=null){
try {
cntn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//main方法检测
public static void main(String[] args) {
NewsDao newsDao=new NewsDao();
newsDao.queryAll();
//newsDao.queryTitle("新标题");
//newsDao.deleteNews(3);
//newsDao.addNews(1, 1, "yewan", "abc", "cdd", "adc", new Date());
//newsDao.updateNews("xinxin", 1);
}
}
package com.kgc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;
public class NewsDao {
Connection cntn=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement pstmt=null;
// 公共代码:连接数据库
public void getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/kgcnews";
cntn=DriverManager.getConnection(url, "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//查询全部新闻 可以使用 Statement 或者 PreparedStatement
public void queryAll(){
try {
//加载mysql驱动
Class.forName("com.mysql.jdbc.Driver");
//获得连接Connection对象
String url="jdbc:mysql://localhost:3306/kgcnews";
cntn=DriverManager.getConnection(url,"root","root");
//创建 statement对象 或者 preparedStatement 对象
stmt=cntn.createStatement();
//调用Statement对象的 执行方法 executeQuery
// where title=' 开课啦'or '1'='1 ' 为真
String sql="SELECT id , categoryId,title,summary,content,author,createDate FROM news_detail";
rs=stmt.executeQuery(sql);
//控制台 输出 查询数据
while(rs.next()){
int id=rs.getInt("id");
int categoryId=rs.getInt("categoryId");
String title=(String)rs.getObject("title");
String summary=rs.getString("summary");
String content=rs.getString("content");
String author=rs.getString("author");
Date createDate=rs.getDate("createDate");
System.out.println(id+"\t"+categoryId+"\t"+title+"\t"+summary+"\t"+content+"\t"+author+"\t"+new Timestamp(createDate.getTime()));
}
} 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(cntn!=null){
try {
cntn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//查询部分新闻 使用 PreparedStatement ,更安全 不要使用 Statement
public void queryTitle(String title){
try {
//加载mysql 数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获得Connection对象
String url="jdbc:mysql://localhost:3306/kgcnews";
cntn=DriverManager.getConnection(url,"root","root");
//获得PreparedStatement对象
String sql="select id, title from news_detail where title=?";
pstmt=cntn.prepareStatement(sql);
pstmt.setString(1, title);
rs=pstmt.executeQuery();
while(rs.next()){
int id=rs.getInt("id");
String title1=rs.getString("title");
System.out.println(id+"\t"+title1);
}
} 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(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cntn!=null){
try {
cntn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//增加新闻信息
public void addNews(int id, int categoryId, String title,
String summary, String content,String author,Date createDate){
try {
getConnection();
String sql="insert into news_detail(id,categoryId,title,summary,content,author,createDate) "
+ "values(?,?,?,?,?,?,?)";
pstmt=cntn.prepareStatement(sql);
pstmt.setInt(1,id);
pstmt.setInt(2,categoryId);
pstmt.setString(3,title);
pstmt.setString(4,summary);
pstmt.setString(5, content);
pstmt.setString(6, author);
pstmt.setTimestamp(7, new Timestamp(createDate.getTime()) );
int i=pstmt.executeUpdate();
if(i>0){
System.out.println("添加成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cntn!=null){
try {
cntn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//删除新闻信息
public void deleteNews(int id){
getConnection();
String sql="delete from news_detail where id=?";
try {
pstmt=cntn.prepareStatement(sql);
pstmt.setInt(1, id);
int i=pstmt.executeUpdate();
if(i>0){
System.out.println("删除成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cntn!=null){
try {
cntn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//修改新闻信息
public void updateNews(String title,int id){
getConnection();
String sql="UPDATE news_detail SET title=? WHERE id=?";
try {
pstmt=cntn.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setInt(2, id);
int i=pstmt.executeUpdate();
if(i>0){
System.out.println("修改成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cntn!=null){
try {
cntn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//main方法检测
public static void main(String[] args) {
NewsDao newsDao=new NewsDao();
newsDao.queryAll();
//newsDao.queryTitle("新标题");
//newsDao.deleteNews(3);
//newsDao.addNews(1, 1, "yewan", "abc", "cdd", "adc", new Date());
//newsDao.updateNews("xinxin", 1);
}
}
本文介绍了一个使用Java实现的新闻管理系统,该系统包括了增删改查等基本操作,并详细展示了如何通过Statement和PreparedStatement来执行SQL语句,实现了新闻的查询、添加、删除及更新功能。
522

被折叠的 条评论
为什么被折叠?



