java如何更改數(shù)據(jù)庫中的數(shù)據(jù)
java更改數(shù)據(jù)庫中的數(shù)據(jù)
不廢話,上代碼
package com.ningmeng; import java.sql.*; /** * 1:更改數(shù)據(jù)庫中的數(shù)據(jù) * @author biexiansheng * */ public class Test04 { public static void main(String[] args) { // TODO Auto-generated method stub try { Class.forName("com.mysql.jdbc.Driver");//加載數(shù)據(jù)庫驅(qū)動 System.out.println("加載數(shù)據(jù)庫驅(qū)動成功"); String url="jdbc:mysql://localhost:3306/test";//聲明數(shù)據(jù)庫test的url String user="root";//數(shù)據(jù)庫賬號 String password="123456";//數(shù)據(jù)庫密碼 //建立數(shù)據(jù)庫連接,獲得連接對象conn Connection conn=DriverManager.getConnection(url, user, password); System.out.println("連接數(shù)據(jù)庫成功"); String sql="update users set age=20 where id=1 ";//生成一條mysql語句 Statement stmt=conn.createStatement();//創(chuàng)建一個Statement對象 stmt.executeUpdate(sql);//執(zhí)行SQL語句 System.out.println("修改數(shù)據(jù)庫成功"); conn.close(); System.out.println("關(guān)閉數(shù)據(jù)庫成功"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
結(jié)果如下
上圖對比代表修改成功,ok.
注意:
修改數(shù)據(jù)庫是數(shù)據(jù)庫操作必不可少的一部分,使用Statement接口中的excuteUpdate()方法可以修改數(shù)據(jù)表中的數(shù)據(jù),也可以使用PreparedStatement接口中的excuteUpdate方法對數(shù)據(jù)庫中的表進(jìn)行修改操作。
package com.ningmeng; import java.sql.*; /** * @author biexiansheng * */ public class Test05 { public static void main(String[] args) { // TODO Auto-generated method stub try { Class.forName("com.mysql.jdbc.Driver");//加載數(shù)據(jù)庫驅(qū)動 System.out.println("加載數(shù)據(jù)庫驅(qū)動成功"); String url="jdbc:mysql://localhost:3306/test";//聲明數(shù)據(jù)庫test的url String user="root";//數(shù)據(jù)庫賬號 String password="123456";//數(shù)據(jù)庫密碼 //建立數(shù)據(jù)庫連接,獲得連接對象conn Connection conn=DriverManager.getConnection(url, user, password); System.out.println("連接數(shù)據(jù)庫成功"); String sql="update users set password=? where sex=? ";//生成一條mysql語句 PreparedStatement ps=conn.prepareStatement(sql);//創(chuàng)建PreparedStatement對象 ps.setString(1, "admin");//為第一個問號賦值 ps.setInt(2, 0);//為第二個問號賦值 int count=ps.executeUpdate();//執(zhí)行sql語句 System.out.println("修改數(shù)據(jù)庫成功"); conn.close(); System.out.println("關(guān)閉數(shù)據(jù)庫成功"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
上圖對比,可知已經(jīng)修改完畢
如上所示修改數(shù)據(jù)是根據(jù)一定的條件進(jìn)行修改,這個條件可以是固定的,也可以是一個范圍,分別是第一個,第二個案例。
第二個案例使用PreparedStatement接口中的executeUpdate()方法修改數(shù)據(jù)庫users表中的數(shù)據(jù)。(將所有性別為0的用戶密碼改為admin,需要注意的是,我得數(shù)據(jù)表創(chuàng)建的時候性別是int類型的,只有0,1,2三種進(jìn)行代表,所以參考案例的需要注意一下代碼的修改)
到此這篇關(guān)于java如何更改數(shù)據(jù)庫中的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)java更改數(shù)據(jù)庫數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java動態(tài)數(shù)組Arraylist存放自定義數(shù)據(jù)類型方式
這篇文章主要介紹了Java動態(tài)數(shù)組Arraylist存放自定義數(shù)據(jù)類型方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10Java注解如何基于Redission實現(xiàn)分布式鎖
這篇文章主要介紹了Java注解如何基于Redission實現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01Java9新特性對HTTP2協(xié)議支持與非阻塞HTTP?API
這篇文章主要為大家介紹了Java9新特性對HTTP2協(xié)議的支持與非阻塞HTTP?API,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03Java實戰(zhàn)之兼職平臺系統(tǒng)的實現(xiàn)
這篇文章主要介紹了如何利用Java編寫一個兼職平臺系統(tǒng),采用到的技術(shù)有Springboot、SpringMVC、MyBatis、ThymeLeaf等,感興趣的小伙伴可以了解一下2022-03-03