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); } }
如何运用JDBC 技术实现 数据库中数据的 增删改查(上)?
猜你喜欢
转载自blog.csdn.net/java_stud/article/details/80474177
今日推荐
周排行