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. Swift3.0P1 语法指南——控制流

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  2. HTTP API开发

    近期手上的安卓app需要用到自动更新的功能,想着怎么实现,看了下很多应用商店的sdk,觉得不太好,可能会收集用户隐私,于是想着自己实现一个http api后面自己可以实现自动更新,甚至广告推送的功能, ...

  3. [BZOJ3173][Tjoi2013]最长上升子序列

    [BZOJ3173][Tjoi2013]最长上升子序列 试题描述 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上 ...

  4. DPM算法源程序voc-release5在Windows中的配置修改过程

    最近的<视频处理与分析>课程中有一个大作业,是有关DPM物体检测算法的.网上有DPM的源代码,但是原版只能在Linux或Mac上运行,而我的电脑是Windows系统,于是在网上搜了一下在怎 ...

  5. JSON.stringify()和JSON.parse()

    parse用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age":&qu ...

  6. Longest Absolute File Path

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  7. 技术架构:IBatisNet

    --连接数据库框架 1        providers.config 提供配制 常用的数据库连接程序 的xml文件 2        SqlMap.xml  SQL语句执行结果和实体对象之间的映射文 ...

  8. python logging

    参考: https://docs.python.org/2/howto/logging.html#logging-basic-tutorial https://docs.python.org/2/li ...

  9. VC++ 产生GUID或UUID

    GUID 和 UUID 是一样的,表示全球唯一标识码. 下面是Windows系统中,产生GUID的一种方法(Windows API) char* GUID_Generator() { ] = {}; ...

  10. LINK1123:failure during conversion to COFF:file invalid or corrupt

    参考 http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=20726500&id=4528320 试了微软官方说明:http ...