java 修改数据_Java 数据库添加,修改和删除

90adf2a157b04df3b95fc609012ee615.jpg

都是用executeUpdate方法

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import com.mysql.jdbc.Statement;

public class JDbc {

public static void main(String[] args) {

Connection connection = null;

Statement statement = null;

try {

Class.forName("com.mysql.jdbc.Driver");//加载Driver类

connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test02","root","root");//链接数据库

statement = (Statement) connection.createStatement();//新建Statement接口

String sql1 = "insert into tb_peo(id, name, birthday) values('5','郭珂','1999-8-10')";

//insert into 表名(表中列名1,列名2,...) values('',''....)

int a = statement.executeUpdate(sql1);

System.out.println("表中有"+a+"行被修改");

String sql2 = "update tb_peo set birthday = '1999-8-11'where id = 5";

//update 表名 set 列名 = '修改值' where = ;

int b = statement.executeUpdate(sql2);

System.out.println("表中有"+b+"行被修改");

String sql3 = "delete from tb_peo where id = 5";

//delete from 表名 where = ;

int c = statement.executeUpdate(sql3);

System.out.println("表中有"+c+"行被修改");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

statement.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

connection.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值