初学JDBC__第三节(基本的CRUD(创建、读取、更新、删除) )
2012-12-22 18:54
513 查看
模板代码 :
Connection conn = null;
Statement st=null;
ResultSet rs = null;
try {
//获得Connection
//创建Statement
//处理查询结果ResultSet
} finally {
//释放资源ResultSet, Statement,Connection
}
创建:
增加对应SQL的INSERT,返回增加成功的行(记录)数
conn = getConnection();
Statement st = conn.createStatement();
String sql=“insert into user(name, age,regist_date )” + “values(‘name’, 10, now())”;
int i = st.executeUpdate(sql);
/ /i为插入的记录数
读取:
读取(查询)对应SQL的SELECT,返回查询结果
conn = getConnection();
st = conn.createStatement();
String sql = "select id, name, age,regist_date from user";
rs = st.executeQuery(sql);
while (rs.next()) {
System.out.print(rs.getInt("id") + " \t\t ");
System.out.print(rs.getString("name") + " \t\t ");
System.out.print(rs.getInt("age") + " \t\t ");
System.out.print(rs.getTimestamp("regist_date") + " \t\t ");
System.out.println();
} (ps:rs.get_*()括号内也可填写序列号,按照select中的顺序填写即可)
更新:
更新(修改)对应SQL的UPDATE,返回被修改的行(记录)数
conn = getConnection();
Statement st = conn.createStatement();
String sql=“update person set name='new name‘”;
int i = st.executeUpdate(sql);
//i为符合条件的记录数
删除:
删除对应SQL的DELETE,返回被删除的行(记录)数
conn = getConnection();
Statement st = conn.createStatement();
String sql=“delete from user where id=1”;
int i = st.executeUpdate(sql);
//i为删掉的记录数
CRUD总结
1、增、删、改用Statement.executeUpdate来完成,返回整数(匹配的记录数),这类操作相对简单。
2、查询用Statement.executeQuery来完成,返回的是ResultSet对象,ResultSet中包含了查询的结果;查询相对与增、删、改要复杂一些,因为有查询结果要处理。
完整的小例子:
Connection conn = null;
Statement st=null;
ResultSet rs = null;
try {
//获得Connection
//创建Statement
//处理查询结果ResultSet
} finally {
//释放资源ResultSet, Statement,Connection
}
创建:
增加对应SQL的INSERT,返回增加成功的行(记录)数
conn = getConnection();
Statement st = conn.createStatement();
String sql=“insert into user(name, age,regist_date )” + “values(‘name’, 10, now())”;
int i = st.executeUpdate(sql);
/ /i为插入的记录数
读取:
读取(查询)对应SQL的SELECT,返回查询结果
conn = getConnection();
st = conn.createStatement();
String sql = "select id, name, age,regist_date from user";
rs = st.executeQuery(sql);
while (rs.next()) {
System.out.print(rs.getInt("id") + " \t\t ");
System.out.print(rs.getString("name") + " \t\t ");
System.out.print(rs.getInt("age") + " \t\t ");
System.out.print(rs.getTimestamp("regist_date") + " \t\t ");
System.out.println();
} (ps:rs.get_*()括号内也可填写序列号,按照select中的顺序填写即可)
更新:
更新(修改)对应SQL的UPDATE,返回被修改的行(记录)数
conn = getConnection();
Statement st = conn.createStatement();
String sql=“update person set name='new name‘”;
int i = st.executeUpdate(sql);
//i为符合条件的记录数
删除:
删除对应SQL的DELETE,返回被删除的行(记录)数
conn = getConnection();
Statement st = conn.createStatement();
String sql=“delete from user where id=1”;
int i = st.executeUpdate(sql);
//i为删掉的记录数
CRUD总结
1、增、删、改用Statement.executeUpdate来完成,返回整数(匹配的记录数),这类操作相对简单。
2、查询用Statement.executeQuery来完成,返回的是ResultSet对象,ResultSet中包含了查询的结果;查询相对与增、删、改要复杂一些,因为有查询结果要处理。
完整的小例子:
import java.sql.*; public class CRUD { public static void main(String[] args) throws SQLException { // TODO Auto-generated method stub System.out.println(delate()); //read(); } static int create() throws SQLException{ Connection conn=null; Statement st=null; ResultSet rs=null; int i; try{ conn=jdbcUtils.getConnection(); st=conn.createStatement(); String sql="insert into T_Users(name,birthday,money) values('qq','20121225','200')"; i=st.executeUpdate(sql); } finally{ jdbcUtils.free(conn, st, rs); } return i; } static void read() throws SQLException{ Connection conn=null; Statement st=null; ResultSet rs=null; try{ conn=jdbcUtils.getConnection(); st=conn.createStatement(); rs=st.executeQuery("select id ,name,birthday,money from T_Users where name='www'"); while(rs.next()){ System.out.println(rs.getObject("name")+" "+rs.getObject("money")); } } finally{ jdbcUtils.free(conn, st, rs); } } static int update() throws SQLException{ Connection conn=null; Statement st=null; ResultSet rs=null; int i; try{ conn=jdbcUtils.getConnection(); st=conn.createStatement(); String sql="update T_Users set name='www' where name='qq'"; i=st.executeUpdate(sql); } finally{ jdbcUtils.free(conn, st, rs); } return i; } static int delate() throws SQLException{ Connection conn=null; Statement st=null; ResultSet rs=null; int i; try{ conn=jdbcUtils.getConnection(); st=conn.createStatement(); String sql="delete from T_Users where id<5"; i=st.executeUpdate(sql); } finally{ jdbcUtils.free(conn, st, rs); } return i; } }
相关文章推荐
- [转载]MongoDB学习 (四):创建、读取、更新、删除(CRUD)快速入门
- MongoDB学习 (四):创建、读取、更新、删除(CRUD)快速入门
- MongoDB学习 (四):创建、读取、更新、删除(CRUD)快速入门
- MongoDB学习 (四):创建、读取、更新、删除(CRUD)快速入门
- NoSQL学习之路(四):创建、读取、更新、删除(CRUD)
- MongoDB学习 (四):创建、读取、更新、删除(CRUD)快速入门
- MongoDB学习 (四):创建、读取、更新、删除(CRUD)快速入门
- 【MongoDB学习笔记5】MongoDB中的创建、读取、更新、删除(CRUD)
- MySQL基本操作:数据库的创建、查询、更新、插入、删除表格
- 基本的文件 I/O 用法:1.复制目录及其文件 2.文件创建,写入,读取,删除,复制
- JDBC 数据库连接 创建表格、插入、查询、删除、修改数据 基本操作
- 使用 ADO.NET 来创建、读取、更新和删除数据
- Zookeeper之Curator(1)客户端基本的创建,删除,更新,查找操作api
- Unity3d XML的创建、更新、添加、删除和读取
- CRUD:增加(Create)、读取查询(Retrieve)、更新(Update)和删除(Delete)
- lucene5学习 - 索引基本操作(创建,查询,更新,删除,分页)
- ASP.NET MVC 4 中 Controller 与 ApiController 做读取、新增、更新、删除 ( CRUD )
- python对MySQL数据库的操作表单的创建,数据的插入,读取,更新,删除操作
- 写给初学数据结构的同学之(循环双链表基本操作,创建,插入,删除,排序)