使用Statement接口实现增,删,改操作(工作中不常用这个,而用PreparedStatement接口)
一、Statement接口
作用:用于执行静态 SQL 语句并返回它所生成结果的对象。
1. 创建数据库连接类及相册实体,代码如下:
- package com.learn.jdbc.util;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.Statement;
- /**
- * 封装数据库连接类
- * @author Administrator
- *
- */
- public class DbUtil {
- // 数据库连接
- private static String dbUrl = "jdbc:mysql://localhost:3306/yizhuangxiu?useUnicode=true&characterEncoding=utf-8";
- // 用户名
- private static String dbUserName = "root";
- // 密码
- private static String dbUserPwd = "123456";
- // 驱动名
- private static String jdbcName = "com.mysql.jdbc.Driver";
- /**
- * 连接数据库
- * @return
- * @throws Exception
- */
- public Connection getCon() throws Exception{
- Class.forName(jdbcName);
- Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbUserPwd);
- return con;
- }
- /**
- * 关闭连接
- * @throws Exception
- */
- public void close(Statement stmt,Connection con) throws Exception{
- if(stmt != null){
- stmt.close();
- if(con != null){
- con.close();
- }
- }
- }
- /**
- * 关闭连接
- * @throws Exception
- */
- public void close(PreparedStatement pstmt,Connection con) throws Exception{
- if(pstmt != null){
- pstmt.close();
- if(con != null){
- con.close();
- }
- }
- }
- }
- package com.learn.jdbc.model;
- import java.io.File;
- /**
- * 相册模型
- * @author Administrator
- *
- */
- public class Album {
- private int id;
- private String name;
- private int uid;
- private long time;
- private File content;
- private File pic;
- public Album(String name, int uid, long time) {
- this.name = name;
- this.uid = uid;
- this.time = time;
- }
- public Album(int id, String name, int uid, long time) {
- this.id = id;
- this.name = name;
- this.uid = uid;
- this.time = time;
- }
- public Album(String name, int uid, long time, File content, File pic) {
- super();
- this.name = name;
- this.uid = uid;
- this.time = time;
- this.content = content;
- this.pic = pic;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getUid() {
- return uid;
- }
- public void setUid(int uid) {
- this.uid = uid;
- }
- public long getTime() {
- return time;
- }
- public void setTime(long time) {
- this.time = time;
- }
- public File getContent() {
- return content;
- }
- public void setContent(File content) {
- this.content = content;
- }
- public File getPic() {
- return pic;
- }
- public void setPic(File pic) {
- this.pic = pic;
- }
- @Override
- public String toString() {
- return "["+this.id+","+this.name+","+this.uid+","+this.time+"]";
- }
- }
2. 实现数据增加、修改、删除
- package com.learn.jdbc.chap03;
- import java.sql.Connection;
- import java.sql.Statement;
- import com.learn.jdbc.model.Album;
- import com.learn.jdbc.util.DbUtil;
- public class Demo3 {
- private static DbUtil dbUtil=new DbUtil();
- /**
- * 添加相册-----用面向对象思想封装
- * @param ab
- * @return
- * @throws Exception
- */
- private static int addInfo2(Album ab) throws Exception{
- Connection con = dbUtil.getCon();
- String sql = "insert into sp_album values (null,'"+ab.getName()+"',"+ab.getUid()+","+ab.getTime()+")";
- Statement stmt = con.createStatement();
- int result=stmt.executeUpdate(sql);
- dbUtil.close(stmt, con);
- return result;
- }
- /**
- * 添加相册-----普通封装
- * @param name
- * @param uid
- * @param time
- * @return
- * @throws Exception
- */
- private static int addInfo(String name,int uid,long time) throws Exception{
- Connection con = dbUtil.getCon();
- String sql = "insert into sp_album values (null,'"+name+"',"+uid+","+time+")";
- Statement stmt = con.createStatement();
- int result=stmt.executeUpdate(sql);
- dbUtil.close(stmt, con);
- return result;
- }
- public static void main(String[] args) throws Exception{
- /*int result = addInfo("李四",8,System.currentTimeMillis());
- if(result>0){
- System.out.println("数据插入成功!");
- }else{
- System.out.println("数据插入失败!");
- }*/
- int result1 = addInfo2(new Album("呵呵",7,System.currentTimeMillis()));
- if(result1>0){
- System.out.println("数据插入成功!");
- }else{
- System.out.println("数据插入失败!");
- }
- }
- }
- package com.learn.jdbc.chap03;
- import java.sql.Connection;
- import java.sql.Statement;
- import com.learn.jdbc.model.Album;
- import com.learn.jdbc.util.DbUtil;
- public class Demo4 {
- private static DbUtil dbUtil = new DbUtil();
- /**
- * 更新相册
- * @param ab
- * @return
- * @throws Exception
- */
- private static int updateInfo(Album ab) throws Exception {
- Connection con = dbUtil.getCon();
- String sql = "update sp_album set name='" + ab.getName() + "',uid="
- + ab.getUid() + ",add_time=" + ab.getTime() + " where id="
- + ab.getId();
- Statement stmt = con.createStatement();
- int result = stmt.executeUpdate(sql);
- dbUtil.close(stmt, con);
- return result;
- }
- public static void main(String[] args) throws Exception {
- int result1 = updateInfo(new Album(12,"呵呵1", 6, System.currentTimeMillis()));
- if (result1 > 0) {
- System.out.println("数据修改成功!");
- } else {
- System.out.println("数据修改失败!");
- }
- }
- }
- package com.learn.jdbc.chap03;
- import java.sql.Connection;
- import java.sql.Statement;
- import com.learn.jdbc.model.Album;
- import com.learn.jdbc.util.DbUtil;
- public class Demo5 {
- private static DbUtil dbUtil = new DbUtil();
- /**
- * 删除数据
- * @param id
- * @return
- * @throws Exception
- */
- private static int deleteInfo(int id) throws Exception{
- Connection con = dbUtil.getCon();
- String sql = "delete from sp_album where id="+id;
- Statement stmt = con.createStatement();
- int result = stmt.executeUpdate(sql);
- dbUtil.close(stmt, con);
- return result;
- }
- public static void main(String[] args) throws Exception {
- int result1 = deleteInfo(13);
- if (result1 > 0) {
- System.out.println("数据删除成功!");
- } else {
- System.out.println("数据删除失败!");
- }
- }
- }
所用到的表结构
- CREATE TABLE `sp_album_test` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(255) DEFAULT '' COMMENT '相册名称',
- `uid` int(11) DEFAULT '' COMMENT '用户id',
- `add_time` bigint(13) DEFAULT '' COMMENT '创建时间',
- `content` longtext COMMENT '简介',
- `pic` longblob COMMENT '图像',
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='相册1'
- CREATE TABLE `sp_account` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `accountName` varchar(255) DEFAULT '' COMMENT '转账用户',
- `accountBalance` double DEFAULT '' COMMENT '转账金额',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='银行转账--Java测试'
使用Statement接口实现增,删,改操作(工作中不常用这个,而用PreparedStatement接口)的更多相关文章
- 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据
第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...
- 【转】Android 增,删,改,查 通讯录中的联系人
一.权限 操作通讯录必须在AndroidManifest.xml中先添加2个权限, <uses-permission android:name="android.permission. ...
- Android 增,删,改,查 通讯录中的联系人
一.权限 操作通讯录必须在AndroidManifest.xml中先添加2个权限, <uses-permission android:name="android.permission. ...
- C# ADO.NET (sql语句连接方式)(增,删,改)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...
- MVC EF 增 删 改 查
using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
随机推荐
- IOS-RunTime应用
什么是Runtime 总结起来,iOS中的RunTime的作用有以下几点: 1.发送消息(obj_msgSend) 2.方法交换(method_exchangeImplementations) 3.消 ...
- jQuery对select操作
(转自:http://www.cnblogs.com/as285996985/articles/1535014.html) //遍历option和添加.移除optionfunction changeS ...
- 深入理解c/c++ 内存对齐
内存对齐,memory alignment.为了提高程序的性能,数据结构(尤其是栈)应该尽可能地在自然边界上对齐.原因在于,为了访问未对齐的内存,处理器需要作两次内存访问:然而,对齐的内存访问仅需要一 ...
- Struts05---动态查询
01.在上面案例的login.jsp页面新增 <%-- 2.动态方法的调用 前提是在 struts.xml文件中开启 不推荐! --%> <a href="user/use ...
- 条款22:将成员变量声明为private
protected成员变量的封装性并非高于public变量. 如果有个public的成员变量,一旦其需要改变,那么所有使用它的代码都需要改变. 如果有个protected的成员变量,一点其需要改变,那 ...
- 【git】git知识梳理(一):基本操作&远程控制&分支管理
(一)基本操作: git中所有文件一共有三个状态:已提交,已暂存,已修改. 三个工作区域: git目录:.git文件夹,每次拷贝其实只拷贝git目录 工作目录:文件和目录都是从git目录中压缩对象数 ...
- 理解 Promise 过程
/** new Promise(fn1).then(function(val){ console.log(val); }) 主要是把 then 的函数抽取出来 , 利用闭包存放在 callback中, ...
- C与C++结构体的区别
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- Python itertools模块中的product函数
product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in A for y ...
- ng 服务
服务的本质是单例对象,封装一些方法和属性的. 单例模式:在实例化变量的时候,如果该变量已经存在,直接返回该变量:如果不存在,就创建一个新的变量再返回 ng自带的服务有很多,常用:$location $ ...