JDBC程序更新數(shù)據(jù)庫中記錄的方法
本文實(shí)例講述了JDBC程序更新數(shù)據(jù)庫中記錄的方法。分享給大家供大家參考,具體如下:
使用JDBC程序(Eclipse、MyEclipse)更新數(shù)據(jù)庫(MySql)中的記錄時(shí)可以只修改記錄的一個(gè)字段或幾個(gè)字段,具體方法為可以加入如下被注釋代碼(前提是修改之前可以從數(shù)據(jù)庫中得到該條記錄)以u(píng)ser表為例
public class UserDaoJdbcImpl implements UserDao { public void update(User u) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); String sql = "update user set name = ?, birthday = ?, money = ? where id=?"; ps = conn.prepareStatement(sql); // 首先得到該記錄 User user = getUserById(u.getId()); // 判斷字段是否需要修改 if (u.getName() == null) { u.setName(user.getName()); } if (u.getBirthday() == null) { u.setBirthday(user.getBirthday()); } if (u.getMoney() == 0) { u.setMoney(user.getMoney()); } ps.setString(1, u.getName()); ps.setDate(2, new java.sql.Date(u.getBirthday().getTime())); ps.setDouble(3, u.getMoney()); ps.setInt(4, u.getId()); int i = ps.executeUpdate(); System.out.println("成功向user表中更新" + i + "條記錄"); } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtils.free(rs, ps, conn); } } public User getUserById(int id) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; User user = null; try { conn = JdbcUtils.getConnection(); String sql = "select * from user where id = ?"; ps = conn.prepareStatement(sql); ps.setInt(1, id); rs = ps.executeQuery(); if (rs.next()) { user = new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setBirthday(rs.getDate("birthday")); user.setMoney(rs.getDouble("money")); } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcUtils.free(rs, ps, conn); } return user; } }
調(diào)用:
public static void main(String[] args) { UserDao ud = new UserDaoJdbcImpl(); User user = new User(); user.setId(9); user.setName("老師");//只修改name和birthday屬性 Date d = null; try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); d = sdf.parse("1999-9-14"); } catch (ParseException e) { e.printStackTrace(); } user.setBirthday(d); //user.setMoney(1234);不修改money屬性 ud.update(user); }
希望本文所述對(duì)大家Java程序設(shè)計(jì)有所幫助。
- 使用JDBC從數(shù)據(jù)庫中查詢數(shù)據(jù)的方法
- java jdbc連接mysql數(shù)據(jù)庫實(shí)現(xiàn)增刪改查操作
- jdbc鏈接遠(yuǎn)程數(shù)據(jù)庫進(jìn)行修改url操作
- java實(shí)現(xiàn)jdbc批量插入數(shù)據(jù)
- JDBC鏈接mysql插入數(shù)據(jù)后顯示問號(hào)的原因及解決辦法
- JSP使用JDBC連接MYSQL數(shù)據(jù)庫的方法
- Java編程中使用JDBC API連接數(shù)據(jù)庫和創(chuàng)建程序的方法
- 在Java的Spring框架的程序中使用JDBC API操作數(shù)據(jù)庫
- Java中使用JDBC操作數(shù)據(jù)庫簡單實(shí)例
- JSP中使用JDBC訪問SQL Server 2008數(shù)據(jù)庫示例
- Java使用JDBC連接數(shù)據(jù)庫的實(shí)現(xiàn)方法
- java 使用JDBC構(gòu)建簡單的數(shù)據(jù)訪問層實(shí)例詳解
相關(guān)文章
mybatis-plus之如何根據(jù)數(shù)據(jù)庫主鍵定義字段類型
這篇文章主要介紹了mybatis-plus之如何根據(jù)數(shù)據(jù)庫主鍵定義字段類型問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07JDK常用命令jps jinfo jstat的具體說明與示例
JDK本身提供了很多方便的JVM性能調(diào)優(yōu)監(jiān)控工具,除了集成式的VisualVM和jConsole外,還有jps、jinfo、jstat等小巧的工具,本文章希望能起拋磚引玉之用,讓大家能開始對(duì)JVM性能調(diào)優(yōu)的常用工具有所了解2021-09-09springboot集成redis實(shí)現(xiàn)簡單秒殺系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了springboot集成redis實(shí)現(xiàn)簡單秒殺系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12SpringBoot2.1.3修改tomcat參數(shù)支持請(qǐng)求特殊符號(hào)問題
最近遇到一個(gè)問題,比如GET請(qǐng)求中,key,value中帶有特殊符號(hào),請(qǐng)求會(huì)報(bào)錯(cuò)。接下來通過本文給大家分享解決SpringBoot2.1.3修改tomcat參數(shù)支持請(qǐng)求特殊符號(hào) ,需要的朋友可以參考下2019-05-05Java使用DFA算法實(shí)現(xiàn)敏感詞過濾的示例代碼
很多項(xiàng)目中都會(huì)有一個(gè)敏感詞管理模塊,本文主要介紹了Java使用DFA算法實(shí)現(xiàn)敏感詞過濾的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03SpringBoot項(xiàng)目訪問任意接口出現(xiàn)401錯(cuò)誤的解決方案
今天小編就為大家分享一篇關(guān)于SpringBoot項(xiàng)目訪問任意接口出現(xiàn)401錯(cuò)誤的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01