第1章 JDBC 简介
1-1 JDBC 简介
- JDBC简介
- 使用详解
- JDBC常用接口
- JDBC编程步骤
- 执行SQL语句
- 管理结果集
- 事务管理
- 高级应用
- 分页
- 高级查询
- 高级函数使用
- 整理总结
JDBC全称为Java Data Base Connectivity(java数据库连接)
可以为多种数据库提供统一的访问
体现Java “编写一次,处处运行”的高大上精神
第2章 JDBC 使用详解
2-1 JDBC 项目效果展示
2-2 JDBC实战—打通数据库
使用详情
1. 工欲善其事必先利其器
- 明确目的:需求(做什么)
- 实现屌丝逆袭
- 拥有女神禁区,享有查看,添加,修改,删除等功能
- 指导思想:概要设计(怎么做)
- 工具准备:MySql(数据库),Eclipse(开发工具)、Navicat(数据库管理工具)
- 埋头苦干:编码
- 实战演练:测试
-大功告成:系统上线
2. JDBC编程步骤
- 加载驱动程序:Class.forName(driverClass)
- 加载MySql驱动:Class.forName(“com.mysql.jdbc.Driver”)
- 加载Oracle驱动:Class.forName(“oracle.jdbc.driver.OracleDriver”)
- 获取数据库连接:
DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/imooc”,”root”,”root”);
- 创建Statement对象:conn.createStatement();
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/imooc";
private static final String USER = "root";
private static final String PASSWORD = "root";
public static void main(String[] args) throws Exception {
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接
Connection conn = DriverManager.getConnection(URL,USER,PASSWORD);
//3.通过数据库连接操作数据库,实现增删改查
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select user_name,age from imooc_goddess");
while(rs.next()) {
System.out.println(rs.getString("user_name") + "," + rs.getInt("age"));
}
}
}
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for imooc_goddess
-- ----------------------------
DROP TABLE IF EXISTS `imooc_goddess`;
CREATE TABLE `imooc_goddess` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(30) NOT NULL,
`sex` int(11) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
`mobile` varchar(11) DEFAULT NULL,
`create_user` varchar(30) DEFAULT NULL,
`create_date` date DEFAULT NULL,
`update_user` varchar(30) DEFAULT NULL,
`update_date` date DEFAULT NULL,
`isdel` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of imooc_goddess
-- ----------------------------
INSERT INTO `imooc_goddess` VALUES ('1', '小溪', '1', '22', '2000-12-12', 'xiaoxi@163.com', '13911111111', 'ADMIN', '2015-01-08', 'ADMIN', '2015-01-08', '0');
2-3 JDBC实战—搭建模型层 Ⅰ
- 详设
- 采用MVC三层结构
- View(视图层)
- Control(控制层)
- Model(模型层)
- DB(数据库)
设计从上往下或从下往上,建议从下往上
public class Goddess {
private Integer id;
private String user_name;
private Integer sex;
private Integer age;
private Date birthday;
private String email;
private String mobile;
private String create_user;
private String update_user;
private Date create_date;
private Date update_date;
private Integer isdel;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getCreate_user() {
return create_user;
}
public void setCreate_user(String create_user) {
this.create_user = create_user;
}
public String getUpdate_user() {
return update_user;
}
public void setUpdate_user(String update_user) {
this.update_user = update_user;
}
public Date getCreate_date() {
return create_date;
}
public void setCreate_date(Date create_date) {
this.create_date = create_date;
}
public Date getUpdate_date() {
return update_date;
}
public void setUpdate_date(Date update_date) {
this.update_date = update_date;
}
public Integer getIsdel() {
return isdel;
}
public void setIsdel(Integer isdel) {
this.isdel = isdel;
}
@Override
public String toString() {
return "Goddess [id=" + id + ", user_name=" + user_name + ", sex="
+ sex + ", age=" + age + ", birthday=" + birthday + ", email="
+ email + ", mobile=" + mobile + ", create_user=" + create_user
+ ", update_user=" + update_user + ", create_date="
+ create_date + ", update_date=" + update_date + ", isdel="
+ isdel + "]";
}
}
public class GoddessDao1 {
public void addGoddess() {
}
public void updateGoddess() {
}
public void delGoddess() {
}
public List<Goddess> query() throws SQLException{
Connection conn = DBUtil1.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select user_name,age from imooc_goddess");
List<Goddess> gs = new ArrayList<Goddess>();
Goddess g = null;
while(rs.next()) {
g = new Goddess();
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
gs.add(g);
}
return gs;
}
public Goddess get() {
return null;
}
}
public class GoddessAction1 {
public static void main(String[] args) throws SQLException {
GoddessDao1 g = new GoddessDao1();
List<Goddess> gs = g.query();
for (Goddess goddess : gs) {
System.out.println(goddess.getUser_name() + "," + goddess.getAge());
}
}
}
public class DBUtil1 {
private static final String URL = "jdbc:mysql://localhost:3306/imooc";
private static final String USER = "root";
private static final String PASSWORD = "root";
private static Connection conn = null;
static {
try {
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接
conn = DriverManager.getConnection(URL,USER,PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
return conn;
}
public static void main(String[] args) throws Exception {
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接
Connection conn = DriverManager.getConnection(URL,USER,PASSWORD);
//3.通过数据库连接操作数据库,实现增删改查
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select user_name,age from imooc_goddess");
while(rs.next()) {
System.out.println(rs.getString("user_name") + "," + rs.getInt("age"));
}
}
}
2-4 JDBC实战—搭建模型层 Ⅱ
public class GoddessAction1 {
public static void main(String[] args) throws Exception {
GoddessDao1 g = new GoddessDao1();
Goddess g1 = new Goddess();
g1.setUser_name("小夏");
g1.setAge(22);
g1.setSex(1);
g1.setBirthday(new Date());
g1.setEmail("xiao@126.com");
g1.setMobile("15312345678");
g1.setCreate_user("ADMIN");
g1.setUpdate_user("ADMIN");
g1.setIsdel(1);
g.addGoddess(g1);
}
}
public class GoddessDao1 {
public void addGoddess(Goddess g) throws SQLException {
Connection conn = DBUtil1.getConnection();
String sql="" +
"insert into imooc_goddess" +
"(user_name,sex,age,birthday,email,mobile," +
"create_user,create_date,update_user,update_date,isdel)" +
"values(" +
"?,?,?,?,?,?,?,current_date(),?,current_date(),?)";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setString(1, g.getUser_name());
ptmt.setInt(2, g.getSex());
ptmt.setInt(3, g.getAge());
ptmt.setDate(4, new Date(g.getBirthday().getTime()));
ptmt.setString(5, g.getEmail());
ptmt.setString(6, g.getMobile());
ptmt.setString(7, g.getCreate_user());
ptmt.setString(8, g.getUpdate_user());
ptmt.setInt(9, g.getIsdel());
ptmt.execute();
}
public void updateGoddess() {
}
public void delGoddess() {
}
public List<Goddess> query() throws SQLException{
Connection conn = DBUtil1.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select user_name,age from imooc_goddess");
List<Goddess> gs = new ArrayList<Goddess>();
Goddess g = null;
while(rs.next()) {
g = new Goddess();
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
gs.add(g);
}
return gs;
}
public Goddess get() {
return null;
}
}
2-5 JDBC实战—搭建模型层 Ⅲ
public class GoddessAction1 {
public static void main(String[] args) throws Exception {
GoddessDao1 g = new GoddessDao1();
Goddess g1 = new Goddess();
g1.setUser_name("小夏");
g1.setAge(24);
g1.setSex(1);
g1.setBirthday(new Date());
g1.setEmail("xiao@126.com");
g1.setMobile("15312345678");
g1.setUpdate_user("ADMIN");
g1.setIsdel(1);
g1.setId(6);
// g.addGoddess(g1);
// g.updateGoddess(g1);
// g.delGoddess(6);
Goddess g2 = g.get(7);
System.out.println(g2.toString());
}
}
public class GoddessDao1 {
public void addGoddess(Goddess g) throws SQLException {
Connection conn = DBUtil1.getConnection();
String sql="" +
"insert into imooc_goddess" +
"(user_name,sex,age,birthday,email,mobile," +
"create_user,create_date,update_user,update_date,isdel)" +
"values(" +
"?,?,?,?,?,?,?,current_date(),?,current_date(),?)";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setString(1, g.getUser_name());
ptmt.setInt(2, g.getSex());
ptmt.setInt(3, g.getAge());
ptmt.setDate(4, new Date(g.getBirthday().getTime()));
ptmt.setString(5, g.getEmail());
ptmt.setString(6, g.getMobile());
ptmt.setString(7, g.getCreate_user());
ptmt.setString(8, g.getUpdate_user());
ptmt.setInt(9, g.getIsdel());
ptmt.execute();
}
public void updateGoddess(Goddess g) throws SQLException {
Connection conn = DBUtil1.getConnection();
String sql="" +
" update imooc_goddess " +
" set user_name=?,sex=?,age=?,birthday=?,email=?,mobile=?, " +
" update_user=?,update_date=current_date(),isdel=? " +
" where id=? ";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1, g.getUser_name());
ptmt.setInt(2, g.getSex());
ptmt.setInt(3, g.getAge());
ptmt.setDate(4, new Date(g.getBirthday().getTime()));
ptmt.setString(5, g.getEmail());
ptmt.setString(6, g.getMobile());
ptmt.setString(7, g.getUpdate_user());
ptmt.setInt(8, g.getIsdel());
ptmt.setInt(9, g.getId());
ptmt.execute();
}
public void delGoddess(Integer id) throws SQLException {
Connection conn = DBUtil1.getConnection();
String sql="" +
" delete from imooc_goddess " +
" where id=? ";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setInt(1, id);
ptmt.execute();
}
public List<Goddess> query() throws SQLException{
Connection conn = DBUtil1.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select user_name,age from imooc_goddess");
List<Goddess> gs = new ArrayList<Goddess>();
Goddess g = null;
while(rs.next()) {
g = new Goddess();
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
gs.add(g);
}
return gs;
}
public Goddess get() {
return null;
}
}
2-6 JDBC实战—搭建模型层 Ⅳ
public static void main(String[] args) throws Exception {
GoddessDao g = new GoddessDao();
List<Map<String,Object>> params = new ArrayList<Map<String,Object>>();
Map<String,Object> param = new HashMap<String,Object>();
param.put("name", "user_name");
param.put("rela", "like");
param.put("value", "'%小美%'");
param = new HashMap<String,Object>();
param.put("name", "mobile");
param.put("rela", "=");
param.put("value", "15312345678");
params.add(param);
List<Goddess> result = g.query(params);
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i).toString());
}
}
}
public class GoddessDao {
public void addGoddess(Goddess g) throws Exception{
Connection conn=DBUtil.getConnection();
String sql="" +
"insert into imooc_goddess" +
"(user_name,sex,age,birthday,email,mobile," +
"create_user,create_date,update_user,update_date,isdel)" +
"values(" +
"?,?,?,?,?,?,?,current_date(),?,current_date(),?)";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1, g.getUser_name());
ptmt.setInt(2, g.getSex());
ptmt.setInt(3, g.getAge());
ptmt.setDate(4, new Date(g.getBirthday().getTime()));
ptmt.setString(5, g.getEmail());
ptmt.setString(6, g.getMobile());
ptmt.setString(7, g.getCreate_user());
ptmt.setString(8, g.getUpdate_user());
ptmt.setInt(9, g.getIsdel());
ptmt.execute();
}
public void updateGoddess(Goddess g) throws SQLException{
Connection conn=DBUtil.getConnection();
String sql="" +
" update imooc_goddess " +
" set user_name=?,sex=?,age=?,birthday=?,email=?,mobile=?, " +
" update_user=?,update_date=current_date(),isdel=? " +
" where id=? ";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setString(1, g.getUser_name());
ptmt.setInt(2, g.getSex());
ptmt.setInt(3, g.getAge());
ptmt.setDate(4, new Date(g.getBirthday().getTime()));
ptmt.setString(5, g.getEmail());
ptmt.setString(6, g.getMobile());
ptmt.setString(7, g.getUpdate_user());
ptmt.setInt(8, g.getIsdel());
ptmt.setInt(9, g.getId());
ptmt.execute();
}
public void delGoddess(Integer id) throws SQLException{
Connection conn=DBUtil.getConnection();
String sql="" +
" delete from imooc_goddess " +
" where id=? ";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setInt(1, id);
ptmt.execute();
}
public List<Goddess> query() throws Exception{
List<Goddess> result=new ArrayList<Goddess>();
Connection conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder();
sb.append("select id,user_name,age from imooc_goddess ");
PreparedStatement ptmt=conn.prepareStatement(sb.toString());
ResultSet rs=ptmt.executeQuery();
Goddess g=null;
while(rs.next()){
g=new Goddess();
g.setId(rs.getInt("id"));
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
result.add(g);
}
return result;
}
public List<Goddess> query(String name,String mobile,String email) throws Exception{
List<Goddess> result=new ArrayList<Goddess>();
Connection conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder();
sb.append("select * from imooc_goddess ");
sb.append(" where user_name like ? and mobile like ? and email like ?");
PreparedStatement ptmt=conn.prepareStatement(sb.toString());
ptmt.setString(1, "%"+name+"%");
ptmt.setString(2, "%"+mobile+"%");
ptmt.setString(3, "%"+email+"%");
System.out.println(sb.toString());
ResultSet rs=ptmt.executeQuery();
Goddess g=null;
while(rs.next()){
g=new Goddess();
g.setId(rs.getInt("id"));
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
g.setSex(rs.getInt("sex"));
g.setBirthday(rs.getDate("birthday"));
g.setEmail(rs.getString("email"));
g.setMobile(rs.getString("mobile"));
g.setCreate_date(rs.getDate("create_date"));
g.setCreate_user(rs.getString("create_user"));
g.setUpdate_date(rs.getDate("update_date"));
g.setUpdate_user(rs.getString("update_user"));
g.setIsdel(rs.getInt("isdel"));
result.add(g);
}
return result;
}
public List<Goddess> query(List<Map<String, Object>> params) throws Exception{
List<Goddess> result=new ArrayList<Goddess>();
Connection conn=DBUtil.getConnection();
StringBuilder sb=new StringBuilder();
sb.append("select * from imooc_goddess where 1=1 ");
if(params!=null&¶ms.size()>0){
for (int i = 0; i < params.size(); i++) {
Map<String, Object> map=params.get(i);
sb.append(" and "+map.get("name")+" "+map.get("rela")+" "+map.get("value")+" ");
}
}
PreparedStatement ptmt=conn.prepareStatement(sb.toString());
System.out.println(sb.toString());
ResultSet rs=ptmt.executeQuery();
Goddess g=null;
while(rs.next()){
g=new Goddess();
g.setId(rs.getInt("id"));
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
g.setSex(rs.getInt("sex"));
g.setBirthday(rs.getDate("birthday"));
g.setEmail(rs.getString("email"));
g.setMobile(rs.getString("mobile"));
g.setCreate_date(rs.getDate("create_date"));
g.setCreate_user(rs.getString("create_user"));
g.setUpdate_date(rs.getDate("update_date"));
g.setUpdate_user(rs.getString("update_user"));
g.setIsdel(rs.getInt("isdel"));
result.add(g);
}
return result;
}
public Goddess get(Integer id) throws SQLException{
Goddess g=null;
Connection conn=DBUtil.getConnection();
String sql="" +
" select * from imooc_goddess " +
" where id=? ";
PreparedStatement ptmt=conn.prepareStatement(sql);
ptmt.setInt(1, id);
ResultSet rs=ptmt.executeQuery();
while(rs.next()){
g=new Goddess();
g.setId(rs.getInt("id"));
g.setUser_name(rs.getString("user_name"));
g.setAge(rs.getInt("age"));
g.setSex(rs.getInt("sex"));
g.setBirthday(rs.getDate("birthday"));
g.setEmail(rs.getString("email"));
g.setMobile(rs.getString("mobile"));
g.setCreate_date(rs.getDate("create_date"));
g.setCreate_user(rs.getString("create_user"));
g.setUpdate_date(rs.getDate("update_date"));
g.setUpdate_user(rs.getString("update_user"));
g.setIsdel(rs.getInt("isdel"));
}
return g;
}
}
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/imooc?useUnicode=true&characterEncoding=utf8";
private static final String USER="root";
private static final String PASSWORD="root";
private static Connection conn=null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
return conn;
}
}
2-7 JDBC实战—搭建控制层
public class TestAction {
public static void main(String[] args) throws Exception {
GoddessAction action=new GoddessAction();
/*查询*/
/**/
Goddess g=new Goddess();
g.setUser_name("小青");
g.setSex(1);
g.setAge(25);
g.setBirthday(new Date());
g.setEmail("xiaoqing@163.com");
g.setMobile("15688888888");
g.setIsdel(0);
g.setId(7);
// action.add(g);
// action.edit(g);
// action.del(7);
List<Map<String, Object>> params=new ArrayList<Map<String,Object>>();
Map<String, Object> map=new HashMap<String, Object>();
map.put("name", "user_name");
map.put("rela", "=");
map.put("value", "'小美'");
params.add(map);
List<Goddess> result=action.query(params);
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i).getId()+
":"+result.get(i).getUser_name());
}
}
}
public class GoddessAction {
public void add(Goddess goddess) throws Exception{
GoddessDao dao=new GoddessDao();
goddess.setSex(1);
goddess.setCreate_user("ADMIN");
goddess.setUpdate_user("ADMIN");
goddess.setIsdel(0);
dao.addGoddess(goddess);
}
public Goddess get(Integer id) throws SQLException{
GoddessDao dao=new GoddessDao();
return dao.get(id);
}
public void edit(Goddess goddess) throws Exception{
GoddessDao dao=new GoddessDao();
dao.updateGoddess(goddess);
}
public void del(Integer id) throws SQLException{
GoddessDao dao=new GoddessDao();
dao.delGoddess(id);
}
public List<Goddess> query() throws Exception{
GoddessDao dao=new GoddessDao();
return dao.query();
}
public List<Goddess> query(List<Map<String, Object>> params) throws Exception{
GoddessDao dao=new GoddessDao();
return dao.query(params);
}
}
2-8 JDBC实战—搭建视图层(上)
试图层
- 流程:
- 程序启动后,一直保持在运行状态
- 循环接收控制台的输入参数
- 调用Action(控制层)响应,并将返回结果展示在控制台中
- 直到输入特定的输入标记(如EXIT)后,程序退出
- 问题点:
- 循环接收参数
- 某个功能的保持
2-9 JDBC实战—搭建视图层(下)
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) {
System.out.println(CONTEXT);
//怎么保持程序一直运行
Scanner scan=new Scanner(System.in);
Goddess goddess=new Goddess();
GoddessAction action=new GoddessAction();
String prenious=null;
Integer step=1;
while(scan.hasNext()){
String in=scan.next().toString();
if(OPERATION_EXIT.equals(in.toUpperCase())
||OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase())){
System.out.println("您已成功退出女神禁区。");
break;
}else if(OPERATION_QUERY.equals(in.toUpperCase())
||OPERATION_QUERY.substring(0, 1).equals(in.toUpperCase())){
try {
List<Goddess> list=action.query();
for (Goddess go : list) {
System.out.println(go.getId()+",姓名:"+go.getUser_name());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}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{
System.out.println("您输入的值为:"+in);
}
}
}
}
第3章 整理总结
3-1 课程总结
JDBC的基本概念
连接数据库的桥梁
由java语言编写的类和接口组成
可以为多种数据库提供统一的访问
JDBC各种连接方式的对比
1. JDBC+ODBC桥的方式
特点:需要数据库ODBC驱动,仅适用于微软的系统
2. JDBC+厂商API的形式
特点:厂商API一般使用C编写
3. JDBC+厂商Database Connection Server+DataBase的形式
特点:在JAVA与DATABASE之间架起了一台专门用于数据库连接的服务器(一般有数据库厂商提供)
4. JDBC+DATABASE的连接方式
*特点:这使得Application与数据库分开
开发者只需要关心内部逻辑的实现而不需注重数据库连接的具体实现*