JDBC操作操作数据库

JDBC操作数据库

package cn.kgc.dao;
/**
 * JDBC操作数据库 
 * 	新闻数据库
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import cn.config.ConfigManager;
public class News {
	//JDBC的初始对象
	Connection connection = null;
	PreparedStatement stmt = null;
	ResultSet rs = null;

	/**
	 * 获取数据连接
	 */
	public void getConnection() {
		try {
			/**
			 * 读取配置文件的属性
			 */
			String className = ConfigManager.getInstanc().getString("ClassName");
			String url = ConfigManager.getInstanc().getString("Url");
			String user = ConfigManager.getInstanc().getString("User");
			String password = ConfigManager.getInstanc().getString("PassWord");
			
			Class.forName(className);
			connection = DriverManager.getConnection(url,user,password);	//数据库地址,用户名,密码
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 	增加新闻信息
	 */
	public void addNews(int id,long categoryId,String title,String summary,String content,String author,Date createDate) {
		try {
			this.getConnection();
			String sql = "insert into `news_detail`(`id`,`categoryId`,`title`,`summary`,`content`,`author`,`createDate`) values (?,?,?,?,?,?,?)";
			stmt = connection.prepareStatement(sql);
			stmt.setInt(1, id);
			stmt.setLong(2, categoryId);
			stmt.setString(3, title);
			stmt.setString(4, summary);
			stmt.setString(5, content);
			stmt.setString(6, author);
			stmt.setTimestamp(7, new Timestamp(createDate.getTime()));
			int r=stmt.executeUpdate();
			if(r>0) {
				System.out.println("增加成功");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				stmt.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 	删除指定新闻信息
	 */
	public void deletenews(int id) {
		try {
			this.getConnection();
			String sql = "delete from `news_detail` where id=?";
			stmt = connection.prepareStatement(sql);
			stmt.setInt(1, id);
			int r = stmt.executeUpdate();
			if(r>0) {
				System.out.println("删除成功");
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				stmt.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 	修改特定新闻标题的方法
	 */
	public void updateNews(int id,String title) {
		try {
			this.getConnection();
			String sql="update `news_detail` set title=? where id=?";
			stmt=connection.prepareStatement(sql);
			stmt.setString(1, title);
			stmt.setInt(2, id);
			int r = stmt.executeUpdate();
			if(r>0) {
				System.out.println("更改成功");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				stmt.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 	查询全部新闻信息
	 */
	public void showAll() {

		this.getConnection();
		try {
			String sql="select * from `news_detail`";
			stmt=connection.prepareStatement(sql);
			rs=stmt.executeQuery();
			while(rs.next()){
				int id = rs.getInt("id");
				long categoryId = rs.getLong("categoryId");
				String title = rs.getString("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"+createDate);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				stmt.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

	}

	/**
	 * 	测试类
	 */
	public static void main(String[] args) {
		News n = new News();
		//		n.addNews(4, 1, "换家德","好多的水","游戏侠","lscs",new Date());	//增加
		//		n.deletenews(4);													//删除
		//		n.updateNews(4,"啊大大");											//更改
				n.showAll();														//显示全部信息

	}
}

  

猜你喜欢

转载自www.cnblogs.com/zzh630/p/10689286.html