Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Java operating the database?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the method of operating the database by Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Pessimistic lock (also called row-level lock)

During the execution of this transaction, the record we specified is queried, and the record will be locked during the process of my query. No one or any transaction can modify the query data specified by me (can not be changed, but can see). Until I finish the query.

1. Use pessimistic locks (used in sql statements in transactions)

/ / sql instruction String sql = "select * from t_shuihuo where id"

< ? for update ";2..完整代码 package com.luosf.jdbc; import com.luosf.jdbc.utils.JdbcUtil; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException; /** * JDBC中锁的使用 * for updata */public class JdbcLock { public static void main(String[] args) { Connection conn = null; PreparedStatement stat = null; ResultSet res = null; try { //创建驱动 //获取数据库对象 conn = JdbcUtil.getConnection(); //sql指令 String sql = "select * from t_shuihuo where id < ? for update "; conn.setAutoCommit(false);//开启事务 //3,sql语句进行编译 stat = conn.prepareStatement(sql); //给占位符填充值 //JDBC下标从1开始的 stat.setInt(1,16); //1,代表第一个问号 Thread.sleep(1000*10); //模拟访问时间 //4,执行sql res = stat.executeQuery(); //5,处理查询结果集 while (res.next()){ int id = res.getInt("id"); String name = res.getString("name"); String nickname = res.getString("nickname"); System.out.println("id :"+ id + " name :" +name + " 昵称 :"+nickname); } conn.commit();//提交事务 } catch (SQLException throwables) { try { if (conn != null){ conn.rollback(); //回滚事务 } } catch (SQLException e) { e.printStackTrace(); } throwables.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally{ //释放资源 JdbcUtil.close(conn,stat,res); } }}3..测试代码 package com.luosf.jdbc; import com.luosf.jdbc.utils.JdbcUtil; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException; /** * 检测锁 */public class JdbcLockTest { public static void main(String[] args) { Connection conn = null; PreparedStatement stat = null; try { //获取驱动 //获取数据库链接对象 conn = JdbcUtil.getConnection(); //开启事务 conn.setAutoCommit(false); //锁开始后进行修改数据 String sql = "update t_shuihuo set name = '小罗' where id = ? "; stat = conn.prepareStatement(sql); stat.setInt(1,10); //1,代表第一个问号 int cunt = stat.executeUpdate(); System.out.println("更新了"+cunt+"条数据"); conn.commit();//提交事务 } catch (SQLException throwables) { try { if (conn != null){ conn.rollback(); } } catch (SQLException e) { e.printStackTrace(); } throwables.printStackTrace(); } finally { //释放资源 JdbcUtil.close(conn,stat,null); } }} 需要等锁等待时间完成才能进行修改

This is the end of the content of "what is the method of Java operating the database". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report