利用Statement接口实现数据表的更新和查询操作
-取得Statement接口对象:Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
一个Connection 可以打开多个Statement
-数据更新操作:int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException
返回int更新行数
-数据查询操作:ResultSet executeQuery(String sql) throws SQLException
返回ResultSet
编写数据库创建脚本,创建一个表格脚本
DROP TABLE member PURGE ;
DROP SEQUENCE myseq;
CREATE SEQUENCE myseq ;
CREATE TABLE member (
mid NUMBER,
name VARCHAR2(20),
birthday DATE DEFAULT (SYSDATE) ,
age NUMBER(3),
note CLOB,
CONSTRAINT pk_mid PRIMARY KEY(mid)
);
以上有序列,mid通过序列生产
数据更新操作:
每次更新之后都会返回影响的数据行数
数据增加的Oracle命令
INSERTINTO 表名称 (列,列,、、) VALUSES (值,值。。。) ;
package cn; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement; public class Test { private static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:mldn";
private static final String USER = "scott" ;
private static final String PASSWORLD = "tiger" ;
public static void main(String[] args) throws Exception{
//第一步 加载数据库驱动程序,此时不需要实例化,会有容器自己负责管理
Class.forName(DBDRIVER) ;
//第二步 连接数据库 每一个Connection都代表一个数据库连接
Connection conn = DriverManager.getConnection(DBURL,USER,PASSWORLD) ;
//第三步,进行数据库的数据操作
Statement stmt = conn.createStatement();
String sql = "INSERT INTO member (mid,name,birthday,age,note) VALUES(myseq.nextval,'张三',TO_DATE('1999-10-10','yyyy-mm-dd'),17,'人')";
int len = stmt.executeUpdate(sql); System.out.println(conn) ;
System.out.println(len); //关闭连接
stmt.close();
conn.close();
} }
这个时候就会更新了数据库中的列表信息
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlwAAABnCAIAAABaXAugAAAHf0lEQVR4nO3d347cthUHYL5pGrRAmzZoiyZpavjl5lWaIheBESAFYsDozkUvllmMRfLoSPNPmvm+qxHnDEWREn8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+TFkxiF2WmkZeZkRT/dtFuRZKN6iXhBwWSum2ehyD09fCiu3v6EYpBVo4JL9RN0KxS35rKT3H7ESnFTjx2KK3bw089OXpQnez5HYRYUXKqfi4SiRLwNochDeYZQPLXos5MX3cOHFMzbovlc10/cbXL+RyNccScwEj9fK+Z5VG+9uJ3HDsVzTEJxsqG/vci76ej37CKhyO5YdzZBKE50I7D7heNmQ3oqwcS2S8PDyCyrpecWhGKr/VOg+4zj+cxOtbV4SPlldQNwdUIRACqhCACVUASASigCQCUUAaASigBQCUUAePV/l26l4Z1FPqYAAAAASUVORK5CYII=" alt="" />
范例:数据修改
命令格式 UPDATE 表名称 SET 字段=值,WHERE 更新条件
String sql = "UPDATE member SET name='李四' ,birthday=SYSDATE ,age=30 WHERE mid IN(2,3)";
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdoAAACACAIAAAAj5D/dAAAHr0lEQVR4nO3c24Krtg4AUP7s/Dp/ts9D2ynFlrDNZbis9UTAEY4sq0n2pNMEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAL/lT+C35wXwMdoxwC1oxwC3oB3DY8zzPM9z9eG8sBpQnh+IXz5MBsw1ja/xDpLJt+S5uijl+fL4f/+1asd5/h+X5MeJMtyy7vnSVMupjF+94577sssyxdEiVZ8SXY0Glw+rC5zMpxzZ9hLvIn8tSZ6n/+a8fJgE//Pnz08jXnbkKc1/MiuOUs3/FC9BV/23LOvq5FSrq977sstcs7pafUoyoBp/NT6vuep8Nu91c8lr6c1zOT4avOrCP8d5/pNZcYjGnA8vR7R38kLarEPONdesrlafkgyI4k/HteMn1keSt948l+OjwdrxPQ3kvKv+V9unPKjerqUO8/uyy3Kdpt1tIoq/kgRJ5lMGeZZk/kmeG8dXkzz1t+Op2MmcYbP+o6c0Lk1ZD/O+dqwkrjC8PMmA6uBqWZTLnExgddz8Eu+i8WWWJ8ur+Znlcd6Oy/wn8+FALfWfX2qJPwX7rhxWfThwX3bZvzz5CkXllZfg2L1ur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45l/fN58+x8tbcm/bZ356/W0tNNLaMVbVVRz66Hf8laWdRv+uKNp38ZYV2fKVqOx6ok58I02M3DhvGCqJUHbY6KB8+tKrydjwFu66aq83sdV1ahY3uG01jrEEQSTK/qpP2gNVj3uCQvRdFiNrxtOOtwU3k7aw6oDFONWDSWI+d/+asGFDmtrdOlqGqxzzeqb14Wvz3f+zpd5a042hAS5DV1YFLjbTjK1XbcXmpJc7mGR5prHc06gq762X8krydVQckEaqDteNHi/LZWyermJtneKSoh/YGGb7aPuZWkrx15TPP//7EDtx3ZzGwkizuQKqjkRaLv212jRYXzfVRzmvHPJF6IDMf9L2wSiolOWlJOy9jH7FNBZxhM6vS/in7v83gK1QAXM+++7rqF77lv1FcPq/P8UU8Fpp/Nbbj7X/F00H6bX6zzOtZaP5VbcdK5BraMRb6zXrfpZbt+IP1Ub7ksXf9vXH2t+MouI8sZ2j5yLgzJu/RXhNlAc1f/burqJclA46KU93PvYvYfsweR9VJGXbnxLij3p28eeb1ooyV7SxPznCcJKx2fB8t61s+bIw8fXLrvdxPKQy04znVOOy5JZVss65XNxAnCdt7x5Zj9tjM5ECqZ+34fQa2Xznms5WRt9Ep6Kple22PsxyQzGoVp3rffDLleYblmRzI82q9xmbF7eRtYvMpP2dWBx8RtdFlM23JyUCc6sIN9NBo/ECPIJIkcyzPs3b8egM7OTr4iKRLRgOOipOE1Y7v5tgkl0+xUi+0vx1PH6uMqI0mA46Kc0g71ouvoR3TZ15of8rqoPrwleZCdOmMOHnYxvxHM+wtAxLJ+k5D7Tgab72+btWOV63k56DdpbN/skPaMY9j3VmrNt/qm6zLpvQpSWLLpeE1WpbV0n9R+Znrd+bxPZupthav1L6sCgAAAAAAAAAAAAAAAAAAAAAAAAAAAADgUv8Hj9Rqx8Hn/FQAAAAASUVORK5CYII=" alt="" />
范例:数据删除
命令: DELETEFROM 表名称 WHRER 删除条件() ;
String sql = "DELETE FROM member WHERE name = '张三'";
数据查询
数据查询的结果返回程序,由用户处理
在数据库中虽然有几百张数据表,但是数据表的数据组成的类型是固定的。
next
boolean next()
throws SQLException
Moves the cursor froward one row from its current position
getInt
int getInt(String columnLabel)
throws SQLException
等等。。。
//开发标准,不写 *
String sql = "SELECT mid,name,age,birthday,note FROM member";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int mid = rs.getInt("mid") ;
String name = rs.getString("name") ;
int age = rs.getInt("age") ;
Date birthday = rs.getDate("birthday");
String note = rs.getString("note"); System.out.println(mid + ", " + name +", " + age + ", " + birthday +", " +note);
}