数据库为MySQL数据库,Oracle数据库类似:

表结构参照

插入数据

package com.jef.sql;

import java.sql.Connection;
import java.sql.PreparedStatement; /**
* //演示如何使用com.mysql.jdbc连接桥连接Mysql,插入内容,共三种方式:静态的两种executeUpdate(),executeBatch(),动态的PrepareStatement,
* 实时动态时可以采用PrepareStatement()和executeBatch()的结合,删除、修改都有这三种方法
*/
public class JavaCtMysqlInsert {
public static void main(String[] args) {
try {
Connection ct = ConnectionMySQL.getMySQLConnection();
ct.setAutoCommit(false);
PreparedStatement ps = ct.prepareStatement("insert into user values(?, ?)");
ps.setString(1, "ranye");
ps.setString(2, "100");
ps.executeUpdate();
ps.clearParameters();
ct.commit();
ct.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.jef.sql;

import java.sql.Connection;
import java.sql.Statement; public class JavaCtMysqlInsertTwo {
public static void main(String[] args) {
try {
Connection ct = ConnectionMySQL.getMySQLConnection();
ct.setAutoCommit(false);
Statement sm = ct.createStatement();
sm.addBatch("insert into user values('dage', 100)");
sm.addBatch("insert into user values('haonan', 100)");
sm.executeBatch();
ct.commit();
sm.close();
ct.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.jef.sql;

import java.sql.Connection;
import java.sql.Statement; public class JavaCtMysqlInsertThree {
public static void main(String[] args) {
try {
Connection ct = ConnectionMySQL.getMySQLConnection();
ct.setAutoCommit(false);
Statement sm = ct.createStatement();
sm.executeUpdate("insert into user values('yuanyuan', '100')");
ct.commit();
sm.close();
ct.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

删除数据

package com.jef.sql;

import java.sql.Connection;
import java.sql.Statement; public class JavaCtMysqlDelete {
public static void main(String[] args) {
try {
Connection ct = ConnectionMySQL.getMySQLConnection();
Statement sm = ct.createStatement();
sm.executeUpdate("delete from user where name='tufujie'");
sm.close();
ct.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.jef.sql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement; public class JavaCtMysqlDeleteTwo {
public static void main(String[] args) {
try {
Connection ct = ConnectionMySQL.getMySQLConnection();
Statement sm = ct.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = sm.executeQuery("select * from user where name='dage'");// 从查询到的内容中进行修改、插入和删除
rs.last();
rs.deleteRow();
rs.close();
sm.close();
ct.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

修改数据

package com.jef.sql;

import java.sql.Connection;
import java.sql.Statement; public class JavaCtMysqlUpdate {
public static void main(String[] args) {
try {
Connection ct = ConnectionMySQL.getMySQLConnection();
Statement sm = ct.createStatement();
sm.executeUpdate("update user set name='ran' where name='ranye'");
sm.close();
ct.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

查询数据

package com.jef.sql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement; /**
* 当前情况下至少需要4+2条数据存在,特殊场景需捕获或者抛出
*/
public class JavaCtMysqlSelect {
public static void main(String[] args) {
try {
Connection conn = ConnectionMySQL.getMySQLConnection();
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet result = stmt.executeQuery("select * from user");
System.out.println("从末尾数据开始输出");
result.last();
System.out.println(result.getString("name") + "\t" + result.getString("password"));
while (result.previous())
System.out.println(result.getString("name") + "\t" + result.getString("password"));
System.out.println("从起始数据开始输出");
result.first();
System.out.println(result.getString("name") + "\t" + result.getString("password"));
while(result.next())
System.out.println(result.getString("name") + "\t" + result.getString("password")); // 指定第几笔数据
System.out.println("指定第几笔数据,这里指定第4笔数据");
result.absolute(4);
System.out.println(result.getString("name") + "\t" + result.getString("password")); // 从目前游标处指定游标下移数
System.out.println("从目前游标处指定游标位置,这里向下移动2笔数据");
result.relative(2);
System.out.println(result.getString("name") + "\t" + result.getString("password"));
result.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Java数据库增删改查的更多相关文章

  1. Android(java)学习笔记193:利用谷歌API对数据库增删改查(推荐使用)

    接下来我们通过项目案例来介绍:这个利用谷歌API对数据库增删改查 1.首先项目图: 2.这里的布局文件activity_main.xml: <LinearLayout xmlns:android ...

  2. Java连接MySQL数据库增删改查通用方法

    版权声明:本文为博主原创文章,未经博主允许不得转载. Java连接MySQL数据库增删改查通用方法 运行环境:eclipse+MySQL 以前我们Java连接MySQL数据库都是一个数据库写一个类,类 ...

  3. Android(java)学习笔记136:利用谷歌API对数据库增删改查(推荐使用)

    接下来我们通过项目案例来介绍:这个利用谷歌API对数据库增删改查 1. 首先项目图: 2. 这里的布局文件activity_main.xml: <LinearLayout xmlns:andro ...

  4. Android SQLite 数据库 增删改查操作

    Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...

  5. spring--boot数据库增删改查

    spring--boot数据库增删改查 数据库配置:(必须配置),我写的文件是yml的,和properties是相同的 1 spring: 2 datasource: 3 driver-class-n ...

  6. mybatis--实现数据库增删改查

    首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下: create database my; use my; create table user( id ...

  7. Yii2.0高级框架数据库增删改查的一些操作(转)

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

  8. 2. MongoDB基本操作 —— 用Mongo.exe操作数据库增删改查

    一.开篇 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象 ...

  9. go——beego的数据库增删改查

    一直都不理解使用go语言的时候,为什么还要自己去装beego,以为使用go便可以解决所有的问题,结果在朋友的点拨下,才意识到: go与beego的关系就好比是nodejs与thinkjs的关系,因此也 ...

随机推荐

  1. HTML5 drag & drop 拖拽与拖放简介

    DataTransfer 对象:退拽对象用来传递的媒介,使用一般为Event.dataTransfer. draggable 属性:就是标签元素要设置draggable=true,否则不会有效果,例如 ...

  2. PHP发起get post put delete请求

    <?php class commonFunction{ function callInterfaceCommon($URL,$type,$params,$headers){ $ch = curl ...

  3. CentOS 6.4 中安装部署 Nutch 1.7

    1.配置SSH 自行查阅相关资料 2.安装JDK,配置Java环境 自行查阅相关资料 3.安装SVN [root@master ~]# yum install -y subversion 通过SVN签 ...

  4. Spark Streaming揭秘 Day25 StreamingContext和JobScheduler启动源码详解

    Spark Streaming揭秘 Day25 StreamingContext和JobScheduler启动源码详解 今天主要理一下StreamingContext的启动过程,其中最为重要的就是Jo ...

  5. 1. Linux驱动开发之开篇--Makefile

    基本Makefile假设现在有3个文件,file2.h是函数声明,file2.c是函数定义,文件file1.c调用file2.c中的函数.则Makefile文件的编写如下: helloworld:fi ...

  6. ubuntu下下载并安装H265(hm.x.x代码和X265代码)

    H265,现今是High Efficiency Video Coding的别称,详细的概述见维基百科,详细的开发见官方网站. 一.下载并编译官方的测试源码HM.x.x: 1 ubuntu下安装svn: ...

  7. php校验

    //校验function filters($grams){    if(get_magic_quotes_gpc()) {        $resgram = trim($grams);       ...

  8. install window7

    install window7 http://www.zhujixc.com/win7home/http://jingyan.baidu.com/album/5bbb5a1b3e301713eba17 ...

  9. iisapp appcmd

    C:\Windows\System32\inetsrv>appcmd list wp

  10. HttpWebRequest

    同步请求=====================================================================================  byte[] da ...