在一个connection上可以创建多个Statement 并且同时执行不同的SQL语句(这句话到底对不对? )
曾向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();
}
}
}
}
我用程序试过了,确实可以.
各位高手也可以帮忙解答,多谢!~
附我的测试代码,有些乱
__________________
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();
}
}
}
}
...全文
相关推荐
Java使用Statement接口执行SQL语句操作实例分析
主要介绍了Java使用Statement接口执行SQL语句操作,结合实例形式详细分析了Java使用Statement接口针对mysql数据库进行连接与执行SQL语句增删改查等相关操作技巧与注意事项,需要的朋友可以参考下
ubunut中MD5校验命令之md5sum
是一个用于计算文件 MD5 哈希值的命令,通俗来说就是对该压缩包进行MD5校验,验证网络传输文档的完整性。最后的结果一般都是由32位十六进制构成:d41d8cd98f00b204e9800998ecf8427e。再与发送机上该压缩包的MD5校验码相比较,如果一样,则说明该压缩包传输完成,没有被篡改或者传输丢失。其中,[选项] 是可选参数,用于控制输出格式和行为;[文件名] 是要计算 MD5 哈希值的文件。-c:检查文件的 MD5 哈希值是否与给定的哈希值匹配。-r:递归处理目录及其子目录中的所有文件。
access 一次执行多条sql语句
access 一次执行多条sql语句! 值得下载看看!资源免费,大家分享!! 更多免费资源 http://ynsky.download.csdn.net/
oracle 中使用批处理文件,同时执行,多个.sql文件。
NULL 博文链接:https://zhouhaitao.iteye.com/blog/954129
针对SQL Server将多个SQL语句作为一个执行
本文介绍了几种如何使用一个SqlCommand执行多个SQL语句的技术。


Java SE
