JDBC程序更新數(shù)據(jù)庫中記錄的方法
本文實例講述了JDBC程序更新數(shù)據(jù)庫中記錄的方法。分享給大家供大家參考,具體如下:
使用JDBC程序(Eclipse、MyEclipse)更新數(shù)據(jù)庫(MySql)中的記錄時可以只修改記錄的一個字段或幾個字段,具體方法為可以加入如下被注釋代碼(前提是修改之前可以從數(shù)據(jù)庫中得到該條記錄)以user表為例
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);
}
希望本文所述對大家Java程序設(shè)計有所幫助。
- 使用JDBC從數(shù)據(jù)庫中查詢數(shù)據(jù)的方法
- java jdbc連接mysql數(shù)據(jù)庫實現(xiàn)增刪改查操作
- jdbc鏈接遠(yuǎn)程數(shù)據(jù)庫進(jìn)行修改url操作
- java實現(xiàn)jdbc批量插入數(shù)據(jù)
- JDBC鏈接mysql插入數(shù)據(jù)后顯示問號的原因及解決辦法
- JSP使用JDBC連接MYSQL數(shù)據(jù)庫的方法
- Java編程中使用JDBC API連接數(shù)據(jù)庫和創(chuàng)建程序的方法
- 在Java的Spring框架的程序中使用JDBC API操作數(shù)據(jù)庫
- Java中使用JDBC操作數(shù)據(jù)庫簡單實例
- JSP中使用JDBC訪問SQL Server 2008數(shù)據(jù)庫示例
- Java使用JDBC連接數(shù)據(jù)庫的實現(xiàn)方法
- java 使用JDBC構(gòu)建簡單的數(shù)據(jù)訪問層實例詳解
相關(guān)文章
SpringBoot跨域Access-Control-Allow-Origin實現(xiàn)解析
這篇文章主要介紹了SpringBoot跨域Access-Control-Allow-Origin實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
Java編程Iterator迭代器設(shè)計原理及實現(xiàn)代碼示例
這篇文章主要介紹了Java編程Iterator迭代器設(shè)計原理及實現(xiàn)代碼示例,具有一定參考價值,需要的朋友可以了解下。2017-10-10
idea2020.1.3 手把手教你創(chuàng)建web項目的方法步驟
這篇文章主要介紹了idea 2020.1.3 手把手教你創(chuàng)建web項目的方法步驟,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
自制Java工具實現(xiàn)翻譯鼠標(biāo)選中文本
這篇文章主要為大家詳細(xì)介紹了如何自制Java工具實現(xiàn)ctrl+c+c翻譯鼠標(biāo)選中文本,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-01-01
IntelliJ IDEA 常用設(shè)置(配置)吐血整理(首次安裝必需)
這篇文章主要介紹了IntelliJ IDEA 常用設(shè)置(配置)吐血整理(首次安裝必需),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
java字符串?dāng)?shù)字補齊位數(shù)詳解
這篇文章主要介紹了java字符串?dāng)?shù)字補齊位數(shù),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03

