【Java基础知识】JDBC基本操作

Jdbc简介


JDBC(Java Data Base Connectivity )(java 数据库连接)

可以为多种数据库提供统一的数据库访问。


JDBC使用详解

 

JDBC编程步骤

1.      加载驱动程序:Class.forName(driverClass)

加载Mysql驱动Class.forName(“com.mysql.jdbc.Driver”);

加载Oracle驱动:Class.forName(“oracle.jdbc.driver.OracleDriver”);

2.      获得数据库连接

DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/imooc”,”root”,”root”);

3.      创建Statement对象:conn.createStatement();

 

例子:简单MVC模式数据库操作


1.首先创建我们的数据库。

[sql]  view plain  copy
  1. CREATE TABLE `imooc_goddess` (  
  2.   `id` int(11) NOT NULL AUTO_INCREMENT,  
  3.   `user_name` varchar(30) NOT NULL,  
  4.   `sex` int(11) DEFAULT NULL,  
  5.   `age` int(11) DEFAULT NULL,  
  6.   `birthday` date DEFAULT NULL,  
  7.   `email` varchar(30) DEFAULT NULL,  
  8.   `mobile` varchar(11) DEFAULT NULL,  
  9.   `create_user` varchar(30) DEFAULT NULL,  
  10.   `create_date` date DEFAULT NULL,  
  11.   `update_user` varchar(30) DEFAULT NULL,  
  12.   `update_date` date DEFAULT NULL,  
  13.   `isdel` int(11) DEFAULT NULL,  
  14.   PRIMARY KEY (`id`)  
  15. ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;  


2. 模型层M:Godness.java 类,和数据库字段相对应

[java]  view plain  copy
  1. /* 
  2.  * 模型层,创建类对应我们的数据库 
  3.  */  
  4. public class Godness {  
  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.     public Integer getId() {  
  18.         return id;  
  19.     }  
  20.     public void setId(Integer id) {  
  21.         this.id = id;  
  22.     }  
  23.     public String getUser_name() {  
  24.         return user_name;  
  25.     }  
  26.     public void setUser_name(String user_name) {  
  27.         this.user_name = user_name;  
  28.     }  
  29.     public Integer getSex() {  
  30.         return sex;  
  31.     }  
  32.     public void setSex(Integer sex) {  
  33.         this.sex = sex;  
  34.     }  
  35.     public Date getBirthday() {  
  36.         return birthday;  
  37.     }  
  38.     public void setBirthday(Date birthday) {  
  39.         this.birthday = birthday;  
  40.     }  
  41.     public String getEmail() {  
  42.         return email;  
  43.     }  
  44.     public void setEmail(String email) {  
  45.         this.email = email;  
  46.     }  
  47.     public String getMobile() {  
  48.         return mobile;  
  49.     }  
  50.     public void setMobile(String mobile) {  
  51.         this.mobile = mobile;  
  52.     }  
  53.     public String getCreate_user() {  
  54.         return create_user;  
  55.     }  
  56.     public void setCreate_user(String create_user) {  
  57.         this.create_user = create_user;  
  58.     }  
  59.     public String getUpdate_user() {  
  60.         return update_user;  
  61.     }  
  62.     public void setUpdate_user(String update_user) {  
  63.         this.update_user = update_user;  
  64.     }  
  65.     public Date getCreate_date() {  
  66.         return create_date;  
  67.     }  
  68.     public void setCreate_date(Date create_date) {  
  69.         this.create_date = create_date;  
  70.     }  
  71.     public Date getUpdate_date() {  
  72.         return update_date;  
  73.     }  
  74.     public void setUpdate_date(Date update_date) {  
  75.         this.update_date = update_date;  
  76.     }  
  77.     public Integer getIsdel() {  
  78.         return isdel;  
  79.     }  
  80.     public Integer getAge() {  
  81.         return age;  
  82.     }  
  83.     public void setAge(Integer age) {  
  84.         this.age = age;  
  85.     }  
  86.     public void setIsdel(Integer isdel) {  
  87.         this.isdel = isdel;  
  88.     }  
  89.     @Override  
  90.     public String toString() {  
  91.         return "Godness [id=" + id + ", user_name=" + user_name + ", sex="  
  92.                 + sex + ", age=" + age + ", birthday=" + birthday + ", email="  
  93.                 + email + ", mobile=" + mobile + ", create_user=" + create_user  
  94.                 + ", update_user=" + update_user + ", create_date="  
  95.                 + create_date + ", update_date=" + update_date + ", isdel="  
  96.                 + isdel + "]";  
  97.     }  
  98. }  
3.模型层M:DBUtil.java 获取数据库的连接

[java]  view plain  copy
  1. public class DBUtil {  
  2.   
  3.     private static final String URL = "jdbc:mysql://127.0.0.1:3306/jdbcdb";  
  4.     private static final String USER = "root";  
  5.     private static final String PASSWORD = "limeng";  
  6.   
  7.     private static Connection conn = null;  
  8.     static {  
  9.         // 1.加载驱动程序  
  10.         try {  
  11.             Class.forName("com.mysql.jdbc.Driver");  
  12.             // 2.获得数据库连接  
  13.             conn = DriverManager.getConnection(URL, USER, PASSWORD);  
  14.         } catch (ClassNotFoundException e) {  
  15.             e.printStackTrace();  
  16.         } catch (SQLException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.     }  
  20.   
  21.     public static Connection getConnection() {  
  22.         return conn;  
  23.     }  
  24. }  
4.模型层M:GodnessDao.java 增删改查方法。

[java]  view plain  copy
  1. //增删改查方法  
  2. public class GodnessDao {  
  3.     public void addGodness(Godness godness) throws SQLException{  
  4.         Connection conn = DBUtil.getConnection();  
  5.         String sql = ""+  
  6.                 "insert into imooc_goddess"+  
  7.                 "(user_name,sex,age,birthday,email,mobile,"+  
  8.                 "create_user,create_date,update_user,update_date,isdel)"+  
  9.                 "values("+  
  10.                 "?,?,?,?,?,?,?,current_date(),?,current_date(),?)";  
  11.         //预编译sql语句  
  12.         PreparedStatement ptmt = conn.prepareStatement(sql);  
  13.           
  14.         ptmt.setString(1, godness.getUser_name());  
  15.         ptmt.setInt(21);  
  16.         ptmt.setInt(3, godness.getAge());  
  17.         ptmt.setDate(4new Date(godness.getBirthday().getTime()));  
  18.         ptmt.setString(5, godness.getEmail());  
  19.         ptmt.setString(6, godness.getMobile());  
  20.         ptmt.setString(7, godness.getCreate_user());  
  21.         ptmt.setString(8, godness.getUpdate_user());  
  22.         ptmt.setInt(90);  
  23.         ptmt.execute();  
  24.     }  
  25.       
  26.     public void updateGodness(Godness godness) throws SQLException{  
  27.         Connection conn = DBUtil.getConnection();  
  28.         String sql = ""+  
  29.                 " update imooc_goddess"+  
  30.                 " set user_name=?,sex=?,age=?,birthday=?,email=?,mobile=?,"+  
  31.                 " create_user=?,update_user=?,update_date=current_date(),isdel=?"+  
  32.                 " where id=?";  
  33.         //预编译sql语句  
  34.         PreparedStatement ptmt = conn.prepareStatement(sql);  
  35.           
  36.         ptmt.setString(1, godness.getUser_name());  
  37.         ptmt.setInt(21);  
  38.         ptmt.setInt(3, godness.getAge());  
  39.         ptmt.setDate(4new Date(godness.getBirthday().getTime()));  
  40.         ptmt.setString(5, godness.getEmail());  
  41.         ptmt.setString(6, godness.getMobile());  
  42.         ptmt.setString(7, godness.getCreate_user());  
  43.         ptmt.setString(8, godness.getUpdate_user());  
  44.         ptmt.setInt(90);  
  45.         ptmt.setInt(10, godness.getId());  
  46.         ptmt.execute();  
  47.     }  
  48.       
  49.     public void delGoddness(Integer id) throws SQLException{  
  50.         Connection conn = DBUtil.getConnection();  
  51.         String sql = ""+  
  52.                 " delete from imooc_goddess"+  
  53.                 " where id=?";  
  54.         //预编译sql语句  
  55.         PreparedStatement ptmt = conn.prepareStatement(sql);  
  56.           
  57.         ptmt.setInt(1, id);  
  58.         ptmt.execute();  
  59.     }  
  60.     //查询所有的数据  
  61.     public List<Godness> query() throws SQLException{  
  62.         Connection conn = DBUtil.getConnection();  
  63.         Statement stmt = conn.createStatement();  
  64.         ResultSet rs = stmt.executeQuery("select * from imooc_goddess");  
  65.           
  66.         List<Godness> gs = new ArrayList<Godness>();  
  67.         Godness g = null;  
  68.         while (rs.next()) {  
  69.             g = new Godness();  
  70.             g.setId(rs.getInt("id"));  
  71.             g.setUser_name(rs.getString("user_name"));  
  72.             g.setAge(rs.getInt("age"));  
  73.             g.setSex(rs.getInt("sex"));  
  74.             g.setBirthday(rs.getDate("birthday"));  
  75.             g.setEmail(rs.getString("email"));  
  76.             g.setMobile(rs.getString("mobile"));  
  77.             g.setCreate_date(rs.getDate("create_date"));  
  78.             g.setCreate_user(rs.getString("create_user"));  
  79.             g.setUpdate_date(rs.getDate("update_date"));  
  80.             g.setUpdate_user(rs.getString("update_user"));  
  81.             g.setIsdel(rs.getInt("isdel"));  
  82.             gs.add(g);  
  83.         }  
  84.         return gs;  
  85.     }  
  86.     //根据姓名进行查询  
  87.     public List<Godness> query(String name,String mobile,String email) throws SQLException{  
  88.         List<Godness> result = new ArrayList<Godness>();  
  89.           
  90.         Connection conn = DBUtil.getConnection();  
  91.         StringBuilder sb = new StringBuilder();  
  92.         sb.append("select * from imooc_goddess ");  
  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.         ResultSet rs = ptmt.executeQuery();  
  100.         Godness g = null;  
  101.         while (rs.next()) {  
  102.             g = new Godness();  
  103.             g.setId(rs.getInt("id"));  
  104.             g.setUser_name(rs.getString("user_name"));  
  105.             g.setAge(rs.getInt("age"));  
  106.             g.setSex(rs.getInt("sex"));  
  107.             g.setBirthday(rs.getDate("birthday"));  
  108.             g.setEmail(rs.getString("email"));  
  109.             g.setMobile(rs.getString("mobile"));  
  110.             g.setCreate_date(rs.getDate("create_date"));  
  111.             g.setCreate_user(rs.getString("create_user"));  
  112.             g.setUpdate_date(rs.getDate("update_date"));  
  113.             g.setUpdate_user(rs.getString("update_user"));  
  114.             g.setIsdel(rs.getInt("isdel"));  
  115.             result.add(g);  
  116.         }  
  117.         return result;  
  118.     }  
  119.       
  120.     public List<Godness> query(List<Map<String,Object >> params) throws SQLException{  
  121.         List<Godness> result = new ArrayList<Godness>();  
  122.           
  123.         Connection conn = DBUtil.getConnection();  
  124.         StringBuilder sb = new StringBuilder();  
  125.         sb.append("select * from imooc_goddess where 1=1 ");  
  126.           
  127.           
  128.         if(params != null && params.size()>0){  
  129.             for(int i = 0; i<params.size();i++){  
  130.                 Map<String, Object> map = params.get(i);  
  131.                 sb.append(" and "+map.get("name")+" "+map.get("rela")+" "+map.get("value"));  
  132.             }  
  133.         }  
  134.           
  135.         PreparedStatement ptmt = conn.prepareStatement(sb.toString());  
  136.         ResultSet rs = ptmt.executeQuery();  
  137.         Godness g = null;  
  138.         while (rs.next()) {  
  139.             g = new Godness();  
  140.             g.setId(rs.getInt("id"));  
  141.             g.setUser_name(rs.getString("user_name"));  
  142.             g.setAge(rs.getInt("age"));  
  143.             g.setSex(rs.getInt("sex"));  
  144.             g.setBirthday(rs.getDate("birthday"));  
  145.             g.setEmail(rs.getString("email"));  
  146.             g.setMobile(rs.getString("mobile"));  
  147.             g.setCreate_date(rs.getDate("create_date"));  
  148.             g.setCreate_user(rs.getString("create_user"));  
  149.             g.setUpdate_date(rs.getDate("update_date"));  
  150.             g.setUpdate_user(rs.getString("update_user"));  
  151.             g.setIsdel(rs.getInt("isdel"));  
  152.             result.add(g);  
  153.         }  
  154.         return result;  
  155.     }  
  156.       
  157.     public Godness get(Integer id) throws SQLException{  
  158.         Connection conn = DBUtil.getConnection();  
  159.         String sql = ""+  
  160.                 " select * from imooc_goddess"+  
  161.                 " where id=?";  
  162.         //预编译sql语句  
  163.         PreparedStatement ptmt = conn.prepareStatement(sql);  
  164.           
  165.         ptmt.setInt(1, id);  
  166.         ResultSet rs =ptmt.executeQuery();  
  167.         Godness g = new Godness();  
  168.         while(rs.next()){  
  169.             g = new Godness();  
  170.             g.setId(rs.getInt("id"));  
  171.             g.setUser_name(rs.getString("user_name"));  
  172.             g.setAge(rs.getInt("age"));  
  173.             g.setSex(rs.getInt("sex"));  
  174.             g.setBirthday(rs.getDate("birthday"));  
  175.             g.setEmail(rs.getString("email"));  
  176.             g.setMobile(rs.getString("mobile"));  
  177.             g.setCreate_date(rs.getDate("create_date"));  
  178.             g.setCreate_user(rs.getString("create_user"));  
  179.             g.setUpdate_date(rs.getDate("update_date"));  
  180.             g.setUpdate_user(rs.getString("update_user"));  
  181.             g.setIsdel(rs.getInt("isdel"));  
  182.         }  
  183.         return g;  
  184.     }  
  185. }  
5.控制层C: GodnessAction.java 调用模型层的方法

[java]  view plain  copy
  1. //控制层  
  2. public class GodnessAction {  
  3.       
  4.     public void add(Godness godness) throws SQLException{  
  5.         GodnessDao dao = new GodnessDao();  
  6.         dao.addGodness(godness);  
  7.     }  
  8.       
  9.     public Godness get(Integer id) throws SQLException{  
  10.         GodnessDao dao = new GodnessDao();  
  11.         return dao.get(id);  
  12.     }  
  13.       
  14.     public void edit(Godness godness) throws SQLException{  
  15.         GodnessDao dao = new GodnessDao();  
  16.         dao.updateGodness(godness);  
  17.     }  
  18.       
  19.     public void del(Integer id) throws SQLException{  
  20.         GodnessDao dao = new GodnessDao();  
  21.         dao.delGoddness(id);  
  22.     }  
  23.       
  24.     public List<Godness> query() throws SQLException{  
  25.         GodnessDao dao= new GodnessDao();  
  26.         return dao.query();  
  27.     }  
  28.       
  29.     public List<Godness> query(List<Map<String,Object >> params) throws SQLException{  
  30.         GodnessDao dao = new GodnessDao();  
  31.         return dao.query(params);  
  32.     }  
  33. }  
6.视图层V: View.java 和用户进行数据交互。

[java]  view plain  copy
  1. public class View {  
  2.     private static final String CONTEXT = "欢迎来到女神禁区:\n" + "功能列表:\n"  
  3.             + "[MAIN/M]:主菜单\n" + "[QUERY/Q]:查看全部女神的信息\n"  
  4.             + "[GET/G]:查看某位女神的详细信息\n" + "[ADD/A]:添加女神信息\n"  
  5.             + "[UPDATE/U]:更新女神信息\n" + "[DELETE/D]:删除女神信息\n"  
  6.             + "[SEARCH/S]:查询女神信息(根据姓名、手机号来查询)\n" + "[EXIT/E]:退出女神禁区\n"  
  7.             + "[BREAK/B]:退出当前功能,返回主菜单";  
  8.     private static final String OPERATION_MAIN = "MAIN";  
  9.     private static final String OPERATION_QUERY = "QUERY";  
  10.     private static final String OPERATION_GET = "GET";  
  11.     private static final String OPERATION_ADD = "ADD";  
  12.     private static final String OPERATION_UPDATE = "UPDATE";  
  13.     private static final String OPERATION_DELETE = "DELETE";  
  14.     private static final String OPERATION_SEARCH = "SEARCH";  
  15.     private static final String OPERATION_EXIT = "EXIT";  
  16.     private static final String OPERATION_BREAK = "BREAK";  
  17.   
  18.     public static void main(String[] args) {  
  19.         System.out.println(CONTEXT);  
  20.   
  21.         Scanner scanner = new Scanner(System.in);  
  22.         Godness godness = new Godness();  
  23.         GodnessAction action = new GodnessAction();  
  24.         String prenious = null;  
  25.         Integer step=1;  
  26.         List<Map<String, Object>> params = new ArrayList<Map<String,Object>>();  
  27.         while (scanner.hasNext()) {  
  28.             String in = scanner.next().toString();  
  29.             if (OPERATION_EXIT.equals(in.toUpperCase())  
  30.                     || OPERATION_EXIT.substring(01).equals(in.toUpperCase())) {  
  31.                 System.out.println("你已退出");  
  32.                 break;  
  33.             }else if (OPERATION_MAIN.equals(in.toUpperCase())  
  34.                     || OPERATION_MAIN.substring(01).equals(in.toUpperCase())  
  35.                     ||OPERATION_BREAK.equals(in.toUpperCase())  
  36.                     ||OPERATION_BREAK.substring(01).equals(in.toUpperCase())){  
  37.                     System.out.println(CONTEXT);  
  38.             }else if (OPERATION_SEARCH.equals(in.toUpperCase())  
  39.                     || OPERATION_SEARCH.substring(01).equals(in.toUpperCase())  
  40.                     || OPERATION_SEARCH.equals(prenious)) {  
  41.                     //根据姓名和电话号码查询  
  42.                     prenious = OPERATION_SEARCH;  
  43.                     if(step ==1){  
  44.                           
  45.                         System.out.println("请输入女神的[姓名]");  
  46.                           
  47.                     }else if(step ==2){  
  48.                         Map<String, Object> p = new HashMap<String, Object>();  
  49.                         p.put("name""user_name");  
  50.                         p.put("rela""=");  
  51.                         p.put("value","'"+in+"'");  
  52.                         params.add(p);  
  53.                         System.out.println("请输入女神的[电话]");  
  54.                     }else if(step == 3){  
  55.                         Map<String, Object> p = new HashMap<String, Object>();  
  56.                         p.put("name""mobile");  
  57.                         p.put("rela""=");  
  58.                         p.put("value""'"+in+"'");  
  59.                         params.add(p);  
  60.                         try {  
  61.                             List<Godness> res = action.query(params);  
  62.                             for(Godness g:res){  
  63.                                 System.out.println(g.getId() + " , " + g.getUser_name()  
  64.                                         + " , " + g.getSex() + "," + g.getAge() + ","  
  65.                                         + g.getBirthday() + "," + g.getEmail() + ","  
  66.                                         + g.getMobile() + "," + g.getIsdel());  
  67.                             }  
  68.                             step = 1;  
  69.                             prenious = null;  
  70.                             params.clear();  
  71.                         } catch (SQLException e) {  
  72.                             e.printStackTrace();  
  73.                         }  
  74.                     }  
  75.                     if(OPERATION_SEARCH == prenious){  
  76.                         step ++;  
  77.                     }  
  78.             }else if (OPERATION_UPDATE.equals(in.toUpperCase())  
  79.                     || OPERATION_UPDATE.substring(01).equals(in.toUpperCase())  
  80.                     || OPERATION_UPDATE.equals(prenious)) {  
  81.                 prenious = OPERATION_UPDATE;  
  82.                 // 更新女神  
  83.                 if(1==step){  
  84.                     System.out.println("请输入要修改女神的[id]:");  
  85.                 }else if(2==step){  
  86.                     godness.setId(Integer.valueOf(in));  
  87.                     System.out.println("请输入女神[姓名]");  
  88.                 }else if(3 == step){  
  89.                     godness.setUser_name(in);  
  90.                     System.out.println("请输入女神[年龄]");  
  91.                 }else if(4 == step){  
  92.                     godness.setAge(Integer.valueOf(in));  
  93.                     System.out.println("请输入女神[生日],格式如:yyyy-MM-dd");  
  94.                 }else if(5 == step){  
  95.                     SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");  
  96.                     Date birthday = null;  
  97.                     try{  
  98.                         birthday = sf.parse(in);  
  99.                         godness.setBirthday(birthday);  
  100.                         System.out.println("请输入女神[邮箱]");  
  101.                     }catch(ParseException e){  
  102.                         e.printStackTrace();  
  103.                         System.out.println("你输入的格式有误,请重新输入");  
  104.                         step = 3;  
  105.                     }  
  106.                 }else if(6 == step){  
  107.                     godness.setEmail(in);  
  108.                     System.out.println("请输入女神的[手机号]");  
  109.                 }else if(7==step){  
  110.                     godness.setMobile(in);  
  111.                     try{  
  112.                         action.edit(godness);  
  113.                         System.out.println("更新女神成功");  
  114.                         step = 1;  
  115.                         prenious = null;  
  116.                     }catch(Exception e){  
  117.                         e.printStackTrace();  
  118.                         System.out.println("更新女神失败");  
  119.                     }  
  120.                 }  
  121.                   
  122.                 if(OPERATION_UPDATE.equals(prenious)){  
  123.                     step++;  
  124.                 }  
  125.             }else if (OPERATION_DELETE.equals(in.toUpperCase())  
  126.                     || OPERATION_DELETE.substring(01).equals(in.toUpperCase())  
  127.                     || OPERATION_DELETE.equals(prenious)) {  
  128.                     prenious = OPERATION_DELETE;  
  129.                     if(step == 1){  
  130.                         step ++;  
  131.                         System.out.println("删除女神信息,请输入ID:");  
  132.                     }else{  
  133.                         try {  
  134.                             action.del(Integer.valueOf(in));  
  135.                             System.out.println("删除成功!");  
  136.                         } catch (NumberFormatException e) {  
  137.                             e.printStackTrace();  
  138.                         } catch (SQLException e) {  
  139.                             e.printStackTrace();  
  140.                         }  
  141.                         step --;  
  142.                         prenious = null;  
  143.                     }  
  144.             }else if (OPERATION_GET.equals(in.toUpperCase())  
  145.                     || OPERATION_GET.substring(01).equals(in.toUpperCase())  
  146.                     || OPERATION_GET.equals(prenious)) {  
  147.                 prenious = OPERATION_GET;  
  148.                 if(step == 1){  
  149.                     step ++;  
  150.                     System.out.println("获取女神的详细信息,请输入ID:");  
  151.                 }else{  
  152.                     try {  
  153.                         Godness godness2 = action.get(Integer.valueOf(in));  
  154.                         System.out.println(godness2.getId() + " , " + godness2.getUser_name()  
  155.                                 + " , " + godness2.getSex() + "," + godness2.getAge() + ","  
  156.                                 + godness2.getBirthday() + "," + godness2.getEmail() + ","  
  157.                                 + godness2.getMobile() + "," + godness2.getIsdel());  
  158.                     } catch (NumberFormatException e) {  
  159.                         e.printStackTrace();  
  160.                     } catch (SQLException e) {  
  161.                         e.printStackTrace();  
  162.                     }  
  163.                     step --;  
  164.                     prenious = null;  
  165.                 }  
  166.             }else if (OPERATION_QUERY.equals(in.toUpperCase())  
  167.                     || OPERATION_QUERY.substring(01).equals(in.toUpperCase())) {  
  168.                 try {  
  169.                     List<Godness> list = action.query();  
  170.                     for(Godness go : list){  
  171.                         System.out.println(go.getId() +" --姓名:"+go.getUser_name());  
  172.                     }  
  173.                 } catch (SQLException e) {  
  174.                     // TODO Auto-generated catch block  
  175.                     e.printStackTrace();  
  176.                 }  
  177.             }else if (OPERATION_ADD.equals(in.toUpperCase())  
  178.                     || OPERATION_ADD.substring(01).equals(in.toUpperCase())  
  179.                     || OPERATION_ADD.equals(prenious)) {  
  180.                 prenious = OPERATION_ADD;  
  181.                 // 新增女神  
  182.                 if(1==step){  
  183.                     System.out.println("请输入女神[姓名]");  
  184.                       
  185.                 }else if(2 == step){  
  186.                     godness.setUser_name(in);  
  187.                     System.out.println("请输入女神[年龄]");  
  188.                 }else if(3 == step){  
  189.                     godness.setAge(Integer.valueOf(in));  
  190.                     System.out.println("请输入女神[生日],格式如:yyyy-MM-dd");  
  191.                 }else if(4 == step){  
  192.                     SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");  
  193.                     Date birthday = null;  
  194.                     try{  
  195.                         birthday = sf.parse(in);  
  196.                         godness.setBirthday(birthday);  
  197.                         System.out.println("请输入女神[邮箱]");  
  198.                     }catch(ParseException e){  
  199.                         e.printStackTrace();  
  200.                         System.out.println("你输入的格式有误,请重新输入");  
  201.                         step = 3;  
  202.                     }  
  203.                 }else if(5 == step){  
  204.                     godness.setEmail(in);  
  205.                     System.out.println("请输入女神的[手机号]");  
  206.                 }else if(6==step){  
  207.                     godness.setMobile(in);  
  208.                     try{  
  209.                         action.add(godness);  
  210.                         System.out.println("新增女神成功");  
  211.                         step = 1;  
  212.                         prenious = null;  
  213.                     }catch(Exception e){  
  214.                         e.printStackTrace();  
  215.                         System.out.println("新增女神失败");  
  216.                     }  
  217.                 }  
  218.                 if(OPERATION_ADD.equals(prenious)){  
  219.                     step++;  
  220.                 }  
  221.                   
  222.             } else {  
  223.                 System.out.println("你输入的值为:" + in);  
  224.             }  
  225.   
  226.         }  
  227.     }  
  228. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值