Java 如何在jdbc中使用线程插入数据?

Java 如何在jdbc中使用线程插入数据?,java,multithreading,jdbc,prepared-statement,Java,Multithreading,Jdbc,Prepared Statement,我想使用JDBC插入数据。 我写这段代码: //我想从这里开始 while(stmt_ver.next()){ stmt_ver.setString(i, "test"+... ); stmt_ver.executeBatch(); connection_ver.commit(); } //我想在这里完成线程 我怎样才能用线做这个 new Thread(new Runnable(){ @Override public void

我想使用JDBC插入数据。 我写这段代码:

//我想从这里开始

      while(stmt_ver.next()){ 
        stmt_ver.setString(i, "test"+... ); 
        stmt_ver.executeBatch();
        connection_ver.commit(); 
}
//我想在这里完成线程

我怎样才能用线做这个

new Thread(new Runnable(){ 
@Override public void run(){
//enter code here
   }
}).start();
编辑您要插入多个并行线程

有许多不同的可能性

您应该阅读:并发(并发集合)和执行器

编辑2我同意Thomas Uhrig的观点,在这里引入线程可能弊大于利。
你为什么认为这会有帮助

你的问题很难回答。你问得很含糊。尽量说清楚。张贴所有必要的代码。试着解释一下你做了什么,你想做什么

这里给你一些提示。如果您复制并通过它,它将不会运行,但我认为它应该明确说明您可以尝试什么:

int i = 0;
while(i < columnCount ){

    // make a new statement
    Statement stmt_ver = new Statement();

    // set your data and make the statement ready
    stmt_ver.set...

    // make a new thread that executes your data
    // and let it run
    new Thread(){ 

        public void run(){

            stmt_ver.addBatch();  
            stmt_ver.executeBatch();
            connection_ver.commit(); 
        }
    }.start();

    i++; 
}
inti=0;
while(i

这是一个非常简单的解决方案。它将在每次迭代中启动一个线程。由于I/O通常需要一些时间,因此这可以提高代码的执行时间。但是要注意,线程化并不容易。这是一个非常简单、天真的解决方案它可能会导致比它解决的问题更多的问题。如果您不熟悉线程(而且似乎不熟悉),请不要这样做

给你。用代码更新答案

螺纹类

public class MyThreadedClass extends Thread{

     //Do what I need here on a thread
     public void run(){
         //Do what I need here
     }
}
主要

//主类
公共静态类MyProgramMain{
//主程序
公共静态void main(字符串[]args){
//发送10个线程
对于(inti=0;i
public类MockCommonDao){
ArrayList listOlists=新的ArrayList();
公共列表CommonInsert(列表示例)
{
List listOlists=null;
ExecutorService executor=Executors.newFixedThreadPool(例如.size());
List callingList=new ArrayList();
对于(int i=0;i

此代码将根据要为insert创建的线程的值进行simualtaneous insert确定要执行的插入操作的数量。希望你是这个意思。

你想做什么?对我来说似乎有点胡说八道-无意冒犯。但我打赌这段代码不是你想要的。我想在JDBC中插入数据。但我想用线程而不是单个线程来完成。它能用单个线程来完成吗?我打赌不会。是的它可以在没有线程结构的情况下工作。这不是完整的代码,我想用线程插入。我怎么能这样做?一个。哦,我明白了。你想用多个线程并行插入?谢谢回答。例如,我想做10个线程。我怎么能像你的代码那样做呢?我仍然确信,迭代不会做OP想要的。什么时候将i增加1,它将把字符串设置为上次迭代中日期的值。加上语句中的索引将增加???如果在他执行时这起作用,那么实际的代码看起来一定非常不同…是的,我同意。所有的stmt\u ver.set…看起来很奇怪。但是因为他没有发布更多的代码,而且他说在它在一个线程中工作时,我只能重新发布自己的代码。谢谢你的回答。例如,我想做10个线程。我怎么能像你的代码那样做呢?谢谢你的回答。想一想:对于int I…100,但我想创建10个线程。那么,既然类是一个线程,在你实例化类的任何时候,我怎么做呢调用.start()方法,您正在创建一个新线程。除此之外,这取决于您要做10次(可能是在for循环中,等等)。我做不到。请给我一些例子。
//Main class
public static class MyProgramMain{

//Program main
public static void main(String[] args) {


                //Send 10 threads
        for (int i=0; i<10; i++){

                  //Init class (threaded)
                   MyThreadedClass threadedClass = new MyThreadedClass();

                   //Execute code in the class run() method
                   threadedClass.start();
            }
    }
}
public class MockCommonDao  {

     ArrayList<ArrayList> listOlists = new ArrayList<ArrayList>();

     public List CommonInsert(List<Object> example)
        {
         List<Future<Object>> listOlists = null;
         ExecutorService executor = Executors.newFixedThreadPool(example.size());
         List<TransactionImpl> callingList = new ArrayList<MockCommonDao.TransactionImpl>();
         for (int i = 0; i < example.size(); i++) {
             TransactionImpl localImpl = new TransactionImpl(example.get(i));
             callingList.add(localImpl);
         }
         try {

             listOlists = executor.invokeAll(callingList);

         } catch (InterruptedException e) {

         }
         return listOlists;





        }
private class TransactionImpl implements Callable<Object>{
          private Object example;


          TransactionImpl(Object Criteria) {
                this.example = Criteria;

            }


            @Override
            public Object call() throws Exception {

            private class TransactionImpl implements Callable<Object>{
          private Object example;


          TransactionImpl(Object Criteria) {
                this.example = Criteria;

            }


            @Override
            public Object call() throws Exception {

            while(stmt_ver.next()){ 
        stmt_ver.setString(i, "test"+... ); 
        stmt_ver.executeBatch();
        connection_ver.commit(); 
}
      }
      }}
      }