1:JDBC将调用传递给ODBC,ODBC再调用本地的数据库驱动代码:
2:JDBC将调用传递给厂商的API然后再调用
3:JDBC将调用传递给厂商的中间服务器然后再调用
4:JDBC直接连接DATABASE
1:JDBC将调用传递给ODBC,ODBC再调用本地的数据库驱动代码:
2:JDBC将调用传递给厂商的API然后再调用
3:JDBC将调用传递给厂商的中间服务器然后再调用
4:JDBC直接连接DATABASE
JDBC同类产品(orm映射工具)
mybatis
hibernate
JDBC各种连接方式的对比
1.JDBC+ODBC桥的方式
特点:需要数据库的ODBC驱动,仅适用于微软的系统
2.JDBC+厂商API的形式
厂商API一般使用C编写
3.JDBC+厂商Database Connection Server+DataBase的形式
特点:在JAVA和DATABASE之间架起了一台专门用于数据库连接的服务器()一般由数据库厂商提供
4.JDBC+DATABASE的连接方式
特点:使得Application与数据库分开 开发者只需关心内部逻辑的实现而不需注重数据库连接的具体实现(效率高)
JDBC+DATABASE
效率高
JDBC基本概念
JDBC各种连接方式的对比: 1、JDBC + ODBC桥的方式。特点:需要数据库的ODBC驱动,仅适用于微软的系统 这种方式,JDBC将调用传递给ODBC,然后ODBC再调用本地的数据库驱动代码。 2、JDBC + 厂商API的形式。特点:厂商API一般使用C编写 这种方式,JDBC将调用直接传递给厂商API的服务,然后在调用本地的数据库驱动。 3、JDBC + 厂商Database Connection Server + DataBase的形式。 特点:在JAVA与DATABASE之间架起了一台专门用于数据库连接的服务器(一般有数据库厂商提供) 这种方式,JDBC将调用传递给中间服务器,中间服务器再将调用转换成数据库能够被调用的形式,在调用数据库服务器。中间增设数据库服务器能够提升效率,但不如直接操作数据库便捷。 4、JDBC + DATABASE的连接方式。 特点:这使得Application与数据库分开,开发者只需关心内部逻辑的实现而不需注重数据库连接的具体实现。(没有中间环节,是推荐方式!)
package com.imooc.view;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import com.imooc.action.GoddessAction;
import com.imooc.model.Goddess;
public class View {
private static final String CONTEXT = "欢迎来到女神世界:\n" + "下面是女神世界的功能列表:\n"
+ "[MAIN/M]:主菜单\n" + "[QUERY/Q]:查看全部女神信息\n" + "[GET/G]:查看某个女神信息\n"
+ "[ADD/A]:添加女神信息\n" + "[UPDATE/U]:更新女神信息\n"
+ "[DELETE/D]:删除女神信息\n" + "[SEARCH/S]:查询女神信息(根据姓名,手机号查询)\n"
+ "[EXIT/E]:退出女神世界\n" + "[BREAK/B]:返回主菜单";
private static final String OPERATION_MAIN = "MAIN";
private static final String OPERATION_QUERY = "QUERY";
private static final String OPERATION_GET = "GET";
private static final String OPERATION_ADD = "ADD";
private static final String OPERATION_UPDATE = "UPDATE";
private static final String OPERATION_DELETE = "DELETE";
private static final String OPERATION_SEARCH = "SEARCH";
private static final String OPERATION_EXIT = "EXIT";
private static final String OPERATION_BREAK = "BREAK";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Goddess goddess = new Goddess();
GoddessAction action = new GoddessAction();
String prenious = null;
Integer step = 1;
while (scanner.hasNext()) {
String in = scanner.next();
if (OPERATION_EXIT.equals(in.toUpperCase())
|| OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase())) {
System.out.println("您已成功退出女神世界!");
break;
} else if (OPERATION_MAIN.equals(in.toUpperCase())
|| OPERATION_MAIN.substring(0, 1).equals(in.toUpperCase())) {
prenious = null;
step = 1;
System.out.println(CONTEXT);
} else if (OPERATION_BREAK.equals(in.toUpperCase())
|| OPERATION_BREAK.substring(0, 1).equals(in.toUpperCase())) {
prenious = null;
step = 1;
System.out.println("退出当前功能,返回主菜单");
System.out.println(CONTEXT);
} else if (OPERATION_QUERY.equals(in.toUpperCase())
|| OPERATION_QUERY.substring(0, 1).equals(in.toUpperCase())) {
try {
List<Goddess> list = action.query();
for (Goddess god : list) {
System.out.println(god.getId() + " 姓名:"
+ god.getUser_name());
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (OPERATION_GET.equals(in.toUpperCase())
|| OPERATION_GET.substring(0, 1).equals(in.toUpperCase())) {
System.out.println("请输入您要查询的女神ID:");
Integer id = scanner.nextInt();
try {
Goddess go = action.get(id);
System.out.println(go.toString());
} catch (SQLException e) {
e.printStackTrace();
}
}
if (OPERATION_GET.equals(prenious)) {
step++;
} else if (OPERATION_UPDATE.equals(in.toUpperCase())
|| OPERATION_UPDATE.substring(0, 1)
.equals(in.toUpperCase())
|| OPERATION_UPDATE.equals(prenious)) {
prenious = OPERATION_UPDATE;
if (1 == step) {
System.out.println("请输入的要更新的女神 ID:");
} else if (2 == step) {
goddess.setId(Integer.valueOf(in));
System.out.println("请输入新的 【姓名】");
} else if (3 == step) {
goddess.setUser_name(in);
System.out.println("请输入新的 【年龄】");
} else if (4 == step) {
goddess.setAge(Integer.valueOf(in));
System.out.println("请输入新的【生日】 ;格式:yyyy-MM-dd");
} else if (5 == step) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date birthday = null;
try {
birthday = sf.parse(in);
goddess.setBirthday(birthday);
System.out.println("请输入新的【邮箱】");
} catch (ParseException e) {
e.printStackTrace();
System.out.println("您输入的格式有误,请重新输入!");
step = 3;
}
} else if (6 == step) {
goddess.setEmail(in);
System.out.println("请输入新的【手机号】");
} else if (7 == step) {
goddess.setMobile(in);
try {
action.edit(goddess);
System.out.println("更新女神成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("更新女神失败!");
}
}
if (OPERATION_UPDATE.equals(prenious)) {
step++;
}
} else if (OPERATION_SEARCH.equals(in.toUpperCase())
|| OPERATION_SEARCH.substring(0, 1)
.equals(in.toUpperCase())) {
List<Map<String, Object>> params = new ArrayList<Map<String, Object>>();
Map<String, Object> param = new HashMap<String, Object>();
System.out.println("请输入您要查询的女神姓名:");
String name = scanner.next();
param.put("name", "user_name");
param.put("rela", "=");
param.put("value", name);
params.add(param);
System.out.println("请输入您要查询的女神手机号:");
String mobile = scanner.next();
param = new HashMap<String, Object>();
param.put("name", "mobile");
param.put("rela", "=");
param.put("value", mobile);
params.add(param);
List<Goddess> list = null;
try {
list = action.query(params);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (OPERATION_SEARCH.equals(prenious)) {
step++;
} else if (OPERATION_ADD.equals(in.toUpperCase())
|| OPERATION_ADD.substring(0, 1).equals(in.toUpperCase())
|| OPERATION_ADD.equals(prenious)) {
prenious = OPERATION_ADD;
if (1 == step) {
System.out.println("请输入女神的 【姓名】");
} else if (2 == step) {
goddess.setUser_name(in);
System.out.println("请输入女神的 【年龄】");
} else if (3 == step) {
goddess.setAge(Integer.valueOf(in));
System.out.println("请输入女神的【生日】 ;格式:yyyy-MM-dd");
} else if (4 == step) {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Date birthday = null;
try {
birthday = sf.parse(in);
goddess.setBirthday(birthday);
System.out.println("请输入女神的【邮箱】");
} catch (ParseException e) {
e.printStackTrace();
System.out.println("您输入的格式有误,请重新输入!");
step = 3;
}
} else if (5 == step) {
goddess.setEmail(in);
System.out.println("请输入女神的【手机号】");
} else if (6 == step) {
goddess.setMobile(in);
try {
action.add(goddess);
System.out.println("添加女神成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("添加女神失败!");
}
}
if (OPERATION_ADD.equals(prenious)) {
step++;
}
} else if (OPERATION_DELETE.equals(in.toUpperCase())
|| OPERATION_DELETE.substring(0, 1)
.equals(in.toUpperCase())) {
System.out.println("请输入您要删除的女神ID:");
Integer id = scanner.nextInt();
try {
action.del(id);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
mybatis 和 hibernate 是orm映射工具
jdbc的基本概念
常用的代替JDBC和数据库打交道的工具,mybatis 、hibernate
==推荐的连接方式==JDBC+Database
使得Application与数据库分开,
开发者只需关心内部逻辑实现,
而不需关注数据库连接的具体实现.
JDBC基本概念
连接数据库的桥梁
由java语言编写的类和接口
可以为多种数据库提供统一的访问 !
JDBC基本概念
连接数据库的桥梁
由java语言编写的类和接口
可以为多种数据库提供统一的访问 !
常用的代替JDBC和数据库打交道的工具,mybatis 、hibernate
JDBC的基本概念
JDBC之“对岸的女孩走过来”
JDBC各种连接方式的对比
JDBC各种连接方式对比
视图层调用控制层,控制层调用模型层