Get the App
SLTechnology News&Howtos  ›  Database  › 

Java concurrently inserts data into oracle

Shulou Source: shulou.com Published: 2022-06-01 08:24:06 09月24日 Update

Technical bulls, please consult java multithreading problem.

Due to the needs of the project, you need to migrate the table records from the oracle to the dept2, and the dept and dept2 table structures are the same.

Imagine:

Query:

Statement stmt = conn.createStatement (

ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)

ResultSet rs = stmt.executeQuery ("select * from dept")

The total number of records is obtained through rs.last ();, and then all records are migrated by multithreading through this total number.

Insert:

PreparedStatement pstmt = null

String psql = "insert into dept2 values (?)"

Pstmt = conn.prepareStatement (psql)

Where index is the index value of the rs result set. When index=1, it represents the first value of rs.next ().

Rs.absolute (index)

Pstmt.setInt (1, rs.getInt (1))

Pstmt.setString (2, rs.getString (2))

Pstmt.setString (3, rs.getString (3))

Pstmt.addBatch ()

I do not know much about multithreading, tried many times, failed to achieve multithreading migration data, hope to have expert advice. Thank you!

Public class ConcurrentTestPreparedStmt

{

Public static int totalRowNum

Public static int index = 1

Public static boolean isFinish

Public static void main (String [] args) throws Exception

{

Class.forName ("oracle.jdbc.driver.OracleDriver")

String url = "jdbc:oracle:thin:@192.168.5.201:1521:orcl"

String user = "scott"

String password = "tiger"

String sql = "select * from dept"

Connection conn = DriverManager.getConnection (url, user, password)

/ * *

* read

, /

Statement stmt = conn.createStatement (

ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)

ResultSet rs = stmt.executeQuery (sql)

/ * *

* write

* use batch processing

, /

PreparedStatement pstmt = null

String psql = "insert into testpstmt values (?)"

Pstmt = conn.prepareStatement (psql)

/ / rs.next as long as it is true, indicate the number of records in the table

Rs.last ()

TotalRowNum = rs.getRow ()

Int I = 0

/ / where the problem lies, when I

Tags: Thread number problem data security one line two enterprise address location technology high permission framework source code purpose index structure result project Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft macOS NVidia vpn Redmi