<三>JDBC_面向对象思想的体现
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_面向对象思想的体现的更多相关文章
- 第一节: Timer的定时任务的复习、Quartz.Net的入门使用、Aop思想的体现
一. 前奏-Timer类实现定时任务 在没有引入第三方开源的定时调度框架之前,我们处理一些简单的定时任务同时都是使用Timer类, DotNet中的Timer类有三个,分别位于不同的命名空间下,分别是 ...
- 【重走Android之路】【Java面向对象基础(三)】面向对象思想
[重走Android之路][基础篇(三)][Java面向对象基础]面向对象思想 1 面向对象的WWH 1.1 What--什么是面向对象 首先,要理解“对象”.在Thinkin ...
- linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现【转】
本文转自自:http://blog.chinaunix.net/uid-25014876-id-59418.html linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现 一. ...
- JAVA之旅(三)——数组,堆栈内存结构,静态初始化,遍历,最值,选择/冒泡排序,二维数组,面向对象思想
JAVA之旅(三)--数组,堆栈内存结构,静态初始化,遍历,最值,选择/冒泡排序,二维数组,面向对象思想 我们继续JAVA之旅 一.数组 1.概念 数组就是同一种类型数据的集合,就是一个容器 数组的好 ...
- linux设备驱动归纳总结(三):3面向对象思想和lseek、container_of、write、read 【转】
linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现 转自:http://blog.chinaunix.net/uid-25014876-id-59418.html 一.结构体 ...
- 【Linux开发】linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现
linux设备驱动归纳总结(三):3.设备驱动面向对象思想和lseek的实现 一.结构体struct file和struct inode 在之前写的函数,全部是定义了一些零散的全局变量.有没有办法整合 ...
- 进击的Python【第六章】:Python的高级应用(三)面向对象编程
Python的高级应用(三)面向对象编程 本章学习要点: 面向对象编程介绍 面向对象与面向过程编程的区别 为什么要用面向对象编程思想 面向对象的相关概念 一.面向对象编程介绍 面向对象程序设计(英语: ...
- 20145208 实验三 Java面向对象程序设计
20145208 实验三 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...
- Java基础知识二次学习--第三章 面向对象
第三章 面向对象 时间:2017年4月24日17:51:37~2017年4月25日13:52:34 章节:03章_01节 03章_02节 视频长度:30:11 + 21:44 内容:面向对象设计思 ...
随机推荐
- Tomcat端口被占用错误
所报错误: 严重: Error initializing endpointjava.lang.Exception: Socket bind failed: [730013] ????????????? ...
- #define #include #undef的其中一个用法(目的)
一.背景 最近在跟一段系统级的代码,和原来单纯的下位机代码相比,真的是刘姥姥进大观园--看花了眼.相较于 之前所常见的各种下位机代码,系统级代码常常会出现深层次结构体嵌套,结构体内的各种回调函数导致对 ...
- 如何让自己的app尽量不被系统杀死
1. 在Service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建 @Override public int onStartComman ...
- r-cnn学习(八):minibatch
这段代码包括由输入图片随机生成相应的RoIs,并生成相应的blobs,由roidb得到相应的 minibatch.其代码如下. # ---------------------------------- ...
- SQLServer 事务隔离级别与锁的申请和释放
脏读:当一个事务开始更新数据,但是这个事务并没有完全提交,这个时候第二个事务开始读取数据,把第一个事务所更改的数据读了出来, 第二个事务读取的数据时临时的,因为有可能第一个事务最终有可能做回滚操作 不 ...
- asp.net中membership使用oracle数据库(一)
第一步 数据库的准备 使用 oracle 11g的数据库 需要安装好,安装过程中先决条件检查失败的处理:确认server服务已运行 cmd->net share c$=c: 就可以通过 orac ...
- 【转】android中ListView的定位:使用setSelectionFromTop实现ListView的position的保持
如果一个ListView太长,有时我们希望ListView在从其他界面返回的时候能够恢复上次查看的位置,这就涉及到ListView的定位问题: 解决的办法如下: 1 2 3 4 5 6 7 // 保存 ...
- 设置UITableView的separatorInset值为UIEdgeInsetsZero,分隔线不最左端显示的问题
一.问题描述 UITableView分割线要显示到最左端 查看UITableView的属性,发现设置separatorInset的值可以自定义分割线的位置. @property (nonatomic) ...
- iOS8系统H264视频硬件编解码说明
公司项目原因,接触了一下视频流H264的编解码知识,之前项目使用的是FFMpeg多媒体库,利用CPU做视频的编码和解码,俗称为软编软解.该方法比较通用,但是占用CPU资源,编解码效率不高.一般系统都会 ...
- SQL查询第m条到第n条的方法
SQL查询第m条到第n条的方法 如表名为GOOD Sselect top (n-m) * from GOODS where (某一列名) not in (select top m (某一列名) fro ...