前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 数据库添加,修改和删除

Java 数据库添加,修改和删除

作者头像
用户2965768
发布2019-03-11 11:37:33
1.8K0
发布2019-03-11 11:37:33
举报
文章被收录于专栏:wymwym

都是用executeUpdate方法

代码语言:javascript
复制
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();
			}
			
		}
		
		}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年02月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档