- 浏览: 174798 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (321)
- eclipse (4)
- idea (2)
- Html (8)
- Css (14)
- Javascript (8)
- Jquery (6)
- Ajax Json (4)
- Bootstrap (0)
- EasyUI (0)
- Layui (0)
- 数据结构 (0)
- Java (46)
- DesPattern (24)
- Algorithm (2)
- Jdbc (8)
- Jsp servlet (13)
- Struts2 (17)
- Hibernate (11)
- Spring (5)
- S2SH (1)
- SpringMVC (4)
- SpringBoot (11)
- WebService CXF (4)
- Poi (2)
- JFreeChart (0)
- Shiro (6)
- Lucene (5)
- ElasticSearch (0)
- JMS ActiveMQ (3)
- HttpClient (5)
- Activiti (0)
- SpringCloud (11)
- Dubbo (6)
- Docker (0)
- MySQL (27)
- Oracle (18)
- Redis (5)
- Mybatis (11)
- SSM (1)
- CentOS (10)
- Ant (2)
- Maven (4)
- Log4j (7)
- XML (5)
最新评论
1. Statement接口引入
2. 使用Statement接口实现添加数据操作
3. 使用Statement接口实现更新数据操作
4. 使用Statement接口实现删除数据操作
作用:用于执行静态SQL语句并返回它所生成结果的对象。 int executeUpdate(String sql)执行给定SQL语句,该语句可能为INSERT、UPDATE或DELETE语句,或者不返回任何内容的SQL语句(如SQL DDL语句)。 void close()立即释放此Statement对象的数据库和JDBC资源,而不是等待该对象自动关闭时发生此操作。
2. 使用Statement接口实现添加数据操作
3. 使用Statement接口实现更新数据操作
4. 使用Statement接口实现删除数据操作
创建数据库db_jdbc create table t_book( id int primary key auto_increment, bookName char(20), price decimal(8,2), author char(20), bookTypeId int ) Book.java package com.andrew.jdbc.model; public class Book { private int id; private String bookName; private float price; private String author; private int bookTypeId; public Book(String bookName, float price, String author, int bookTypeId) { super(); this.bookName = bookName; this.price = price; this.author = author; this.bookTypeId = bookTypeId; } public Book(int id, String bookName, float price, String author, int bookTypeId) { super(); this.id = id; this.bookName = bookName; this.price = price; this.author = author; this.bookTypeId = bookTypeId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getBookTypeId() { return bookTypeId; } public void setBookTypeId(int bookTypeId) { this.bookTypeId = bookTypeId; } } DbUtil.java package com.andrew.jdbc.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class DbUtil { private static String dbUrl = "jdbc:mysql://localhost:3306/db_jdbc"; private static String dbUserName = "root"; private static String dbPassword = "root"; private static String jdbcName = "com.mysql.jdbc.Driver"; public Connection getConnection() throws Exception { Class.forName(jdbcName); Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword); return connection; } public void close(Statement stmt, Connection con) throws Exception { if (stmt != null) { stmt.close(); if (con != null) { con.close(); } } } } TestInsert01.java package com.andrew.jdbc.chap03; import java.sql.Connection; import java.sql.Statement; import com.andrew.jdbc.util.DbUtil; public class TestInsert01 { public static void main(String[] args) throws Exception { DbUtil dbUtil = new DbUtil(); String sql = "insert into t_book values(null, 'java学习', 666, '张三', 1)"; Connection connection = dbUtil.getConnection(); // 获取数据连接 Statement statement = connection.createStatement(); // 获取Statement int result = statement.executeUpdate(sql); System.out.println("操作的结果:" + result + "数据"); statement.close(); // 关闭statement connection.close(); // 关闭连接 } } 操作的结果:1数据 TestInsert02.java package com.andrew.jdbc.chap03; import java.sql.Connection; import java.sql.Statement; import com.andrew.jdbc.model.Book; import com.andrew.jdbc.util.DbUtil; public class TestInsert02 { private static DbUtil dbUtil = new DbUtil(); private static int addBook(String bookName, float price, String author, int bookTypeId) throws Exception { Connection connection = dbUtil.getConnection(); // 获取连接 String sql = "insert into t_book values(null,'" + bookName + "'," + price + ",'" + author + "'," + bookTypeId + ")"; Statement stmt = connection.createStatement(); // 创建Statement int result = stmt.executeUpdate(sql); dbUtil.close(stmt, connection); // 关闭Statement和连接 return result; } private static int addBook2(Book book) throws Exception { Connection connection = dbUtil.getConnection(); // 获取连接 String sql = "insert into t_book values(null,'" + book.getBookName() + "'," + book.getPrice() + ",'" + book.getAuthor() + "'," + book.getBookTypeId() + ")"; Statement statement = connection.createStatement(); // 创建Statement int result = statement.executeUpdate(sql); dbUtil.close(statement, connection); // 关闭Statement和连接 return result; } public static void main(String[] args) throws Exception { int result = addBook("Java学习1", 101, "牛哥1", 1); if (result == 1) { System.out.println("添加成功!"); } else { System.out.println("添加失败!"); } Book book = new Book("Java学习2", 102, "牛哥2", 2); int result2 = addBook2(book); if (result2 == 1) { System.out.println("添加成功!"); } else { System.out.println("添加失败!"); } } } 添加成功! 添加成功! TestUpdate.java package com.andrew.jdbc.chap03; import java.sql.Connection; import java.sql.Statement; import com.andrew.jdbc.model.Book; import com.andrew.jdbc.util.DbUtil; public class TestUpdate { private static DbUtil dbUtil = new DbUtil(); private static int updateBook(Book book) throws Exception { Connection con = dbUtil.getConnection(); // 获取连接 String sql = "update t_book set bookName='" + book.getBookName() + "',price=" + book.getPrice() + ",author='" + book.getAuthor() + "',bookTypeId=" + book.getBookTypeId() + " where id=" + book.getId(); Statement stmt = con.createStatement(); // 创建Statement int result = stmt.executeUpdate(sql); dbUtil.close(stmt, con); // 关闭Statement和连接 return result; } public static void main(String[] args) throws Exception { Book book = new Book(2, "Java修改", 666, "修改人2", 1); int result = updateBook(book); if (result == 1) { System.out.println("更新成功!"); } else { System.out.println("更新败!"); } } } 更新成功! TestDelete.java package com.andrew.jdbc.chap03; import java.sql.Connection; import java.sql.Statement; import com.andrew.jdbc.util.DbUtil; public class TestDelete { private static DbUtil dbUtil = new DbUtil(); private static int deleteBook(int id) throws Exception { Connection connection = dbUtil.getConnection(); // 获取连接 String sql = "delete from t_book where id=" + id; Statement statement = connection.createStatement(); // 创建Statement int result = statement.executeUpdate(sql); dbUtil.close(statement, connection); // 关闭Statement和连接 return result; } public static void main(String[] args) throws Exception { int result = deleteBook(3); if (result == 1) { System.out.println("删除成功!"); } else { System.out.println("删除失败!"); } } } 删除成功!
发表评论
-
JDBC事务处理
2019-04-18 00:13 3921. 事务的概念 事务处理在数据库开发中有着非常重要的作 ... -
使用元数据分析数据库
2019-04-18 00:13 3981. 使用DatabaseMetaData获取数据库基本信息 ... -
使用CallableStatement接口调用存储过程
2019-04-18 00:13 8701. CallableStatement接口的引入 Ca ... -
处理大数据对象
2019-04-17 09:18 3641. 处理大数据对象 大数据对象处理主要有CLOB(ch ... -
ResultSet结果集
2019-04-17 09:17 6001. ResultSet结果集的引入 当我们查询数据库时 ... -
使用PreparedStatement接口实现增,删,改操作
2019-04-17 09:17 5541. PreparedStatement接口引入 Pre ... -
JDBC概述与连接
2019-04-17 09:17 3751. JDBC概述 1.1) JDBC简介 ...
相关推荐
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...
ORM:在持久化对象上执行基本的增、删、改、查操作 使用Mybatis的步骤 1、 加载数据库驱动 2、 创建并获取数据库链接 3、 创建jdbc statement对象 4、 设置sql语句 5、 设置sql语句中的参数(使用preparedStatement) ...