欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java如何更改數(shù)據(jù)庫(kù)中的數(shù)據(jù)

 更新時(shí)間:2021年11月08日 09:53:25   作者:別先生  
這篇文章主要介紹了java如何更改數(shù)據(jù)庫(kù)中的數(shù)據(jù),修改數(shù)據(jù)庫(kù)是數(shù)據(jù)庫(kù)操作必不可少的一部分,使用Statement接口中的excuteUpdate()方法可以修改數(shù)據(jù)表中的數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧

java更改數(shù)據(jù)庫(kù)中的數(shù)據(jù)

不廢話,上代碼

package com.ningmeng;

import java.sql.*;

/**
 * 1:更改數(shù)據(jù)庫(kù)中的數(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ù)庫(kù)驅(qū)動(dòng)
            System.out.println("加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)成功");
            String url="jdbc:mysql://localhost:3306/test";//聲明數(shù)據(jù)庫(kù)test的url
            String user="root";//數(shù)據(jù)庫(kù)賬號(hào)
            String password="123456";//數(shù)據(jù)庫(kù)密碼
            //建立數(shù)據(jù)庫(kù)連接,獲得連接對(duì)象conn
            Connection conn=DriverManager.getConnection(url, user, password);
            System.out.println("連接數(shù)據(jù)庫(kù)成功");
            String sql="update users set age=20 where id=1 ";//生成一條mysql語(yǔ)句
            Statement stmt=conn.createStatement();//創(chuàng)建一個(gè)Statement對(duì)象
            stmt.executeUpdate(sql);//執(zhí)行SQL語(yǔ)句
            System.out.println("修改數(shù)據(jù)庫(kù)成功");
            conn.close();
            System.out.println("關(guān)閉數(shù)據(jù)庫(kù)成功");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

結(jié)果如下

上圖對(duì)比代表修改成功,ok.

注意:

修改數(shù)據(jù)庫(kù)是數(shù)據(jù)庫(kù)操作必不可少的一部分,使用Statement接口中的excuteUpdate()方法可以修改數(shù)據(jù)表中的數(shù)據(jù),也可以使用PreparedStatement接口中的excuteUpdate方法對(duì)數(shù)據(jù)庫(kù)中的表進(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ù)庫(kù)驅(qū)動(dòng)
            System.out.println("加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)成功");
            String url="jdbc:mysql://localhost:3306/test";//聲明數(shù)據(jù)庫(kù)test的url
            String user="root";//數(shù)據(jù)庫(kù)賬號(hào)
            String password="123456";//數(shù)據(jù)庫(kù)密碼
            //建立數(shù)據(jù)庫(kù)連接,獲得連接對(duì)象conn
            Connection conn=DriverManager.getConnection(url, user, password);
            System.out.println("連接數(shù)據(jù)庫(kù)成功");

            String sql="update users set password=? where sex=? ";//生成一條mysql語(yǔ)句
            PreparedStatement ps=conn.prepareStatement(sql);//創(chuàng)建PreparedStatement對(duì)象
            ps.setString(1, "admin");//為第一個(gè)問(wèn)號(hào)賦值
            ps.setInt(2, 0);//為第二個(gè)問(wèn)號(hào)賦值
            int count=ps.executeUpdate();//執(zhí)行sql語(yǔ)句
            System.out.println("修改數(shù)據(jù)庫(kù)成功");
            conn.close();
            System.out.println("關(guān)閉數(shù)據(jù)庫(kù)成功");

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

上圖對(duì)比,可知已經(jīng)修改完畢

如上所示修改數(shù)據(jù)是根據(jù)一定的條件進(jìn)行修改,這個(gè)條件可以是固定的,也可以是一個(gè)范圍,分別是第一個(gè),第二個(gè)案例。

第二個(gè)案例使用PreparedStatement接口中的executeUpdate()方法修改數(shù)據(jù)庫(kù)users表中的數(shù)據(jù)。(將所有性別為0的用戶(hù)密碼改為admin,需要注意的是,我得數(shù)據(jù)表創(chuàng)建的時(shí)候性別是int類(lèi)型的,只有0,1,2三種進(jìn)行代表,所以參考案例的需要注意一下代碼的修改)

到此這篇關(guān)于java如何更改數(shù)據(jù)庫(kù)中的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)java更改數(shù)據(jù)庫(kù)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論