在一个connection上可以创建多个Statement 并且同时执行不同的SQL语句(这句话到底对不对? )

oneonone oneonone 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
曾向treeroot请教此问题,他说后半句是不对的,因为在多线程情况下是不能同时执行的.
我用程序试过了,确实可以.
各位高手也可以帮忙解答,多谢!~

附我的测试代码,有些乱
__________________

package testtitles;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.lang.Thread;
import java.sql.PreparedStatement;

public class TestOracle {
public static int i = 0;

public static void main(String[] args) {
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@138.54.252.68:1521:ora", "system",
"admin");
con.setAutoCommit(true);
if(con.isClosed()) System.out.println("Connection closed");

Thread t1 = new TestSZThread(con,1);
Thread t2 = new TestHKThread(con,1);
Thread t3 = new TestSZThread(con,1);
Thread t4 = new TestHKThread(con,1);
Thread t5 = new TestSZThread(con,1);
Thread t6 = new TestHKThread(con,1);
Thread t7 = new TestSZThread(con,1);
Thread t8 = new TestHKThread(con,1);


} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Main Exception");
e.printStackTrace();
try {
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

return;
}
}
private String sample = "Start value";

//Access sample property
public String getSample() {
return sample;
}
public boolean mysql2SqlServlerTransform() {
return false;
}

}

class TestSZThread extends Thread {
Connection con = null;
long sleepMillSec = 0;
public TestSZThread(Connection con, long sleepMillSec) {
this.con = con;
try {
if(con.isClosed()) System.out.println("Connection closed1");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.sleepMillSec = sleepMillSec;
this.start();
}

public void run() {
while (true) {
try {
if(con.isClosed()) System.out.println("Connection closed1");
PreparedStatement stmt = con
.prepareStatement("update info.t_userinfo set marktid=? where subscrbid='U010000035'");
stmt.setString(1, "SZ");
stmt.executeUpdate();
stmt.close();
System.out.println("update to SZ " + ++TestOracle.i);
} catch (Exception e) {
System.out.println("SZ Exception "+TestOracle.i);
e.printStackTrace();
break;
}

try {
Thread.sleep(sleepMillSec);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

class TestHKThread extends Thread {
Connection con = null;
long sleepMillSec = 0;
public TestHKThread(Connection con, long sleepMillSec) {
this.con = con;
this.sleepMillSec = sleepMillSec;
this.start();
}

public void run() {
while (true) {
try {
PreparedStatement stmt = con
.prepareStatement("update info.t_userinfo set marktid=? where subscrbid='U010000035'");
System.out.println("update to HK "+ ++TestOracle.i);
stmt.setString(1, "HK");
stmt.executeUpdate();
stmt.close();

} catch (Exception e) {
System.out.println("HK Exception "+TestOracle.i);
e.printStackTrace();
break;
}

try {
Thread.sleep(sleepMillSec);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
oneonone
Java SE 62,621 成员 307,251 内容