利用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,iVBORw0KGgoAAAANSUhEUgAAAlwAAABnCAIAAABaXAugAAAHf0lEQVR4nO3d347cthUHYL5pGrRAmzZoiyZpavjl5lWaIheBESAFYsDozkUvllmMRfLo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ebJe60YFKz1DKC56LLvb6+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+FALfWfX2qJPwX7rhxWfThw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qrm8ePEPXEMp/723Hy3K45l/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);
}

Statemen接口对象的更多相关文章

  1. 创建DirectInput接口对象说明---(void **)&m_pDI

    读别人代码时遇到的,起初没明白过来这是怎么回事,后来忽然想明白了. if (FAILED(DirectInput8Create(appInstance, DIRECTINPUT_VERSION, II ...

  2. C# 实例化接口对象

    在head first 设计模式中 用到了很多很多接口对象 首先澄清一个问题,就是接口不仅可以声明对象,而且可以把对象实例化,还可以当做参数被传入. 一.接口回调 这就是继承中的向上转型.父类 FL= ...

  3. Tcp 数据对象传输接口对象设计

    输入是一个对象inputObj,接口对象.Send(inputObj),对端接收之后解包成outputObj(与inputObj应相同),触发onPackageReceive事件 事件 public ...

  4. 服务端怎样暴露IBinder接口对象

    服务端怎样暴露IBinder接口对象: package com.example.mydownload; import android.app.Service; import android.conte ...

  5. dhroid - ioc高级(接口,对象注入)

    下面到了接口对象的注入了解冻吧,现在才是我们的重点,这才是ioc的核心思想,上面的都是android的辅助1.5 对象依赖问题 我们先来将一下对象对象依赖的重要性,很多同学可能只学了android没学 ...

  6. client怎样调用IBinder接口对象

    代码: public void funclick(View view){ Intent _intent = new Intent(MainActivity.this,MyService.class); ...

  7. autofac如何注册静态方法里的接口对象

    标题可能是不准确的,因为我不知道如何描述.不知道的原因,是对依赖注入一知半解. Autofac可以自动注册对象实例到接口,人所尽知.而在asp.net mvc中,这个实例化的工作,通常在每个控制器的构 ...

  8. Java基础--接口回调(接口 对象名 = new 类名)理解

    接口 对象名1 = new 类名和类名 对象名2 = new 类名的区别是什么? 实例 /** *Person.java 接口 */ public interface Person { void in ...

  9. golang中接口对象的转型

    接口对象的转型有两种方式: 1. 方式一:instance,ok:=接口对象.(实际类型) 如果该接口对象是对应的实际类型,那么instance就是转型之后对象,ok的值为true 配合if...el ...

随机推荐

  1. POJ - 2336 Wireless Network

    Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have ...

  2. Sea.Js使用入门

    1.Sea.Js是什么 seajs相对于RequireJs与LabJS就比较年轻,2010年玉伯发起了这个开源项目,SeaJS遵循CMD规范,与RequireJS类似,同样做为模块加载器.示例 // ...

  3. PAT (Advanced Level) 1107. Social Clusters (30)

    简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  4. mysql deadlock

    http://database.51cto.com/art/201108/286325.htm 这篇文章说的很清楚,记下来. 原因分析: 当“update tab_test set state=106 ...

  5. String转float

    float mTotalhour = Float.parseFloat(mTotalHourEt.getText().toString().trim());

  6. 简单的划分数问题<划分问题>

    将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问有多少种不同的分法. 思路: 动态规 ...

  7. PAT (Advanced Level) 1041. Be Unique (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  8. win8 explorer 进程频繁奔溃的原因及处理

    最近  ,  win8 系统中的 explorer 进程总是频繁奔溃    ,    想想可能是最近新装或更新了什么软件引起的 . 查看了记录奔溃的详细信息 ,  果不其然 , 是最近更新的快播新版本 ...

  9. JSP中EL表达式取值问题记录(已解决)

    ***************************2015-10-28 22:21************************* 问题描述如下: 在当前的jsp页面已经有了如下代码: < ...

  10. FatMouse's Speed 基础DP

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...