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)文章
mybatis-plus之如何根據(jù)數(shù)據(jù)庫主鍵定義字段類型
這篇文章主要介紹了mybatis-plus之如何根據(jù)數(shù)據(jù)庫主鍵定義字段類型問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
JDK常用命令jps jinfo jstat的具體說明與示例
JDK本身提供了很多方便的JVM性能調(diào)優(yōu)監(jiān)控工具,除了集成式的VisualVM和jConsole外,還有jps、jinfo、jstat等小巧的工具,本文章希望能起拋磚引玉之用,讓大家能開始對JVM性能調(diào)優(yōu)的常用工具有所了解2021-09-09
springboot集成redis實現(xiàn)簡單秒殺系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了springboot集成redis實現(xiàn)簡單秒殺系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12
SpringBoot2.1.3修改tomcat參數(shù)支持請求特殊符號問題
最近遇到一個問題,比如GET請求中,key,value中帶有特殊符號,請求會報錯。接下來通過本文給大家分享解決SpringBoot2.1.3修改tomcat參數(shù)支持請求特殊符號 ,需要的朋友可以參考下2019-05-05
SpringBoot項目訪問任意接口出現(xiàn)401錯誤的解決方案
今天小編就為大家分享一篇關(guān)于SpringBoot項目訪問任意接口出現(xiàn)401錯誤的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01

