使用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 ...
随机推荐
- Python主流框架
15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框架Django 应该是最出名的Pyth ...
- win8 商店应用 设计风格原则
共八条: 1,突出内容(数据). a,仅在屏幕上保留最相关的元素:移除线条.框和不必要的图形效果:限制屏幕上持久显示的导航框,如选项卡. b,交互尽量直接在内容上,直接控制内容来完成操作,而不是使用控 ...
- input type=file 怎么样调取用户手机照相机
input 有个属性accept="image/*" 这样就可以了,同时在网上看到了其他答案,试了下没啥效果.写记录下来 如下: 使用input:file标签, 去调用系统默认相机 ...
- 【python】python内存管理摘要
a = 1 id(a) == id(1) 每次退出ipython重新进入,这个Id都会不一样 sys.getrefcount(a) 可以计数某个对象的引用次数,是原来的次数+1 垃圾回收 使用gc包 ...
- Node.js 问题集合
使用node合并多个接口, 最后获取数据慢的问题 暂时没解决方法 pm2 访问 ip 记录到日志 ...
- Lua基础---变量与赋值
看以下案例: test.lua -- 第一个lua脚本 --注释使用"--"符 --变量未定义时,默认初始化的值为nil --这样的定义为全局 num1 = 1 ; --加了关键字 ...
- 旧书重温:0day2【9】第六章 攻击c++的虚函数
不知不觉,我们学到了0day2的第六章形形色色的内存攻击技术!其中,这张很多东西都是理论的东西,不过!我们还是要想办法还原下发生的现场! 其中部分文章截图 http://user.qzone.qq.c ...
- 解放内存之搭建自己的 R Server
学校的课五门有四门需要跑R程序,有一些长长长的代码实在是占用了太多的内存,果断决定搭个R的服务器放着自己跑. 愉快的是,R studio server 的搭建真心简单快捷~这个从前被我忽略的东东终于排 ...
- 【转载】GetAdaptersInfo函数在64位系统上返回ERROR_NOACCESS的有关问题
From:http://www.educity.cn/wenda/351190.html GetAdaptersInfo函数在64位系统下返回ERROR_NOACCESS的问题 实际应用中一个程序在长 ...
- 剑指offer-第七章面试案例1(字符串转换为整型)
//将字符串转换为整型 //思路:特殊的输入测试: //1,考虑字符串是否为空.2.字符串问空的时候的返回0,和真实的返回0直键的区别.3,字符串中出现0~9的字符处理 //4.字符串中出现*,¥等一 ...