public void division(){
//获取要导入的总的数据条数
String sql3="SELECT count() FROM [CMD][dbo][mycopy1]";
try {
pss=consprepareStatement(sql3);
rss=pssexecuteQuery();
while(rssnext()){
Systemoutprintln("总记录条数:"+rssgetInt(1));
sum=rssgetInt(1);
}
//每30000条记录作为一个分割点
if(sum>=30000){
n=sum/30000;
residue=sum%30000;
}else{
residue=sum;
}
Systemoutprintln(n+" "+residue);
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
线程类
public MyThread(int start,int end) {
thisend=end;
thisstart=start;
Systemoutprintln("处理掉余数");
try {
Systemoutprintln("--------"+ThreadcurrentThread()getName()+"------------");
ClassforName(SQLSERVERDRIVER);
Systemoutprintln("加载sqlserver驱动");
cons = DriverManagergetConnection(CONTENTS,UNS,UPS);
stas = conscreateStatement();
Systemoutprintln("连接SQLServer数据库成功!!");
Systemoutprintln("加载mysql驱动");
ClassforName(MYSQLDRIVER);
con = DriverManagergetConnection(CONTENT,UN,UP);
sta = concreateStatement();
// 关闭事务自动提交
consetAutoCommit(false);
Systemoutprintln("连接mysql数据库成功!!");
} catch (Exception e) {
eprintStackTrace();
}
// TODO Auto-generated constructor stub
}
public ArrayList<Member> getAll(){
Member member;
String sql1="select from (select row_number() over (order by pmcode) as rowNum," +
" from [CMD][dbo][mycopy1]) as t where rowNum between "+start+" and "+end;
try {
Systemoutprintln("正在获取数据");
allmembers=new ArrayList();
rss=stasexecuteQuery(sql1);
while(rssnext()){
member=new Member();
membersetAddress1(rssgetString("address1"));
membersetBnpoints(rssgetString("bnpoints"));
membersetDbno(rssgetString("dbno"));
membersetExpiry(rssgetString("expiry"));
membersetHispoints(rssgetString("hispoints"));
membersetKypoints(rssgetString("kypoints"));
membersetLevels(rssgetString("levels"));
membersetNames(rssgetString("names"));
membersetPmcode(rssgetString("pmcode"));
membersetRemark(rssgetString("remark"));
membersetSex(rssgetString("sex"));
membersetTelephone(rssgetString("telephone"));
membersetWxno(rssgetString("wxno"));
membersetPmdate(rssgetString("pmdate"));
allmembersadd(member);
// Systemoutprintln(membergetNames());
}
Systemoutprintln("成功获取sqlserver数据库数据!");
return allmembers;
} catch (SQLException e) {
// TODO Auto-generated catch block
Systemoutprintln("获取sqlserver数据库数据发送异常!");
eprintStackTrace();
}
try {
rssclose();
stasclose();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
return null;
}
public void inputAll(ArrayList<Member> allmembers){
Systemoutprintln("开始向mysql中写入");
String sql2="insert into testmycopy2 values (,,,,,,,,,,,,,)";
try {
ps=conprepareStatement(sql2);
Systemoutprintln("-------------------------等待写入数据条数: "+allmemberssize());
for(int i=0;i<allmemberssize();i++){
pssetString(1, allmembersget(i)getPmcode());
pssetString(2, allmembersget(i)getNames());
//Systemoutprintln(allmembersget(i)getNames());
pssetString(3, allmembersget(i)getSex());
pssetString(4, allmembersget(i)getTelephone());
pssetString(5, allmembersget(i)getAddress1());
pssetString(6, allmembersget(i)getPmdate());
pssetString(7, allmembersget(i)getExpiry());
pssetString(8, allmembersget(i)getLevels());
pssetString(9, allmembersget(i)getDbno());
pssetString(10, allmembersget(i)getHispoints());
pssetString(11, allmembersget(i)getBnpoints());
pssetString(12, allmembersget(i)getKypoints());
pssetString(13, allmembersget(i)getWxno());
pssetString(14, allmembersget(i)getRemark());
//插入命令列表
//psaddBatch();
psexecuteUpdate();
}
//psexecuteBatch();
concommit();
psclose();
conclose();
thisflag=false;
Systemoutprintln(ThreadcurrentThread()getName()+"--->OK");
} catch (SQLException e) {
// TODO Auto-generated catch block
Systemoutprintln("向mysql中更新数据时发生异常!");
eprintStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true&&flag){
thisinputAll(getAll());
}
}
一、使用工具:java语言、Myeclipse。
二、 *** 作步骤:
1、第一步:加载MySQL的JDBC的驱动
2、第二步:创建与MySQL数据库的连接类的实例
3、第三步:获取连接类实例con,用con创建Statement对象类实例 sql_statement
4、第四步:执行查询,用ResultSet类的对象,返回查询的结果
5、得出数据
三、注意事项:有几处是需要根据自身情况修改的
1、如下图中的url和账号,密码需要与你自己的相一致。
2、这些需要访问的数据必须要与数据库中的类型相互匹配,才能打印出正确的结果。
以上就是关于java怎么多线程 *** 作一个数据库全部的内容,包括:java怎么多线程 *** 作一个数据库、如何在Java程序中访问mysql数据库中的数据并进行简单的 *** 作、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)