JDBCTools.java

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCTools {
 
   /*
    * 执行SQL的方法
    *   insert,update,delete
    * */
   public static void update(String sql){
  
      Connection conn=null;
      Statement st=null;
  
      try {
         /*
          * 1、获取Connection连接
          * 2、获取Statement
          * 3、SQL语句
          * 4、关闭数据库连接
          *
          * */
         conn=getConnection();
         st=conn.createStatement();
         st.executeUpdate(sql);
   
   
      } catch (Exception e) {
         e.printStackTrace();
      }finally{
         release(st, conn);
      }
   }

  public static Connection getConnection() throws Exception {

      String driverClass = null;
      String jdbcUrl = null;
      String user = null;
      String password = null;

     // 读取类路径下的jdbc.properties文件
      InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
      Properties properties = new Properties();
      properties.load(in);

    driverClass = properties.getProperty("driver");
      jdbcUrl = properties.getProperty("jdbcUrl");
      user = properties.getProperty("user");
      password = properties.getProperty("password");
      // 加载数据库驱动程序
      Class.forName(driverClass);
      // 通过DriverManager的getConnection()方法获取数据库连接
      Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
      return connection;

  }
 
   public static void release(Statement st, Connection conn) {

     if (st != null) {
         try {
            st.close();
         } catch (SQLException e) {
            e.printStackTrace();
         }
     }

    if (conn != null) {
         try {
            conn.close();
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
   }
 
   public static void release(ResultSet rs,Statement st,Connection conn){
  
      if (rs!=null) {
         try {
            rs.close();
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
  
      if (st!=null) {
         try {
            st.close();
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
  
      if (conn!=null) {
         try {
            conn.close();
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
   }
}

测试类:TestJDBC.java

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

import org.junit.Test;

public class TestJDBC {

@Test
 public void testGetStudent(){
  /*
   * 1、得到查询的类型
   * 2、具体查询学生信息
   * 3、打印显示信息
   * */
  int searchType=getSearchTypeFromConsole();
  Student student=searchStudent(searchType);
  
  printStudent(student);
 }
 
 private void printStudent(Student student) {
  
  if (student!=null) {
   System.out.println(student);
  }else{
   System.out.println("查无此人!");
  }
 }

/*
  * 具体查询显示信息,返回一个Student对象,若不存在返回null
  * @param searchType:1 或2
  * @return
  * */
 private Student searchStudent(int searchType) {
  /*
   * search=1: 提示输入身份证号
   * search=2: 准考证号
   * */
  String sql="select flowid,type,idcard,examcard,studentname from "
    + "examstudent where ";
  
  Scanner sc=new Scanner(System.in);
  if (searchType==1) {
   System.out.println("请输入身份证号:");
   String examCard=sc.next();
   sql=sql+"idcard='"+examCard+"'";
   
  }else{
   System.out.println("请输入准考证号:");
   String examCard=sc.next();
   sql=sql+"examCard='"+examCard+"'";
  }
  //执行查询
  Student student=getStudent(sql);
  
  return student;
 }

private Student getStudent(String sql) {
  
  Student stu=null;
  Connection conn=null;
  Statement st=null;
  ResultSet rs=null;
  try {
   conn=JDBCTools.getConnection();
   st=conn.createStatement();
   rs=st.executeQuery(sql);
   
   if (rs.next()) {
    stu=new Student();
    stu.setFlowID(rs.getInt(1));
    stu.setType(rs.getInt(2));
    stu.setIdCard(rs.getString(3));
    stu.setExamCard(rs.getString(4));
    stu.setStudentName(rs.getString(5));
   }
   
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   JDBCTools.release(rs, st, conn);
  }
  
  return stu;
 }

private int getSearchTypeFromConsole() {
  /*
   * 1:用户身份证查询
   * 2:用户准考证号查询
   * */
  System.out.println("请输入查询类型:1.用身份证查询;2.用准考号查询");
  Scanner sc=new Scanner(System.in);
  int type=sc.nextInt();
  if (type!=1&&type!=2) {
   System.out.println("输入有误,请重新输入!");
   throw new RuntimeException();
  }
  return type;
 }

public void addNewStudent(Student student){
  /*
   * 1、准备sql语句
   * 2、调用JDBCTools类的update(sql)方法执行插入操作
   * 问题:
   *   String类型的值
   * */
  String sql="insert into examstudent values ("
    + ""+student.getFlowID()+","
    +student.getType()+",'"
    +student.getIdCard()+"','"
    +student.getExamCard()+"','"
    +student.getStudentName()
    +"')";
 
  JDBCTools.update(sql);
 }
 
 @Test
 public void testAddNewStudent(){
  
  Student student=getStudentFromConsole();
  addNewStudent(student);
 }

/*
  * 从控制台输入学生的信息
  * */
 private Student getStudentFromConsole() {
  
  Scanner sc=new Scanner(System.in);
  Student student=new Student();
  System.out.print("FlowId:");
  student.setFlowID(sc.nextInt());
  System.out.print("Type:");
  student.setType(sc.nextInt());
  System.out.print("IdCard:");
  student.setIdCard(sc.next());
  System.out.print("ExamCard:");
  student.setExamCard(sc.next());
  System.out.print("StudentName:");
  student.setStudentName(sc.next());
  
  return student;
 }
}

<三>JDBC_面向对象思想的体现的更多相关文章

  1. 第一节: Timer的定时任务的复习、Quartz.Net的入门使用、Aop思想的体现

    一. 前奏-Timer类实现定时任务 在没有引入第三方开源的定时调度框架之前,我们处理一些简单的定时任务同时都是使用Timer类, DotNet中的Timer类有三个,分别位于不同的命名空间下,分别是 ...

  2. 【重走Android之路】【Java面向对象基础(三)】面向对象思想

    [重走Android之路][基础篇(三)][Java面向对象基础]面向对象思想   1 面向对象的WWH   1.1 What--什么是面向对象         首先,要理解“对象”.在Thinkin ...

  3. linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现【转】

    本文转自自:http://blog.chinaunix.net/uid-25014876-id-59418.html linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现 一. ...

  4. JAVA之旅(三)——数组,堆栈内存结构,静态初始化,遍历,最值,选择/冒泡排序,二维数组,面向对象思想

    JAVA之旅(三)--数组,堆栈内存结构,静态初始化,遍历,最值,选择/冒泡排序,二维数组,面向对象思想 我们继续JAVA之旅 一.数组 1.概念 数组就是同一种类型数据的集合,就是一个容器 数组的好 ...

  5. linux设备驱动归纳总结(三):3面向对象思想和lseek、container_of、write、read 【转】

    linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现 转自:http://blog.chinaunix.net/uid-25014876-id-59418.html 一.结构体 ...

  6. 【Linux开发】linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现

    linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现 一.结构体struct file和struct inode 在之前写的函数,全部是定义了一些零散的全局变量.有没有办法整合 ...

  7. 进击的Python【第六章】:Python的高级应用(三)面向对象编程

    Python的高级应用(三)面向对象编程 本章学习要点: 面向对象编程介绍 面向对象与面向过程编程的区别 为什么要用面向对象编程思想 面向对象的相关概念 一.面向对象编程介绍 面向对象程序设计(英语: ...

  8. 20145208 实验三 Java面向对象程序设计

    20145208 实验三 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...

  9. Java基础知识二次学习--第三章 面向对象

    第三章 面向对象   时间:2017年4月24日17:51:37~2017年4月25日13:52:34 章节:03章_01节 03章_02节 视频长度:30:11 + 21:44 内容:面向对象设计思 ...

随机推荐

  1. Sql Server FOR XML PATH

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  2. Activiti学习(二)数据表结构

    Activiti工作流引擎数据库表结构 数据库表的命名 Acitiviti数据库中表的命名都是以ACT_开头的.第二部分是一个两个字符用例表的标识.此用例大体与服务API是匹配的. l        ...

  3. PHP函数call_user_func和call_user_func_array详解

    今天在群里面,有个叫lewis的在问call_user_func_array的用法,因为之前一直没有用过,也不能说什么,于是看一下手册,发现是这么写的: call_user_func_array (P ...

  4. Node.js入门学习笔记(一)

    先来个最常见的"Hello World!". 打开你最喜欢的编辑器(我用的是Sublime Text),创建一个helloWorld.js的文件.我们要做的就是向stdout输出& ...

  5. 学习 opencv---(1) opencv3.1.0 组件结构浅析

    本系列是根据 浅墨大神 的opencv系列而写的,,应该大部分内容会一样..如有侵权还请告知........... 开发环境:win7 + VS2013 + opencv3.1.0 至于OpenCV组 ...

  6. 为 MySQL 设置默认字符集(UTF-8)避免产生乱码

    环境:Windows 7+Wamp Server+MySQL 5.7.9 查看MySQL默认编码: SHOW VARIABLES LIKE 'character%' character_set_cli ...

  7. RobotFrameWork(二)Ride简单使用及快捷键

    一.简单示例 注意:以下操作使用到快捷键的,请先确保没有与其他软件的快捷键设置冲突,比如sogou拼音.有道词典等等 1.启动ride 启动ride方法: 1)  通过界面图标 2)  dos命令行: ...

  8. winrt简单克隆对象

    public MapPoint Copy()//MapPoint克隆方法 { MapPoint p = new MapPoint();//这是我自定义的对象 //利用反射获得类成员 FieldInfo ...

  9. (转)EClipse插件推荐

    http://www.importnew.com/4707.html 来自非营利性Eclipse基金会的Eclipse IDE以其插件生态系统著称.Eclipse市场拥有海量插件可供下载,你可以通过插 ...

  10. AngularJS多模块开发

    angularJS中的多模块开发是指多个module模块开发,步骤为: 1. 确定主模块    var app=angular.module('myApp',[]); 2. 其他的子模块添加到主模块后 ...