JDBC学习笔记(5):数据库的基本操作CRUD
2015-03-16 13:11
246 查看
1.查询
【运行结果】:
1 zhangs 1985-01-01 100.0
2 lisi 1986-01-01 200.0
3 wangwu 1987-01-01 300.0
2.增减一条记录,由于id是自增的,所以不需要重复插入数据:
【运行结果】:
i=1 //表示成功插入一条记录
【查询数据库结果】:多了一条记录
3.将id=1的money修改为400
【运行结果】:
i = 1
【查询数据库结果】:
4.删除id=4的记录
【运行结果】:
i=====1
【查询数据库结果】:
public static void read() throws SQLException { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery("select * from user"); while(rs.next()) { System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getDate("birthday") + "\t" + rs.getFloat("money")); } } finally { JdbcUtils.close(rs, stmt, conn); } }
【运行结果】:
1 zhangs 1985-01-01 100.0
2 lisi 1986-01-01 200.0
3 wangwu 1987-01-01 300.0
2.增减一条记录,由于id是自增的,所以不需要重复插入数据:
public static void create() throws SQLException { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); stmt = conn.createStatement(); String sql = "insert into user(name,birthday,money) values('zhaoliu','1990-01-01',400)"; int i = stmt.executeUpdate(sql); System.out.println("i="+i); } finally { JdbcUtils.close(rs, stmt, conn); } }
【运行结果】:
i=1 //表示成功插入一条记录
【查询数据库结果】:多了一条记录
3.将id=1的money修改为400
public static void update() throws SQLException { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); stmt = conn.createStatement(); String sql = "update user set money=400 where id=1"; int i = stmt.executeUpdate(sql); System.out.println("i = " + i); } finally { JdbcUtils.close(rs, stmt, conn); } }
【运行结果】:
i = 1
【查询数据库结果】:
4.删除id=4的记录
public static void delete() throws SQLException { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); stmt = conn.createStatement(); String sql = "delete from user where id=4"; int i = stmt.executeUpdate(sql); System.out.println("i=====" + i); } finally{ JdbcUtils.close(rs, stmt, conn); } }
【运行结果】:
i=====1
【查询数据库结果】:
相关文章推荐
- 使用JDBC操作数据库(CRUD基本操作)
- SQL数据库—<2>数据库基本操作(CRUD)
- 数据库基本操作
- ORACEL数据库基本操作
- Java数据库基本操作
- 对数据库中列的一些基本的操作的SQL命令
- java数据库操作基本流程
- java 数据库基本操作
- Asp.Net2.0数据库调用的基本操作
- ASP.NET对数据库的基本操作——插入,删除,修改(下)
- ASP.NET对数据库的基本操作——插入,删除,修改(上)
- 可重用数据库操作基本类
- 对数据库的几种基本操作
- C# on rails! 基本信息CRUD操作的简单实现
- 数据库中表的基本操作
- PHP中数据库基本操作
- 1、java数据库操作基本流程
- 数据库的基本操作
- java 数据库基本操作
- JAVA数据库基本操作