Java -- JDBC 学习--通过Statement进行数据库更新操作
通过 JDBC 向指定的数据表中插入一条记录.
1. Statement: 用于执行 SQL 语句的对象
1). 通过 Connection 的 createStatement() 方法来获取
2). 通过 executeUpdate(sql) 可以执行 SQL 语句.
3). 传入的 SQL 可以是 INSRET, UPDATE 或 DELETE. 但不能是 SELECT
2. Connection、Statement 都是应用程序和数据库服务器的连接资源. 使用后一定要关闭。
需要在 finally 中关闭 Connection 和 Statement 对象.
3. 关闭的顺序是: 先关闭后获取的. 即先关闭 Statement 后关闭 Connection。
封装jdbc的基本操作:
public class JDBCTools {
public static void release(ResultSet rs,
Statement statement, Connection conn) {
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/**
* 关闭 Statement 和 Connection
* @param statement
* @param conn
*/
public static void release(Statement statement, Connection conn) {
if (statement != null) {
try {
statement.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/**
* 1. 获取连接的方法. 通过读取配置文件从数据库服务器获取一个连接.
*
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception {
// 1. 准备连接数据库的 4 个字符串.
// 1). 创建 Properties 对象
Properties properties = new Properties();
// 2). 获取 jdbc.properties 对应的输入流
InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(
"jdbc.properties");
// 3). 加载 2) 对应的输入流
properties.load(in);
// 4). 具体决定 user, password 等4 个字符串.
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String jdbcUrl = properties.getProperty("jdbcUrl");
String driver = properties.getProperty("driver");
// 2. 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)
Class.forName(driver);
// 3. 通过 DriverManager 的 getConnection() 方法获取数据库连接.
return DriverManager.getConnection(jdbcUrl, user, password);
}
/**
* 通用的更新的方法: 包括 INSERT、UPDATE、DELETE
* 版本 1.
*/
public void update(String sql){
Connection conn = null;
Statement statement = null;
try {
conn = getConnection();
statement = conn.createStatement();
statement.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally{
release(statement, conn);
}
}
}
使用的时候通过调用JDBCTools.java的update方法来进行数据的新增,更新,删除操作
其中jdbc.properties文件配置如下:
#driver=oracle.jdbc.driver.OracleDriver
#jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
#user=scott
#password=java driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/soyoungboy
user=root
password=1230
Java -- JDBC 学习--通过Statement进行数据库更新操作的更多相关文章
- Java JDBC学习实战(二): 管理结果集
在我的上一篇博客<Java JDBC学习实战(一): JDBC的基本操作>中,简要介绍了jdbc开发的基本流程,并详细介绍了Statement和PreparedStatement的使用:利 ...
- JDBC操作,执行数据库更新操作
目标: 使用Connection对象取得Statement实例 使用Statement进行数据增删改. Statement接口 要对数据库操作,要使用Statement完成.此接口可以使用Connec ...
- 吴裕雄--天生自然JAVA数据库编程:执行数据库更新操作
import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.Statement ; public clas ...
- Java知多少(109)数据库更新
数据库更新操作包括数据表创建.删除.以及数据表记录的增加.删除.修改等操作.如果利用数据 SQL命令实现,则利用Statement对旬的executeUpdate()方法,执行SQL的update语句 ...
- python 数据库更新操作
数据库更新操作 更新操作用于更新数据表的的数据,以下实例将 EMPLOYEE 表中的 SEX 字段为 'M' 的 AGE 字段递增 1: #!/usr/bin/python # -*- coding: ...
- Java -- JDBC 学习--获取数据库链接
数据持久化 持久化(persistence): 把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应用,数据持久化意味着将内存中的数据保存到硬盘上加以”固化”,而持久化的实现过程大 ...
- Java JDBC学习实战(一): JDBC的基本操作
一.JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager,管理JDBC驱动的服务类,主要通过它获取Connect ...
- Java -- JDBC 学习--数据库连接池
JDBC数据库连接池的必要性 在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤: 在主程序(如servlet.beans)中建立数据库连接. 进行sql操作 断开数据库连接. 这种模式开 ...
- 记录Access数据库更新操作大坑一个
对于更新Access数据库的操作,必须保持参数数组与sql语句中参数顺序一致,如下: public bool Update(MyModel model) { StringBuilder strSql ...
随机推荐
- spring boot 集成Druid
Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB ...
- 宇宙最强IDE,查看设计器报错,看不了设计界面
在使用自定义控件或者用户控件的时候,查看设计器打不开界面的原因: Loaded事件中加以下判断条件:if (!DesignerProperties.GetIsInDesignMode(this))
- mybatis 思考
https://my.oschina.net/xianggao/blog/548579 https://my.oschina.net/xianggao/blog/548873 https://my.o ...
- libmysqlclient.so.16: cannot open shared object file: No such file or directory
编译安装的mysql5.6.39,安装目录是/usr/local/mysql,启用程序时报错:libmysqlclient.so.16: cannot open shared object file: ...
- PHP从入门到精通(二)
PHP从入门到精通 之PHP中的函数 各位开发者朋友大家好,自上次更新PHP的相关知识,得到了大家的广泛支持.PHP的火爆程度不言而喻,函数作为PHP中极为重要的部分,应诸位的支持,博主继续跟进更新 ...
- github的使用心得
我的github地址:https://github.com/gaino1/test GitHub 是一个用于使用Git版本控制系统的项目的基于互联网的存取服务. GitHub可以托管各种git库,并提 ...
- realm vs. domain
从wiki的角度:https://wikidiff.com/domain/realm domain是物理的,realm是抽象的,都是领域. domain是一个人或组织拥有或控制的地理区域,而realm ...
- [转帖]知乎专栏:正确使用 Docker 搭建 GitLab 只要半分钟
正确使用 Docker 搭建 GitLab 只要半分钟 https://zhuanlan.zhihu.com/p/49499229 很多程序员在内网搭建 gitlab 都搭建的坑坑洼洼,不支持 htt ...
- rgb & rgba convert
rgb & rgba convert RGB color to Hex, Pantone, RAL, HSL, HSV, HSB, JSON. Get color scheme. https: ...
- Performance testing test scenarios
1 check if page load time is within acceptable range2 check page load on slow connections 3 check re ...