服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java多线程数据库操作


  import java.io.*;

import java.sql.*;

public class tooracle {

public static void main(string[] args) {

int n = 10;//每个线程执行的个数

//system.out.println("start action!!!!!!");

tooracle ac = new tooracle();

threadrun[] testrun = new threadrun[21];

for (int i = 1; i <= 21; i++) {

testrun[i] = ac.new threadrun();

testrun[i].trun(n);

system.out.println("线程" + i + "已经启动");

}

//testrun[1] = ac.new threadrun();

//testrun[1].trun(10);

for (int i = 1; i <= 21; i++) {

testrun[i].texit();

}

}

class threadrun {

string blobname = "d:/eclipse/test/test1.txt"; //blob文件名

string clobname = "d:/eclipse/test/test2.txt"; //clob文件名

string in = "insert into ";

string in1 = "(order_id,ric_code,siz,price,trade_datetime,status,testblob,testclob) values(9,´asdfjkl´,21,123.34567,sysdate,´nill逆耳´,?,?)";

string tablename = "orders1";

string sqlstr = ""; // sql 语句

threaduseextends thread = new threaduseextends(blobname, clobname,

sqlstr);

public void trun(int n) {

sqlstr = in + tablename + in1;

thread = new threaduseextends(blobname, clobname, sqlstr);

//thread.start();

thread.run(n);

/*

* int n=10; for (int i = 1; i <= 21; i++) { while(0 <n){ sqlstr[i] =

* in + tablename + in1; thread[i] = new threaduseextends(blobname,

* clobname, sqlstr[i]); thread[i].start(); n--; }

* system.out.println("线程" + i + "已经启动"); }

*/

//system.out.println("线程1已经启动");

}

public void texit() {

thread.interrupt();

}

class threaduseextends extends thread {

string filename1;//blob filename

string filename2;//clob filename

string str;

//构造函数要有(blob文件名,clob文件名,sql语句)

public threaduseextends(string name1, string name2, string sqlstr) {

filename1 = name1;

filename2 = name2;

str = sqlstr;

}

readfiles r1 = new readfiles();

public void run(int n) {

while (n > 0) {

try {

string url = "jdbc:oracle:thin:@193.9.200.111:1521:ora9i";

string login = "system"; // use your login here

string password = "ti2003"; // use your password here

class.forname("oracle.jdbc.driver.oracledriver");

connection con = drivermanager.getconnection(url,

login, password);

string testlong = r1.readfile(filename1);

string testlong1 = r1.readfile(filename2);

byte[] ba = testlong.getbytes();

system.out.println("str=" + str);

//string strsql = str; //"insert into

// orders(order_id,ric_code,siz,price,trade_datetime,status,testblob,testclob)

// values(8,´asdfjkl´,21,123.34567,sysdate,´nill逆耳´,?,?)";

preparedstatement stm = con.preparestatement(str);

stm.setbytes(1, ba);

stringreader test = new stringreader(testlong1);

stm.setcharacterstream(2, test, testlong.length());

stm.execute();

stm.close();

con.close();

} catch (exception e) {

e.printstacktrace();

}

n--;

}//while

}//run

}//threaduseextends class

// readfiles class for read text!!

class readfiles {

public readfiles() {

}

//readfile method,read file

public string readfile(string filename) {

string name = filename;

string file = "";

try {

filereader readf = new filereader(name);//读文件

bufferedreader huanchong = new bufferedreader(readf);//文件读缓冲.

try {

file = huanchong.readline();

} catch (ioexception e1) {

// todo 自动生成 catch 块

e1.printstacktrace();

}

} catch (filenotfoundexception e) {

// todo 自动生成 catch 块

e.printstacktrace();

}

//system.out.println("文件:"+file);

return file;

}

}//readfiles class

}//threadrun class

}//tooracle class

扫描关注微信公众号