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. [Network] 计算机网络基础知识总结

    计算机网络学习的核心内容就是网络协议的学习.网络协议是为计算机网络中进行数据交换而建立的规则.标准或者说是约定的集合.因为不同用户的数据终端可能采取的字符集是不同的,两者需要进行通信,必须要在一定的标 ...

  2. Mysql存中文值乱码

    一是安装mysql时,其中会有一个步骤选择编码方式,此时选择gbk即可.如果不选择,默认的编码是latin1: 二是在安装玩mysql之后,手动修改其配置文件,如下: (1)修改 MySql安装目录下 ...

  3. 利用Nginx实现域名转发 不修改主机头

    在conf下 新建一个 文件 格式 : 域名.conf  例如:www.test.com.conf 文件里配置: server{ listen 80; server_name www.test.com ...

  4. 修改mysql默认字符编码出现的Job failed to start解决方法

    5.5以后的版本对字符编码方式修改的办法,原来在[mysqld]下的修改已经发生了变化,正确方式如下: [mysqld]下添加的应该为: character-set-server=utf8 colla ...

  5. securecrt设置 (外观,中文不乱码)

    最终效果图 这叫做先入为主,哈哈~~ 详细设置,action!!!! ############### 菜单栏:  选项---会话选项   一.终端---仿真 1.终端选择 linux 2.ANSI颜色 ...

  6. js 中 Math对象

    Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法.这是它与Date,String对象的区别. Math 对象属性 Math 对象方法

  7. 9.JAVA中的正则表达式

    一.JAVA中的正则表达式 1.概念:以某种特定的方式描述字符串 1.Java中正则表达式的规则 ?          #{0,1}-?有一个-或者没有 \\           #表示一个" ...

  8. (一)安卓小app开发之基础环境搭建

    一.准备工作: 1.下载Android Studio开发环境 https://dl.google.com/dl/android/studio/ide-zips/2.1.1.0/android-stud ...

  9. Javascript函数重载,存在呢—还是存在呢?

    1.What's is 函数重载? );//Here is int 10 print("ten");//Here is string ten } 可以发现在C++中会根据参数的类型 ...

  10. Js-知识小总结

    1.给标签价 class属性的时候,记住是 ClassName, 例如:var oImg = document.getElementById('img1'); 要给这个对象价class属性 oimg. ...