一、Statement接口

作用:用于执行静态 SQL 语句并返回它所生成结果的对象。

1. 创建数据库连接类及相册实体,代码如下:

  1. package com.learn.jdbc.util;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.Statement;
  7.  
  8. /**
  9. * 封装数据库连接类
  10. * @author Administrator
  11. *
  12. */
  13. public class DbUtil {
  14. // 数据库连接
  15. private static String dbUrl = "jdbc:mysql://localhost:3306/yizhuangxiu?useUnicode=true&characterEncoding=utf-8";
  16. // 用户名
  17. private static String dbUserName = "root";
  18. // 密码
  19. private static String dbUserPwd = "123456";
  20. // 驱动名
  21. private static String jdbcName = "com.mysql.jdbc.Driver";
  22.  
  23. /**
  24. * 连接数据库
  25. * @return
  26. * @throws Exception
  27. */
  28. public Connection getCon() throws Exception{
  29. Class.forName(jdbcName);
  30. Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbUserPwd);
  31. return con;
  32. }
  33.  
  34. /**
  35. * 关闭连接
  36. * @throws Exception
  37. */
  38. public void close(Statement stmt,Connection con) throws Exception{
  39. if(stmt != null){
  40. stmt.close();
  41. if(con != null){
  42. con.close();
  43. }
  44. }
  45. }
  46.  
  47. /**
  48. * 关闭连接
  49. * @throws Exception
  50. */
  51. public void close(PreparedStatement pstmt,Connection con) throws Exception{
  52. if(pstmt != null){
  53. pstmt.close();
  54. if(con != null){
  55. con.close();
  56. }
  57. }
  58. }
  59.  
  60. }
  1. package com.learn.jdbc.model;
  2.  
  3. import java.io.File;
  4.  
  5. /**
  6. * 相册模型
  7. * @author Administrator
  8. *
  9. */
  10. public class Album {
  11.  
  12. private int id;
  13. private String name;
  14. private int uid;
  15. private long time;
  16.  
  17. private File content;
  18. private File pic;
  19.  
  20. public Album(String name, int uid, long time) {
  21. this.name = name;
  22. this.uid = uid;
  23. this.time = time;
  24. }
  25.  
  26. public Album(int id, String name, int uid, long time) {
  27. this.id = id;
  28. this.name = name;
  29. this.uid = uid;
  30. this.time = time;
  31. }
  32.  
  33. public Album(String name, int uid, long time, File content, File pic) {
  34. super();
  35. this.name = name;
  36. this.uid = uid;
  37. this.time = time;
  38. this.content = content;
  39. this.pic = pic;
  40. }
  41.  
  42. public int getId() {
  43. return id;
  44. }
  45.  
  46. public void setId(int id) {
  47. this.id = id;
  48. }
  49.  
  50. public String getName() {
  51. return name;
  52. }
  53.  
  54. public void setName(String name) {
  55. this.name = name;
  56. }
  57.  
  58. public int getUid() {
  59. return uid;
  60. }
  61.  
  62. public void setUid(int uid) {
  63. this.uid = uid;
  64. }
  65.  
  66. public long getTime() {
  67. return time;
  68. }
  69.  
  70. public void setTime(long time) {
  71. this.time = time;
  72. }
  73.  
  74. public File getContent() {
  75. return content;
  76. }
  77.  
  78. public void setContent(File content) {
  79. this.content = content;
  80. }
  81.  
  82. public File getPic() {
  83. return pic;
  84. }
  85.  
  86. public void setPic(File pic) {
  87. this.pic = pic;
  88. }
  89.  
  90. @Override
  91. public String toString() {
  92. return "["+this.id+","+this.name+","+this.uid+","+this.time+"]";
  93. }
  94.  
  95. }

2. 实现数据增加、修改、删除

  1. package com.learn.jdbc.chap03;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Statement;
  5.  
  6. import com.learn.jdbc.model.Album;
  7. import com.learn.jdbc.util.DbUtil;
  8.  
  9. public class Demo3 {
  10. private static DbUtil dbUtil=new DbUtil();
  11.  
  12. /**
  13. * 添加相册-----用面向对象思想封装
  14. * @param ab
  15. * @return
  16. * @throws Exception
  17. */
  18. private static int addInfo2(Album ab) throws Exception{
  19. Connection con = dbUtil.getCon();
  20. String sql = "insert into sp_album values (null,'"+ab.getName()+"',"+ab.getUid()+","+ab.getTime()+")";
  21. Statement stmt = con.createStatement();
  22. int result=stmt.executeUpdate(sql);
  23. dbUtil.close(stmt, con);
  24. return result;
  25. }
  26.  
  27. /**
  28. * 添加相册-----普通封装
  29. * @param name
  30. * @param uid
  31. * @param time
  32. * @return
  33. * @throws Exception
  34. */
  35. private static int addInfo(String name,int uid,long time) throws Exception{
  36. Connection con = dbUtil.getCon();
  37. String sql = "insert into sp_album values (null,'"+name+"',"+uid+","+time+")";
  38. Statement stmt = con.createStatement();
  39. int result=stmt.executeUpdate(sql);
  40. dbUtil.close(stmt, con);
  41. return result;
  42. }
  43.  
  44. public static void main(String[] args) throws Exception{
  45. /*int result = addInfo("李四",8,System.currentTimeMillis());
  46. if(result>0){
  47. System.out.println("数据插入成功!");
  48. }else{
  49. System.out.println("数据插入失败!");
  50. }*/
  51.  
  52. int result1 = addInfo2(new Album("呵呵",7,System.currentTimeMillis()));
  53. if(result1>0){
  54. System.out.println("数据插入成功!");
  55. }else{
  56. System.out.println("数据插入失败!");
  57. }
  58. }
  59. }
  1. package com.learn.jdbc.chap03;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Statement;
  5.  
  6. import com.learn.jdbc.model.Album;
  7. import com.learn.jdbc.util.DbUtil;
  8.  
  9. public class Demo4 {
  10. private static DbUtil dbUtil = new DbUtil();
  11.  
  12. /**
  13. * 更新相册
  14. * @param ab
  15. * @return
  16. * @throws Exception
  17. */
  18. private static int updateInfo(Album ab) throws Exception {
  19. Connection con = dbUtil.getCon();
  20. String sql = "update sp_album set name='" + ab.getName() + "',uid="
  21. + ab.getUid() + ",add_time=" + ab.getTime() + " where id="
  22. + ab.getId();
  23. Statement stmt = con.createStatement();
  24. int result = stmt.executeUpdate(sql);
  25. dbUtil.close(stmt, con);
  26. return result;
  27. }
  28.  
  29. public static void main(String[] args) throws Exception {
  30. int result1 = updateInfo(new Album(12,"呵呵1", 6, System.currentTimeMillis()));
  31. if (result1 > 0) {
  32. System.out.println("数据修改成功!");
  33. } else {
  34. System.out.println("数据修改失败!");
  35. }
  36. }
  37. }
  1. package com.learn.jdbc.chap03;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Statement;
  5.  
  6. import com.learn.jdbc.model.Album;
  7. import com.learn.jdbc.util.DbUtil;
  8.  
  9. public class Demo5 {
  10. private static DbUtil dbUtil = new DbUtil();
  11. /**
  12. * 删除数据
  13. * @param id
  14. * @return
  15. * @throws Exception
  16. */
  17. private static int deleteInfo(int id) throws Exception{
  18. Connection con = dbUtil.getCon();
  19. String sql = "delete from sp_album where id="+id;
  20. Statement stmt = con.createStatement();
  21. int result = stmt.executeUpdate(sql);
  22. dbUtil.close(stmt, con);
  23. return result;
  24. }
  25.  
  26. public static void main(String[] args) throws Exception {
  27. int result1 = deleteInfo(13);
  28. if (result1 > 0) {
  29. System.out.println("数据删除成功!");
  30. } else {
  31. System.out.println("数据删除失败!");
  32. }
  33. }
  34. }

所用到的表结构

  1. CREATE TABLE `sp_album_test` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) DEFAULT '' COMMENT '相册名称',
  4. `uid` int(11) DEFAULT '' COMMENT '用户id',
  5. `add_time` bigint(13) DEFAULT '' COMMENT '创建时间',
  6. `content` longtext COMMENT '简介',
  7. `pic` longblob COMMENT '图像',
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='相册1'
  10.  
  11. CREATE TABLE `sp_account` (
  12. `id` int(11) NOT NULL AUTO_INCREMENT,
  13. `accountName` varchar(255) DEFAULT '' COMMENT '转账用户',
  14. `accountBalance` double DEFAULT '' COMMENT '转账金额',
  15. PRIMARY KEY (`id`)
  16. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='银行转账--Java测试'

使用Statement接口实现增,删,改操作(工作中不常用这个,而用PreparedStatement接口)的更多相关文章

  1. 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据

    第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...

  2. 【转】Android 增,删,改,查 通讯录中的联系人

    一.权限 操作通讯录必须在AndroidManifest.xml中先添加2个权限, <uses-permission android:name="android.permission. ...

  3. Android 增,删,改,查 通讯录中的联系人

    一.权限 操作通讯录必须在AndroidManifest.xml中先添加2个权限, <uses-permission android:name="android.permission. ...

  4. C# ADO.NET (sql语句连接方式)(增,删,改)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  5. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  6. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  7. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  8. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

  9. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

随机推荐

  1. IOS-RunTime应用

    什么是Runtime 总结起来,iOS中的RunTime的作用有以下几点: 1.发送消息(obj_msgSend) 2.方法交换(method_exchangeImplementations) 3.消 ...

  2. jQuery对select操作

    (转自:http://www.cnblogs.com/as285996985/articles/1535014.html) //遍历option和添加.移除optionfunction changeS ...

  3. 深入理解c/c++ 内存对齐

    内存对齐,memory alignment.为了提高程序的性能,数据结构(尤其是栈)应该尽可能地在自然边界上对齐.原因在于,为了访问未对齐的内存,处理器需要作两次内存访问:然而,对齐的内存访问仅需要一 ...

  4. Struts05---动态查询

    01.在上面案例的login.jsp页面新增 <%-- 2.动态方法的调用 前提是在 struts.xml文件中开启 不推荐! --%> <a href="user/use ...

  5. 条款22:将成员变量声明为private

    protected成员变量的封装性并非高于public变量. 如果有个public的成员变量,一旦其需要改变,那么所有使用它的代码都需要改变. 如果有个protected的成员变量,一点其需要改变,那 ...

  6. 【git】git知识梳理(一):基本操作&远程控制&分支管理

    (一)基本操作:  git中所有文件一共有三个状态:已提交,已暂存,已修改. 三个工作区域: git目录:.git文件夹,每次拷贝其实只拷贝git目录 工作目录:文件和目录都是从git目录中压缩对象数 ...

  7. 理解 Promise 过程

    /** new Promise(fn1).then(function(val){ console.log(val); }) 主要是把 then 的函数抽取出来 , 利用闭包存放在 callback中, ...

  8. C与C++结构体的区别

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  9. Python itertools模块中的product函数

    product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in A for y ...

  10. ng 服务

    服务的本质是单例对象,封装一些方法和属性的. 单例模式:在实例化变量的时候,如果该变量已经存在,直接返回该变量:如果不存在,就创建一个新的变量再返回 ng自带的服务有很多,常用:$location $ ...