JAVA访问数据库之增删改查

来源:互联网 发布:imf的数据 编辑:程序博客网 时间:2024/04/29 03:39

查询

查询在数据库的操作中是很重要的,我们把数据保存在数据库中,就是为了我们在需要的时候能够快速、高效的查询出来。

/**    * 查询    *     * @throws Exception    */   public void query() throws Exception   {      Connection conn = getConnection();      Statement statement = conn.createStatement();      ResultSet resultSet = statement            .executeQuery("SELECT USER_ID, USER_NAME FROM USER_INFO");      System.out.printf("%20s %50s\n", "USER_ID", "USER_NAME");      // 遍历结果集      while (resultSet.next())      {         System.out.printf("%20s %50s\n", resultSet.getString("USER_ID"),               resultSet.getString("USER_NAME"));      }      resultSet.close();      statement.close();      conn.close();   }   /**    * 通过用户编号查询    *     * @param userId    * @throws Exception    */   public void queryById(String userId) throws Exception   {      Connection conn = getConnection();      // 动态参数使用PreparedStatement,使用Statement存在SQL注入风险      PreparedStatement statement = conn            .prepareStatement("SELECT USER_ID, USER_NAME FROM USER_INFO WHERE USER_ID = ?");      statement.setString(1, userId);      ResultSet resultSet = statement.executeQuery();      System.out.printf("%20s %50s\n", "USER_ID", "USER_NAME");      // 遍历结果集      while (resultSet.next())      {         System.out.printf("%20s %50s\n", resultSet.getString("USER_ID"),               resultSet.getString("USER_NAME"));      }      resultSet.close();      statement.close();      conn.close();   }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

修改

在这里,我讲增删改作为一类,同属于对数据库的修改操作,在例子中,也会发现,我们使用的方法完全相同,仅仅是SQL的区别。

/**    * 修改    *     * @param userId    * @param userName    * @throws Exception    */   public void update(String userId, String userName) throws Exception   {      Connection conn = getConnection();      // 动态参数使用PreparedStatement,使用Statement存在SQL注入风险      PreparedStatement statement = conn            .prepareStatement("UPDATE USER_INFO SET USER_NAME = ? WHERE USER_ID = ?");      statement.setString(1, userName);      statement.setString(2, userId);      System.out.println("受影响的记录条数为:" + statement.executeUpdate());      statement.close();      conn.close();   }   /**    * 清空    */   public void clear() throws Exception   {      Connection conn = getConnection();      Statement statement = conn.createStatement();      System.out.println("受影响的记录条数为:"            + statement.executeUpdate("DELETE USER_INFO"));      statement.close();      conn.close();   }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

完整示例

import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;public class JDBCCRUDemo{   /**    * 获得数据库连接    *     * @return    * @throws Exception    */   public Connection getConnection() throws Exception   {      Class.forName("org.h2.Driver");      return DriverManager.getConnection("jdbc:h2:h2.db", "test", "123");   }   /**    * 查询    *     * @throws Exception    */   public void query() throws Exception   {      Connection conn = getConnection();      Statement statement = conn.createStatement();      ResultSet resultSet = statement            .executeQuery("SELECT USER_ID, USER_NAME FROM USER_INFO");      System.out.printf("%20s %50s\n", "USER_ID", "USER_NAME");      // 遍历结果集      while (resultSet.next())      {         System.out.printf("%20s %50s\n", resultSet.getString("USER_ID"),               resultSet.getString("USER_NAME"));      }      resultSet.close();      statement.close();      conn.close();   }   /**    * 通过用户编号查询    *     * @param userId    * @throws Exception    */   public void queryById(String userId) throws Exception   {      Connection conn = getConnection();      // 动态参数使用PreparedStatement,使用Statement存在SQL注入风险      PreparedStatement statement = conn            .prepareStatement("SELECT USER_ID, USER_NAME FROM USER_INFO WHERE USER_ID = ?");      statement.setString(1, userId);      ResultSet resultSet = statement.executeQuery();      System.out.printf("%20s %50s\n", "USER_ID", "USER_NAME");      // 遍历结果集      while (resultSet.next())      {         System.out.printf("%20s %50s\n", resultSet.getString("USER_ID"),               resultSet.getString("USER_NAME"));      }      resultSet.close();      statement.close();      conn.close();   }   /**    * 添加    *     * @param userId    * @param userName    * @throws Exception    */   public void insert(String userId, String userName) throws Exception   {      Connection conn = getConnection();      // 动态参数使用PreparedStatement,使用Statement存在SQL注入风险      PreparedStatement statement = conn            .prepareStatement("INSERT INTO USER_INFO(USER_ID, USER_NAME) VALUES(?, ?)");      statement.setString(1, userId);      statement.setString(2, userName);      System.out.println("受影响的记录条数为:" + statement.executeUpdate());      statement.close();      conn.close();   }   /**    * 修改    *     * @param userId    * @param userName    * @throws Exception    */   public void update(String userId, String userName) throws Exception   {      Connection conn = getConnection();      // 动态参数使用PreparedStatement,使用Statement存在SQL注入风险      PreparedStatement statement = conn            .prepareStatement("UPDATE USER_INFO SET USER_NAME = ? WHERE USER_ID = ?");      statement.setString(1, userName);      statement.setString(2, userId);      System.out.println("受影响的记录条数为:" + statement.executeUpdate());      statement.close();      conn.close();   }   /**    * 清空    */   public void clear() throws Exception   {      Connection conn = getConnection();      Statement statement = conn.createStatement();      System.out.println("受影响的记录条数为:"            + statement.executeUpdate("DELETE USER_INFO"));      statement.close();      conn.close();   }   public static void main(String[] args) throws Exception   {      JDBCCRUDemo demo = new JDBCCRUDemo();      demo.insert("id111", "name111");      demo.insert("id222", "name222");      demo.insert("id333", "name333");      demo.insert("id444", "name444");      demo.insert("id555", "name555");      demo.query();      demo.queryById("id444");      demo.update("id222", "name1234");      demo.query();      demo.clear();   }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138

运行结果: 
受影响的记录条数为:1 
受影响的记录条数为:1 
受影响的记录条数为:1 
受影响的记录条数为:1 
受影响的记录条数为:1 
             USER_ID                                          USER_NAME 
               id111                                            name111 
               id222                                            name222 
               id333                                            name333 
               id444                                            name444 
               id555                                            name555 
             USER_ID                                          USER_NAME 
               id444                                            name444 
受影响的记录条数为:1 
             USER_ID                                          USER_NAME 
               id111                                            name111 
               id333                                            name333 
               id444                                            name444 
               id555                                            name555 
               id222                                           name1234 
受影响的记录条数为:5

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 河南许昌小学生生病办休学怎么办 1岁宝宝内向胆小怎么办 3岁宝宝内向胆小怎么办 数学物理好不喜欢学医怎么办 孩子眉毛太浓了怎么办 小娃一年级成绩太差怎么办 孩子晚上睡得晚怎么办 在深圳买房难小孩读书怎么办 上海小孩读书积分不够怎么办 读书时静不下心怎么办 初中生静不下心读书怎么办 孩子学习静不下心怎么办 退烧后体温35度怎么办 孩子体温34度多怎么办 宝宝感冒咳嗽流鼻涕出汗怎么办 养的小鸡总打架怎么办 小鸡一条腿瘸了怎么办 同窝小斗鸡打架怎么办 夏季羊长的慢怎么办 1岁吃母乳不吃饭怎么办 5个月宝宝黏妈妈怎么办 九个月宝宝不爱吃饭怎么办 20个月宝宝吐了怎么办 十个月的宝宝消化不好怎么办 两岁宝宝不爱喝水怎么办 两岁宝宝不爱喝水吃饭怎么办 两岁的宝宝不爱喝水怎么办 宝宝不爱吃饭不爱喝水怎么办 一多半宝宝爱喝水 不爱吃饭怎么办 1岁宝宝不爱吃饭喝水怎么办 两岁小宝宝不爱吃饭怎么办 4个月小宝宝咳嗽怎么办 3个月小宝宝咳嗽怎么办 2个月小宝宝咳嗽怎么办 8的岁儿童腿不直怎么办 作业盒子选错年级怎么办 两岁x型腿怎么办 绿萝叶子有水滴怎么办 打游戏变菜了怎么办 车被记号笔画了怎么办 水溶性彩铅受潮了怎么办