JDBC的学习(三)——以查询新闻系统为例

数据库截图:

效果图:

 

主要代码:

package com.yn.service;

import java.sql.*;
import java.util.*;

import com.yn.dao.SqlHelper;
import com.yn.domain.News;
/**
 * 
 * @author nickY9527
 *
 */

public class NewsService {

    //编写一个搜索新闻的函数
    public ArrayList<News> searchNews(String keys) {
        
        ArrayList<News> al = null;
        String sql = "select * from news where newsTitle = '"+keys+"'";
        
        try {
            //完成查询
            ResultSet rs = SqlHelper.executeQuery(sql, null);
            //将rs -> ArrayList[News对象]  业务逻辑二次封装
            al = new ArrayList<News>();
            while(rs.next()) {
                News news = new News();
                news.setNewsId(rs.getInt(1));
                news.setNewsTitle(rs.getString(2));
                news.setNewsDate(rs.getDate(3));
                news.setNewsContent(rs.getString(4));
                
                al.add(news);
            }        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            //关闭资源
            SqlHelper.close(SqlHelper.getRs(), SqlHelper.getPs(), SqlHelper.getCt());
        }
        return al;
    }

}
package com.yn.view;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.yn.domain.News;
import com.yn.service.NewsService;

public class NewsSystemFrame {

    public static void main(String[] args) throws Exception {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        
        System.out.println("********欢迎登录到系统********");
        System.out.println("****请输入用户名****");
        String userId = br.readLine();
        
        System.out.println("****请输入用户名****");
        String pwd = br.readLine();
        
        if("10000".equals(userId)&&"10000".equals(pwd)) {
            
            while(true) {
            System.out.println("****管理界面****");
            System.out.println("请选择你的操作");
            System.out.println("search \t查询新闻");
            System.out.println("add \t添加新闻");
            System.out.println("exit \t退出系统");    
            
            String operType = br.readLine();
            
            if(operType.equals("search")) {
                
                //接收用户的输入
                System.out.println("请输入关键字");
                String keys = br.readLine();
                //调用service完成任务
                NewsService ns = new NewsService();
                ArrayList<News> al = ns.searchNews(keys);
                for(News news: al) {
                    System.out.println(news.getNewsId()+"\t"
                            +news.getNewsTitle()+"\t"
                            +news.getNewsDate().toString()+"\t"
                            +news.getNewsContent());
                }
            
            }else if(operType.equals("add")) {
                System.out.println("请输入新闻的标题");
                String title = br.readLine();
                System.out.println("请输入新闻的内容");
                String content = br.readLine();
                
            }else if(operType.equals("exit")) {
                 System.exit(0);
            }
            }
        }else {
            
        }
    }
}

 

posted on 2018-07-25 20:09  thelast9527  阅读(148)  评论(0编辑  收藏  举报

导航