JDBC 技术是Java开发必备的知识,并且是学习 Hibernate 和 Mybatis 的基础,本文章一步步剖析 JDBC 技术,由浅入深的了解 JDBC 技术的全貌,什么是 JDBC ,怎么使用 JDBC ,为进一步学习集成框架打下良好的基础。
主要内容:
1.JDBC简介
2.JDBC使用详解
3.JDBC各种连接方式的对比
JDBC简介
JDBC使用详解
通过实现简单通讯录项目来深入理解JDBC。
将多个联系人存入MySql数据库,同时拥有查看,添加,删除,修改等功能。
工具准备:MySql,MyEclipse,Navicat(数据库管理工具)。
1.创建MySql数据库
1 /* 2 Navicat MySQL Data Transfer 3 4 Source Server : localhost_3306 5 Source Server Version : 50544 6 Source Host : localhost:3306 7 Source Database : book 8 9 Target Server Type : MYSQL 10 Target Server Version : 50544 11 File Encoding : 65001 12 13 Date: 2018-03-22 14:13:39 14 */ 15 16 SET FOREIGN_KEY_CHECKS=0; 17 18 -- ---------------------------- 19 -- Table structure for addressbook 20 -- ---------------------------- 21 DROP TABLE IF EXISTS `addressbook`; 22 CREATE TABLE `addressbook` ( 23 `id` int(11) NOT NULL AUTO_INCREMENT, 24 `user_name` varchar(30) NOT NULL, 25 `sex` int(11) DEFAULT NULL, 26 `age` int(11) DEFAULT NULL, 27 `birthday` date DEFAULT NULL, 28 `email` varchar(30) DEFAULT NULL, 29 `mobile` varchar(11) DEFAULT NULL, 30 `create_user` varchar(30) DEFAULT NULL, 31 `create_date` date DEFAULT NULL, 32 `update_user` varchar(30) DEFAULT NULL, 33 `update_date` date DEFAULT NULL, 34 `isdel` int(11) DEFAULT NULL, 35 PRIMARY KEY (`id`) 36 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 37 38 -- ---------------------------- 39 -- Records of addressbook 40 -- ---------------------------- 41 INSERT INTO `addressbook` VALUES ('1', '王小明', '1', '20', '1998-02-17', 'xiaoming@163.com', '13911111111', 'ADMIN', '2018-03-22', 'ADMIN', '2018-03-22', '0');
2.JDBC获取数据库连接
1 public class DBUtil { 2 private static final String URL="jdbc:mysql://127.0.0.1:3306/book"; 3 private static final String USER="root"; 4 private static final String PASSWORD="123456"; 5 public static void main(String[] args) throws Exception { 6 //第一步:加载MySQL驱动 7 Class.forName("com.mysql.jdbc.Driver"); 8 9 //第二步获得数据库的连接: 10 Connection conn= DriverManager.getConnection(URL, USER, PASSWORD); 11 12 //第三步:创建statement对象,用于在已经建立的连接的基础上向数据库发送SQL语句的对象 13 Statement stmt =conn.createStatement(); 14 15 //通过数据库的连接操作数据库,实现填删改查 16 ResultSet rs=stmt.executeQuery("SELECT user_name,age FROM address Book"); 17 while(rs.next()){ 18 System.out.println(rs.getString("user_name")+" "+rs.getInt("age")); 19 } 20 } 21 }
3.详设,采用MVC三层架构
4.代码实现:
获取数据库连接:
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.SQLException; 4 public class DBUtil { 5 6 private static final String URL="jdbc:mysql://127.0.0.1:3306/book?useUnicode=true&characterEncoding=utf-8"; 7 private static final String USER="root"; 8 private static final String PASSWORD=""; 9 10 private static Connection conn=null; 11 12 static { 13 try { 14 //1.加载驱动程序 15 Class.forName("com.mysql.jdbc.Driver"); 16 //2.获得数据库的连接 17 conn=DriverManager.getConnection(URL, USER, PASSWORD); 18 } catch (ClassNotFoundException e) { 19 e.printStackTrace(); 20 } catch (SQLException e) { 21 e.printStackTrace(); 22 } 23 } 24 25 public static Connection getConnection(){ 26 return conn; 27 } 28 29 }
模型层:
1 import java.util.Date; 2 3 public class Goddess { 4 5 private Integer id; 6 private String user_name; 7 private Integer sex; 8 private Integer age; 9 private Date birthday; 10 private String email; 11 private String mobile; 12 private String create_user; 13 private String update_user; 14 private Date create_date; 15 private Date update_date; 16 private Integer isdel; 17 18 public Integer getId() { 19 return id; 20 } 21 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 26 public String getUser_name() { 27 return user_name; 28 } 29 30 public void setUser_name(String user_name) { 31 this.user_name = user_name; 32 } 33 34 public Integer getSex() { 35 return sex; 36 } 37 38 public void setSex(Integer sex) { 39 this.sex = sex; 40 } 41 42 public Integer getAge() { 43 return age; 44 } 45 46 public void setAge(Integer age) { 47 this.age = age; 48 } 49 50 public Date getBirthday() { 51 return birthday; 52 } 53 54 public void setBirthday(Date birthday) { 55 this.birthday = birthday; 56 } 57 58 public String getEmail() { 59 return email; 60 } 61 62 public void setEmail(String email) { 63 this.email = email; 64 } 65 66 public String getMobile() { 67 return mobile; 68 } 69 70 public void setMobile(String mobile) { 71 this.mobile = mobile; 72 } 73 74 public String getCreate_user() { 75 return create_user; 76 } 77 78 public void setCreate_user(String create_user) { 79 this.create_user = create_user; 80 } 81 82 public String getUpdate_user() { 83 return update_user; 84 } 85 86 public void setUpdate_user(String update_user) { 87 this.update_user = update_user; 88 } 89 90 public Date getCreate_date() { 91 return create_date; 92 } 93 94 public void setCreate_date(Date create_date) { 95 this.create_date = create_date; 96 } 97 98 public Date getUpdate_date() { 99 return update_date; 100 } 101 102 public void setUpdate_date(Date update_date) { 103 this.update_date = update_date; 104 } 105 106 public Integer getIsdel() { 107 return isdel; 108 } 109 110 public void setIsdel(Integer isdel) { 111 this.isdel = isdel; 112 } 113 114 @Override 115 public String toString() { 116 return "Goddess [id=" + id + ", user_name=" + user_name + ", sex=" 117 + sex + ", age=" + age + ", birthday=" + birthday + ", email=" 118 + email + ", mobile=" + mobile + ", create_user=" + create_user 119 + ", update_user=" + update_user + ", create_date=" 120 + create_date + ", update_date=" + update_date + ", isdel=" 121 + isdel + "]"; 122 } 123 }
1 import java.sql.Connection; 2 import java.sql.Date; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.util.ArrayList; 7 import java.util.List; 8 import java.util.Map; 9 10 public class GoddessDao { 11 12 public void addGoddess(Goddess g) throws Exception{ 13 Connection conn=DBUtil.getConnection(); 14 String sql="" + 15 "insert into addressbook" + 16 "(user_name,sex,age,birthday,email,mobile," + 17 "create_user,create_date,update_user,update_date,isdel)" + 18 "values(" + 19 "?,?,?,?,?,?,?,current_date(),?,current_date(),?)"; 20 PreparedStatement ptmt=conn.prepareStatement(sql); 21 22 ptmt.setString(1, g.getUser_name()); 23 ptmt.setInt(2, g.getSex()); 24 ptmt.setInt(3, g.getAge()); 25 ptmt.setDate(4, new Date(g.getBirthday().getTime())); 26 ptmt.setString(5, g.getEmail()); 27 ptmt.setString(6, g.getMobile()); 28 ptmt.setString(7, g.getCreate_user()); 29 ptmt.setString(8, g.getUpdate_user()); 30 ptmt.setInt(9, g.getIsdel()); 31 ptmt.execute(); 32 } 33 34 public void updateGoddess(Goddess g) throws SQLException{ 35 Connection conn=DBUtil.getConnection(); 36 String sql="" + 37 " update addressbook " + 38 " set user_name=?,sex=?,age=?,birthday=?,email=?,mobile=?, " + 39 " update_user=?,update_date=current_date(),isdel=? " + 40 " where id=? "; 41 PreparedStatement ptmt=conn.prepareStatement(sql); 42 43 ptmt.setString(1, g.getUser_name()); 44 ptmt.setInt(2, g.getSex()); 45 ptmt.setInt(3, g.getAge()); 46 ptmt.setDate(4, new Date(g.getBirthday().getTime())); 47 ptmt.setString(5, g.getEmail()); 48 ptmt.setString(6, g.getMobile()); 49 ptmt.setString(7, g.getUpdate_user()); 50 ptmt.setInt(8, g.getIsdel()); 51 ptmt.setInt(9, g.getId()); 52 ptmt.execute(); 53 } 54 55 public void delGoddess(Integer id) throws SQLException{ 56 Connection conn=DBUtil.getConnection(); 57 String sql="" + 58 " delete from addressbook " + 59 " where id=? "; 60 PreparedStatement ptmt=conn.prepareStatement(sql); 61 62 ptmt.setInt(1, id); 63 ptmt.execute(); 64 } 65 public List<Goddess> query() throws Exception{ 66 List<Goddess> result=new ArrayList<Goddess>(); 67 68 Connection conn=DBUtil.getConnection(); 69 StringBuilder sb=new StringBuilder(); 70 sb.append("select id,user_name,age from addressbook "); 71 72 PreparedStatement ptmt=conn.prepareStatement(sb.toString()); 73 74 ResultSet rs=ptmt.executeQuery(); 75 76 Goddess g=null; 77 while(rs.next()){ 78 g=new Goddess(); 79 g.setId(rs.getInt("id")); 80 g.setUser_name(rs.getString("user_name")); 81 g.setAge(rs.getInt("age")); 82 result.add(g); 83 } 84 return result; 85 } 86 public List<Goddess> query(String name,String mobile,String email) throws Exception{ 87 List<Goddess> result=new ArrayList<Goddess>(); 88 89 Connection conn=DBUtil.getConnection(); 90 StringBuilder sb=new StringBuilder(); 91 sb.append("select * from addressbook "); 92 93 sb.append(" where user_name like ? and mobile like ? and email like ?"); 94 95 PreparedStatement ptmt=conn.prepareStatement(sb.toString()); 96 ptmt.setString(1, "%"+name+"%"); 97 ptmt.setString(2, "%"+mobile+"%"); 98 ptmt.setString(3, "%"+email+"%"); 99 System.out.println(sb.toString()); 100 ResultSet rs=ptmt.executeQuery(); 101 102 Goddess g=null; 103 while(rs.next()){ 104 g=new Goddess(); 105 g.setId(rs.getInt("id")); 106 g.setUser_name(rs.getString("user_name")); 107 g.setAge(rs.getInt("age")); 108 g.setSex(rs.getInt("sex")); 109 g.setBirthday(rs.getDate("birthday")); 110 g.setEmail(rs.getString("email")); 111 g.setMobile(rs.getString("mobile")); 112 g.setCreate_date(rs.getDate("create_date")); 113 g.setCreate_user(rs.getString("create_user")); 114 g.setUpdate_date(rs.getDate("update_date")); 115 g.setUpdate_user(rs.getString("update_user")); 116 g.setIsdel(rs.getInt("isdel")); 117 118 result.add(g); 119 } 120 return result; 121 } 122 public List<Goddess> query(List<Map<String, Object>> params) throws Exception{ 123 List<Goddess> result=new ArrayList<Goddess>(); 124 125 Connection conn=DBUtil.getConnection(); 126 StringBuilder sb=new StringBuilder(); 127 sb.append("select * from addressbook where 1=1 "); 128 129 if(params!=null&¶ms.size()>0){ 130 for (int i = 0; i < params.size(); i++) { 131 Map<String, Object> map=params.get(i); 132 sb.append(" and "+map.get("name")+" "+map.get("rela")+" "+map.get("value")+" "); 133 } 134 } 135 136 PreparedStatement ptmt=conn.prepareStatement(sb.toString()); 137 138 System.out.println(sb.toString()); 139 ResultSet rs=ptmt.executeQuery(); 140 141 Goddess g=null; 142 while(rs.next()){ 143 g=new Goddess(); 144 g.setId(rs.getInt("id")); 145 g.setUser_name(rs.getString("user_name")); 146 g.setAge(rs.getInt("age")); 147 g.setSex(rs.getInt("sex")); 148 g.setBirthday(rs.getDate("birthday")); 149 g.setEmail(rs.getString("email")); 150 g.setMobile(rs.getString("mobile")); 151 g.setCreate_date(rs.getDate("create_date")); 152 g.setCreate_user(rs.getString("create_user")); 153 g.setUpdate_date(rs.getDate("update_date")); 154 g.setUpdate_user(rs.getString("update_user")); 155 g.setIsdel(rs.getInt("isdel")); 156 157 result.add(g); 158 } 159 return result; 160 } 161 public Goddess get(Integer id) throws SQLException{ 162 Goddess g=null; 163 Connection conn=DBUtil.getConnection(); 164 String sql="" + 165 " select * from addressbook " + 166 " where id=? "; 167 PreparedStatement ptmt=conn.prepareStatement(sql); 168 169 ptmt.setInt(1, id); 170 ResultSet rs=ptmt.executeQuery(); 171 while(rs.next()){ 172 g=new Goddess(); 173 g.setId(rs.getInt("id")); 174 g.setUser_name(rs.getString("user_name")); 175 g.setAge(rs.getInt("age")); 176 g.setSex(rs.getInt("sex")); 177 g.setBirthday(rs.getDate("birthday")); 178 g.setEmail(rs.getString("email")); 179 g.setMobile(rs.getString("mobile")); 180 g.setCreate_date(rs.getDate("create_date")); 181 g.setCreate_user(rs.getString("create_user")); 182 g.setUpdate_date(rs.getDate("update_date")); 183 g.setUpdate_user(rs.getString("update_user")); 184 g.setIsdel(rs.getInt("isdel")); 185 } 186 return g; 187 } 188 }
控制层:
1 import java.sql.SQLException; 2 import java.util.List; 3 import java.util.Map; 4 5 public class GoddessAction { 6 7 public void add(Goddess goddess) throws Exception{ 8 GoddessDao dao=new GoddessDao(); 9 goddess.setSex(1); 10 goddess.setCreate_user("ADMIN"); 11 goddess.setUpdate_user("ADMIN"); 12 goddess.setIsdel(0); 13 dao.addGoddess(goddess); 14 } 15 16 public Goddess get(Integer id) throws SQLException{ 17 GoddessDao dao=new GoddessDao(); 18 return dao.get(id); 19 } 20 21 public void edit(Goddess goddess) throws Exception{ 22 GoddessDao dao=new GoddessDao(); 23 dao.updateGoddess(goddess); 24 } 25 public void del(Integer id) throws SQLException{ 26 GoddessDao dao=new GoddessDao(); 27 dao.delGoddess(id); 28 } 29 30 public List<Goddess> query() throws Exception{ 31 GoddessDao dao=new GoddessDao(); 32 return dao.query(); 33 } 34 public List<Goddess> query(List<Map<String, Object>> params) throws Exception{ 35 GoddessDao dao=new GoddessDao(); 36 return dao.query(params); 37 } 38 }
视图层:
1 import java.text.SimpleDateFormat; 2 import java.util.ArrayList; 3 import java.util.Date; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Scanner; 8 9 public class View { 10 11 private static final String CONTEXT="欢迎来到简单通讯录: " + 12 "下面是简单通讯录的功能列表: " + 13 "[MAIN/M]:主菜单 " + 14 "[QUERY/Q]:查看全部联系人的信息 " + 15 "[GET/G]:查看某位联系人的详细信息 " + 16 "[ADD/A]:添加联系人信息 " + 17 "[UPDATE/U]:更新联系人信息 " + 18 "[DELETE/D]:删除联系人信息 " + 19 "[SEARCH/S]:查询联系人信息(根据姓名、手机号来查询) " + 20 "[EXIT/E]:退出简单通讯录 " + 21 "[BREAK/B]:退出当前功能,返回主菜单"; 22 23 24 private static final String OPERATION_MAIN="MAIN"; 25 private static final String OPERATION_QUERY="QUERY"; 26 private static final String OPERATION_GET="GET"; 27 private static final String OPERATION_ADD="ADD"; 28 private static final String OPERATION_UPDATE="UPDATE"; 29 private static final String OPERATION_DELETE="DELETE"; 30 private static final String OPERATION_SEARCH="SEARCH"; 31 private static final String OPERATION_EXIT="EXIT"; 32 private static final String OPERATION_BREAK="BREAK"; 33 34 35 public static void main(String[] args) throws Exception { 36 System.out.println(CONTEXT); 37 38 Scanner s=new Scanner(System.in); 39 GoddessAction action=new GoddessAction(); 40 41 String pervious=null; 42 Integer step=1; 43 Goddess go=null; 44 45 while(s.hasNext()){ 46 String in=s.next(); 47 if(OPERATION_EXIT.equals(in.toUpperCase()) 48 ||OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase())){ 49 System.out.println("您已成功退出简单通讯录"); 50 break; 51 }else if(OPERATION_MAIN.equals(in.toUpperCase()) 52 ||OPERATION_MAIN.substring(0, 1).equals(in.toUpperCase())){ 53 step=1; 54 pervious=null; 55 go=null; 56 System.out.println(CONTEXT); 57 } 58 else if(OPERATION_BREAK.equals(in.toUpperCase()) 59 ||OPERATION_BREAK.substring(0, 1).equals(in.toUpperCase())){ 60 step=1; 61 pervious=null; 62 go=null; 63 System.out.println(CONTEXT); 64 }else if(OPERATION_QUERY.equals(in.toUpperCase()) 65 ||OPERATION_QUERY.substring(0, 1).equals(in.toUpperCase())){ 66 List<Goddess> list=action.query(); 67 for (Goddess goddess : list) { 68 System.out.println(goddess.toString()); 69 } 70 }else if(OPERATION_GET.equals(in.toUpperCase()) 71 ||OPERATION_GET.substring(0, 1).equals(in.toUpperCase()) 72 ||OPERATION_GET.equals(pervious)){ 73 pervious=OPERATION_GET; 74 if(1==step){ 75 System.out.println("请输入查询的联系人ID:"); 76 }else if(step>1){ 77 Integer id=null; 78 Goddess g; 79 try { 80 id = Integer.valueOf(in); 81 try { 82 g = action.get(id); 83 if(g==null){ 84 System.out.println("查询联系人信息失败"); 85 }else{ 86 System.out.println(g.toString()); 87 } 88 } catch (Exception e) { 89 System.out.println("查询联系人信息失败"); 90 } 91 } catch (Exception e) { 92 System.out.println("请输入正确的联系人ID:"); 93 } 94 95 } 96 step++; 97 }else if(OPERATION_ADD.equals(in.toUpperCase()) 98 ||OPERATION_ADD.substring(0, 1).equals(in.toUpperCase()) 99 ||OPERATION_ADD.equals(pervious)){ 100 pervious=OPERATION_ADD; 101 if(1==step){ 102 System.out.println("请输入联系人的信息[姓名]:"); 103 }else if(2==step){ 104 go=new Goddess(); 105 go.setUser_name(in); 106 System.out.println("请输入联系人的信息[年龄]:"); 107 }else if(3==step){ 108 Integer age=null; 109 try { 110 age = Integer.valueOf(in); 111 go.setAge(age); 112 System.out.println("请输入联系人的信息[生日,格式:2014-12-12]:"); 113 } catch (Exception e) { 114 step=2; 115 System.out.println("请输入正确联系人的信息[年龄]:"); 116 } 117 }else if(4==step){ 118 SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd"); 119 Date birthday=null; 120 try { 121 birthday = sf.parse(in); 122 go.setBirthday(birthday); 123 System.out.println("请输入联系人的信息[邮箱]:"); 124 } catch (Exception e) { 125 step=3; 126 System.out.println("请输入正确联系人的信息[生日]:"); 127 } 128 }else if(5==step){ 129 go.setEmail(in); 130 System.out.println("请输入联系人的信息[手机号]:"); 131 }else if(6==step){ 132 go.setMobile(in); 133 try { 134 action.add(go); 135 } catch (Exception e) { 136 System.out.println("新增联系人信息失败"); 137 } 138 System.out.println("新增联系人信息成功"); 139 step=1; 140 pervious=null; 141 } 142 if(OPERATION_ADD.equals(pervious)){ 143 step++; 144 } 145 }else if(OPERATION_UPDATE.equals(in.toUpperCase()) 146 ||OPERATION_UPDATE.substring(0, 1).equals(in.toUpperCase()) 147 ||OPERATION_UPDATE.equals(pervious)){ 148 pervious=OPERATION_UPDATE; 149 if(1==step){ 150 System.out.println("请输入要修改的联系人ID:"); 151 }else if(2==step){ 152 Integer id=null; 153 try { 154 id = Integer.valueOf(in); 155 try { 156 go = action.get(id); 157 if(go==null){ 158 System.out.println("查询联系人信息失败"); 159 step=1; 160 } 161 } catch (Exception e) { 162 System.out.println("查询联系人信息失败"); 163 step=1; 164 } 165 } catch (Exception e) { 166 System.out.println("请输入正确的联系人ID:"); 167 step=1; 168 } 169 System.out.println("请输入新的联系人信息[姓名],如果不修改该值,请输入null:"); 170 }else if(3==step){ 171 if(null!=in){ 172 go.setUser_name(in); 173 } 174 System.out.println("请输入新的联系人信息[年龄],如果不修改该值,请输入-1:"); 175 }else if(4==step){ 176 Integer age=null; 177 try { 178 age = Integer.valueOf(in); 179 if(-1!=age){ 180 go.setAge(age); 181 } 182 System.out.println("请输入新的联系人信息[生日,格式:2014-12-12],如果不修改该值,请输入null:"); 183 } catch (Exception e) { 184 step=3; 185 System.out.println("请输入正确联系人的信息[年龄]:"); 186 } 187 }else if(5==step){ 188 SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd"); 189 Date birthday=null; 190 try { 191 if(null!=in){ 192 birthday = sf.parse(in); 193 go.setBirthday(birthday); 194 } 195 System.out.println("请输入新的联系人信息[邮箱],如果不修改该值,请输入null:"); 196 } catch (Exception e) { 197 step=4; 198 System.out.println("请输入正确联系人的信息[生日]:"); 199 } 200 }else if(6==step){ 201 if(null!=in){ 202 go.setEmail(in); 203 } 204 System.out.println("请输入新的联系人信息[手机号],如果不修改该值,请输入null:"); 205 }else if(7==step){ 206 if(null!=in){ 207 go.setMobile(in); 208 } 209 try { 210 action.edit(go); 211 } catch (Exception e) { 212 System.out.println("更新联系人信息失败"); 213 } 214 System.out.println("更新联系人信息成功"); 215 step=1; 216 pervious=null; 217 } 218 if(OPERATION_UPDATE.equals(pervious)){ 219 step++; 220 } 221 }else if(OPERATION_DELETE.equals(in.toUpperCase()) 222 ||OPERATION_DELETE.substring(0, 1).equals(in.toUpperCase()) 223 ||OPERATION_DELETE.equals(pervious)){ 224 pervious=OPERATION_DELETE; 225 if(1==step){ 226 System.out.println("请输入要删除的联系人ID:"); 227 }else if(2==step){ 228 Integer id=null; 229 try { 230 id = Integer.valueOf(in); 231 try { 232 action.del(id); 233 step=1; 234 System.out.println("删除联系人信息成功"); 235 } catch (Exception e) { 236 System.out.println("删除联系人信息失败"); 237 } 238 } catch (Exception e) { 239 System.out.println("请输入正确的联系人ID:"); 240 step=1; 241 } 242 } 243 if(OPERATION_DELETE.equals(pervious)){ 244 step++; 245 } 246 }else if(OPERATION_SEARCH.equals(in.toUpperCase()) 247 ||OPERATION_SEARCH.substring(0, 1).equals(in.toUpperCase()) 248 ||OPERATION_SEARCH.equals(pervious)){ 249 pervious=OPERATION_SEARCH; 250 if(1==step){ 251 System.out.println("请输入要查询的联系人信息,支持姓名、手机号查询,如果两个参数都输入则用逗号分隔[user_name=xx,mobile=xx]:"); 252 }else if(2==step){ 253 if(in!=null&&in!=""){ 254 List<Map<String, Object>> params=new ArrayList<Map<String,Object>>(); 255 Map<String, Object> param=null; 256 String[] strs=in.split(","); 257 for (int i = 0; i < strs.length; i++) { 258 String[] strs_s=strs[i].split("="); 259 param=new HashMap<String, Object>(); 260 param.put("name", strs_s[0]); 261 param.put("rela", "="); 262 param.put("value", "'"+strs_s[1]+"'"); 263 params.add(param); 264 } 265 List<Goddess> list=action.query(params); 266 if(list!=null&&list.size()>0){ 267 for (Goddess goddess : list) { 268 System.out.println(goddess.toString()); 269 } 270 }else{ 271 System.out.println("没有查询到联系人信息。。。"); 272 } 273 step=1; 274 } 275 } 276 if(OPERATION_SEARCH.equals(pervious)){ 277 step++; 278 } 279 } 280 } 281 } 282 }
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与数据库分开,开发者只需关心内部逻辑的实现而不需注重数据库连接的具体实现。(没有中间环节,是推荐方式!)