JDBC操作操作数据库

Posted 纸灰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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+"	"+categoryId+"	"+title+"	"+summary+"	"+content+"	"+author+"	"+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();														//显示全部信息

	}
}

  

以上是关于JDBC操作操作数据库的主要内容,如果未能解决你的问题,请参考以下文章

部分代码片段

JDBC操作

java jdbc操作数据库通用代码

VSCode自定义代码片段——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程