public void division(){
//获取要导入的总的数据条数
String sql3="SELECT count(*) FROM [CMD].[dbo].[mycopy1]"
try {
pss=cons.prepareStatement(sql3)
rss=pss.executeQuery()
while(rss.next()){
System.out.println("总记录条数:"+rss.getInt(1))
sum=rss.getInt(1)
}
//每30000条记录作为一个分割点
if(sum>=30000){
n=sum/30000
residue=sum%30000
}else{
residue=sum
}
System.out.println(n+" "+residue)
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
线程类
public MyThread(int start,int end) {
this.end=end
this.start=start
System.out.println("处理掉余数")
try {
System.out.println("--------"+Thread.currentThread().getName()+"------------")
Class.forName(SQLSERVERDRIVER)
System.out.println("加载sqlserver驱动...")
cons = DriverManager.getConnection(CONTENTS,UNS,UPS)
stas = cons.createStatement()
System.out.println("连接SQLServer数据库成功!!")
System.out.println("加载mysql驱动.....")
Class.forName(MYSQLDRIVER)
con = DriverManager.getConnection(CONTENT,UN,UP)
sta = con.createStatement()
// 关闭事务自动提交
con.setAutoCommit(false)
System.out.println("连接mysql数据库成功!!")
} catch (Exception e) {
e.printStackTrace()
}
// 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 {
System.out.println("正在获取数据...")
allmembers=new ArrayList()
rss=stas.executeQuery(sql1)
while(rss.next()){
member=new Member()
member.setAddress1(rss.getString("address1"))
member.setBnpoints(rss.getString("bnpoints"))
member.setDbno(rss.getString("dbno"))
member.setExpiry(rss.getString("expiry"))
member.setHispoints(rss.getString("hispoints"))
member.setKypoints(rss.getString("kypoints"))
member.setLevels(rss.getString("levels"))
member.setNames(rss.getString("names"))
member.setPmcode(rss.getString("pmcode"))
member.setRemark(rss.getString("remark"))
member.setSex(rss.getString("sex"))
member.setTelephone(rss.getString("telephone"))
member.setWxno(rss.getString("wxno"))
member.setPmdate(rss.getString("pmdate"))
allmembers.add(member)
// System.out.println(member.getNames())
}
System.out.println("成功获取sqlserver数据库数据!")
return allmembers
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("获取sqlserver数据库数据发送异常!")
e.printStackTrace()
}
try {
rss.close()
stas.close()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return null
}
public void inputAll(ArrayList<Member>allmembers){
System.out.println("开始向mysql中写入")
String sql2="insert into test.mycopy2 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
try {
ps=con.prepareStatement(sql2)
System.out.println("-------------------------等待写入数据条数: "+allmembers.size())
for(int i=0i<allmembers.size()i++){
ps.setString(1, allmembers.get(i).getPmcode())
ps.setString(2, allmembers.get(i).getNames())
//System.out.println(allmembers.get(i).getNames())
ps.setString(3, allmembers.get(i).getSex())
ps.setString(4, allmembers.get(i).getTelephone())
ps.setString(5, allmembers.get(i).getAddress1())
ps.setString(6, allmembers.get(i).getPmdate())
ps.setString(7, allmembers.get(i).getExpiry())
ps.setString(8, allmembers.get(i).getLevels())
ps.setString(9, allmembers.get(i).getDbno())
ps.setString(10, allmembers.get(i).getHispoints())
ps.setString(11, allmembers.get(i).getBnpoints())
ps.setString(12, allmembers.get(i).getKypoints())
ps.setString(13, allmembers.get(i).getWxno())
ps.setString(14, allmembers.get(i).getRemark())
//插入命令列表
//ps.addBatch()
ps.executeUpdate()
}
//ps.executeBatch()
con.commit()
ps.close()
con.close()
this.flag=false
System.out.println(Thread.currentThread().getName()+"--->OK")
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("向mysql中更新数据时发生异常!")
e.printStackTrace()
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true&&flag){
this.inputAll(getAll())
}
}
#include <QCoreApplication>#include "thread.h"
#include <QVector>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv)
QVector<Thread*>vector
Thread *thread
//创建多个线程,并start
for(int i=0i<10i++){
thread=new Thread
vector.append(thread)
thread->set(i)
thread->start()
}
//等待所有线程执行完,然后删除线程
foreach(thread,vector){
thread->wait()
}
foreach(thread,vector){
delete thread
}
return a.exec()
}
-------------------------------------------------------------------------------------------------
#include "thread.h"
Thread::Thread(QObject *parent) : QThread(parent)
{
}
void Thread::run()
{
begin()
}
//为每个线程创建一个连接名
void Thread::set(int a)
{
connectionName=QString::number(a)
}
void Thread::connectionDatabase(QString dbName)
{
QSqlDatabasedb=QSqlDatabase::addDatabase("QMYSQL",connectionName)
db.setHostName("localhost")
db.setDatabaseName(dbName)
db.setUserName("root")
db.setPassword("")
if(!db.open())
qDebug()<<"db open fail"
}
void Thread::begin()
{
QString dbName="learnsql"
connectionDatabase(dbName)
QSqlDatabase db=QSqlDatabase::database(connectionName)
db.transaction()//开启事物
QSqlQuery query(db)
//向表student中插入10000条数据
for(int i=1i<=10000i++){
query.exec("insert into student values(1)")
}
db.commit()//提交事物
}
多线程访问数据库,其中有一个线程会长时间占用数据库。这个线程是独立线程,另外有一个线程池也会访问数据库,这个线程池中的线程我用lock锁住数据库了,但是由于独立线程和线程池线程不在同一个方法中,所以访问数据库的时间不同,用lock只能对线程池线程有效 回答: 你这么讲一定能保证两个线程不会在一个时刻共同发起对数据库的访问么?除非你做了很多同步让两个线程都按照你设计的逻辑不发生访问数据库的冲突,否则你必须加锁。看你的说明,你应该没加同步,所以他会出现冲突,你不能缺锁,他是共享资源的访问了欢迎分享,转载请注明来源:内存溢出
评论列表(0条)