如何运用JDBC 技术实现 数据库中数据的 增删改查(上)?

本文介绍了一个使用Java实现的新闻管理系统,该系统包括了增删改查等基本操作,并详细展示了如何通过Statement和PreparedStatement来执行SQL语句,实现了新闻的查询、添加、删除及更新功能。

 

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);
        
        
    
        
        
    }
}
 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟空非空也

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值