JDBC封装学习笔记---面向对象的JDBC

Posted 爱生活的Mr.Yao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC封装学习笔记---面向对象的JDBC相关的知识,希望对你有一定的参考价值。

上一篇是针对连接数据库以及释放数据库资源进行的基本封装练习,这一次就是实战操作了,实际的操作一张表,数据表的信息如下图所示

一 、添加、更新、删除其实都是用的executeUpdate()方法,所以除了SQL语句不同,其他的基本一样,这里写的是一个添加操作:

 1 // 添加一个运动员,传入一个运动员对象
 2 public void addPlayers(Players player) {
 3 // 给出一个SQL
 4 String sql = "insert into userinfo" + " values (" + player.getId() + ",\'" + player.getPlayerID() + "\',\'"
 5 + player.getName() + "\',\'" + player.getTeam() + "\',\'" + player.getCity() + "\'," + player.getMaxScore()
 6 + ",\'" + player.getDraftyear() + "\')";
 7 System.out.println(sql);
 8 // 调用更新方法
 9 JDBCUtils.updateData(sql);
10 }

 

因为是面向对象,所以添加操作添加一个完整的对象进去,这时就是需要我们创建一Players类:并且需要有对应的get(),set()方法,重写toString()是为了测试方便。另外updateData()方法的实现在上一篇文章中

 1 package com.jdbc.bean;
 2 
 3 public class Players {
 4 private int id; // ID
 5 private String playerID;
 6 private String name; // 名字
 7 private String team; // 球队
 8 private String city; // 城市
 9 private int maxScore; // 个人单场最高分
10 private String draftyear; // 选秀年
11 
12 public int getId() {
13 return id;
14 }
15 
16 public void setId(int id) {
17 this.id = id;
18 }
19 
20 public String getName() {
21 return name;
22 }
23 
24 public void setName(String name) {
25 this.name = name;
26 }
27 
28 public String getTeam() {
29 return team;
30 }
31 
32 public void setTeam(String team) {
33 this.team = team;
34 }
35 
36 public String getCity() {
37 return city;
38 }
39 
40 public void setCity(String city) {
41 this.city = city;
42 }
43 
44 public int getMaxScore() {
45 return maxScore;
46 }
47 
48 public void setMaxScore(int maxScore) {
49 this.maxScore = maxScore;
50 }
51 
52 public String getDraftyear() {
53 return draftyear;
54 }
55 
56 public void setDraftyear(String draftyear) {
57 this.draftyear = draftyear;
58 }
59 
60 public String getPlayerID() {
61 return playerID;
62 }
63 
64 public void setPlayerID(String playerID) {
65 this.playerID = playerID;
66 }
67 
68 public Players(int id, String playerID, String name, String team, String city, int maxScore, String draftyear) {
69 super();
70 this.id = id;
71 this.playerID = playerID;
72 this.name = name;
73 this.team = team;
74 this.city = city;
75 this.maxScore = maxScore;
76 this.draftyear = draftyear;
77 }
78 
79 public Players() {
80 super();
81 // TODO Auto-generated constructor stub
82 }
83 
84 @Override
85 public String toString() {
86 return "Players [id=" + id + ", playerID=" + playerID + ", name=" + name + ", team=" + team + ", city=" + city
87 + ", maxScore=" + maxScore + ", draftyear=" + draftyear + "]";
88 }
89 
90 }

 

我这里测试的时候使用的Junit框架,测试添加方法的方法如下所示:

1 @Test
2 public void testAddPlayers() {
3 Players player = getFromConsole();
4 addPlayers(player);
5 }

 

其中getFromConsole是从控制台获取的用户输入的信息,方法如下:

 1 // 从控制台获取输入的运动员信息
 2 private Players getFromConsole() {
 3 // TODO Auto-generated method stub
 4 Scanner input = new Scanner(System.in);
 5 Players player = new Players();
 6 System.out.println("请输入球员信息");
 7 // 这里因为主键在数据表中设置了自增,所以拼接的SQL中只获取,而不用再一次输入ID
 8 System.out.print("球员ID:");
 9 player.setPlayerID(input.next());
10 System.out.print("球员姓名:");
11 player.setName(input.next());
12 System.out.print("球队名称:");
13 player.setTeam(input.next());
14 System.out.print("球队城市:");
15 player.setCity(input.next());
16 System.out.print("个人单场最高分:");
17 player.setMaxScore(input.nextInt());
18 System.out.println("选秀年:");
19 player.setDraftyear(input.next());
20 
21 return player;
22 }

 

这些写完,就可以去测试那个testAddPlayers() 方法了,Junit框架的使用就不说了,然后运行结果附上:

二:查询操作,这里还是根据用户的输入去执行的查询:

1 @Test
2 public void testGetPlayer() {
3 // 获取查询类型
4 int searchType = getTypeFromConsole();
5 // 执行查询
6 Players players = searchPlayer(searchType);
7 // 打印运动员信息
8 printPlayer(players);
9 }

 

Junit的测试方法一共就调用了几个方法,然后每个方法的具体实现看一下:

  1 getTypeFromConsole():获得查询类型,用户可以根据playID和名字去查询,所以要判断用户在控制台的选择
  2 
  3 // 从控制台读入一个整数,返回1,用球员ID 查询,2,用姓名查询,其他的无效并提示让用户重新输入
  4 private int getTypeFromConsole() {
  5 // TODO Auto-generated method stub
  6 // 1 根据提示让用户输入查询类型,返回1,用球员ID 查询,2,用姓名查询
  7 System.out.println("请输入查询类型:1.使用球员ID查询,2.使用姓名查询");
  8 Scanner input = new Scanner(System.in);
  9 int type = input.nextInt();
 10 // 判断
 11 if (type != 1 && type != 2) {
 12 System.out.println("输入不合法,请重新输入....");
 13 throw new RuntimeException(); // 中断程序
 14 
 15 }
 16 return type;
 17 }
 18 
 19 searchPlayer(searchType):传入获取到的查询类型,然后进行查询
 20 
 21 // 具体查询运动员信息方法
 22 private Players searchPlayer(int searchType) {
 23 // TODO Auto-generated method stub
 24 // select * 的写法不好,这里为了偷懒
 25 String sql = "select * from userinfo" + " where ";
 26 Scanner scanner = new Scanner(System.in);
 27 // 根据确定的type,提示用户输入查询的类型
 28 //// 最终确认SQL语句
 29 if (searchType == 1) {
 30 System.out.print("请输入球员ID编号:");
 31 String playerID = scanner.next();
 32 sql = sql + "playID=\'" + playerID + "\'";
 33 } else {
 34 System.out.print("请输入球员姓名:");
 35 String name = scanner.next();
 36 sql = sql + "username=\'" + name + "\'";
 37 }
 38 // 执行查询
 39 Players player = getPlayer(sql);
 40 
 41 return player;
 42 }
 43 
 44 其中调用了getPlayer(sql)的方法,具体实现如下图所示:
 45 
 46 // 执行查询的方法,根据SQL语句执行查询
 47 private Players getPlayer(String sql) {
 48 // TODO Auto-generated method stub
 49 Players player = null;
 50 Connection conn = null;
 51 Statement statement = null;
 52 ResultSet rs = null;
 53 
 54 try {
 55 conn = JDBCUtils.getConnection();
 56 statement = conn.createStatement();
 57 rs = statement.executeQuery(sql);
 58 if (rs.next()) {
 59 player = new Players(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5),
 60 rs.getInt(6), rs.getString(7));
 61 int id = rs.getInt(1);
 62 String playID = rs.getString(2);
 63 String name = rs.getString(3);
 64 String team = rs.getString(4);
 65 String city = rs.getString(5);
 66 int maxScore = rs.getInt(6);
 67 String draftYear = rs.getString(7);
 68 
 69 System.out.println("ID:" + id);
 70 System.out.println("playID:" + playID);
 71 System.out.println("名字:" + name);
 72 System.out.println("球队:" + team);
 73 System.out.println("城市:" + city);
 74 System.out.println("个人单场最高分:" + maxScore);
 75 System.out.println("选秀年:" + draftYear);
 76 
 77 }
 78 } catch (ClassNotFoundException e) {
 79 // TODO Auto-generated catch block
 80 e.printStackTrace();
 81 } catch (IOException e) {
 82 // TODO Auto-generated catch block
 83 e.printStackTrace();
 84 } catch (SQLException e) {
 85 // TODO Auto-generated catch block
 86 e.printStackTrace();
 87 } finally {
 88 JDBCUtils.closeSource(rs, statement, conn);
 89 }
 90 // System.out.println(sql);
 91 return player;
 92 }
 93 
 94 printPlayer(player):打印运动员信息,如果运动员信息存在,打印出来。不存在就显示查无此人
 95 
 96 // 打印运动员信息
 97 private void printPlayer(Players players) {
 98 // TODO Auto-generated method stub
 99 if (players != null) {
100 System.out.println(players);
101 } else {
102 System.out.println("查无此人....");
103 }
104 }

 

上述这些代码写完,就可以执行testGetPlayer()方法了。

以上是关于JDBC封装学习笔记---面向对象的JDBC的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate 学习笔记

HIbernate学习笔记 之 简介

java JDBC编程学习笔记

java JDBC编程学习笔记

day14_DBUtils学习笔记

hibernate学习笔记之一 hibernate简介