Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何提高数据从java应用到数据库的传输速度?_Java_Mysql_Jdbc_Garbage Collection - Fatal编程技术网

如何提高数据从java应用到数据库的传输速度?

如何提高数据从java应用到数据库的传输速度?,java,mysql,jdbc,garbage-collection,Java,Mysql,Jdbc,Garbage Collection,在我的应用程序中,我从文件中提取数据并传输到数据库。我有40万张唱片。首先,它可以快速传输数据,最多传输10000条记录,然后更新速度非常慢。硬件如何提高向数据库传输数据的性能 gc有什么问题吗 这是我的代码: package com.fileupload; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java

在我的应用程序中,我从文件中提取数据并传输到数据库。我有40万张唱片。首先,它可以快速传输数据,最多传输10000条记录,然后更新速度非常慢。硬件如何提高向数据库传输数据的性能

gc有什么问题吗

这是我的代码:

package com.fileupload;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.text.ZoneView;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class SendDataToDb extends HttpServlet{
PreparedStatement ps = null;
HttpSession hs;
Connection con1;
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

     hs = request.getSession(false);
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/xlsx","root","Inf123#");
        ps = con1.prepareStatement("INSERT INTO userdetails(ID, NAME, AGE, GENDER,ADDRESS, ZONEID, LOCATION) VALUES(?, ?, ?, ?, ?, ?, ?)");

    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    try {
        processOneSheet("C:/Users/Penchalaiah/Desktop/New folder/"+hs.getAttribute("filename1"));
        System.out.println("clossing the connnection");
        ps.close();
        con1.close();
    } catch (Exception e) {
        e.printStackTrace();
    }   

}

public void processOneSheet(String filename) throws Exception {
    OPCPackage pkg = OPCPackage.open(filename);
    XSSFReader r = new XSSFReader( pkg );
    SharedStringsTable sst = r.getSharedStringsTable();

    XMLReader parser = fetchSheetParser(sst);

    // To look up the Sheet Name / Sheet Order / rID,
    //  you need to process the core Workbook stream.
    // Normally it's of the form rId# or rSheet#
    InputStream sheet2 = r.getSheet("rId2");
    InputSource sheetSource = new InputSource(sheet2);
    parser.parse(sheetSource);
    sheet2.close();
}

public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {
    XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
    ContentHandler handler = new SheetHandler(sst);
    parser.setContentHandler(handler);
    return parser;
}

/** 
 * See org.xml.sax.helpers.DefaultHandler javadocs 
 */
private  class SheetHandler extends DefaultHandler {

    private SharedStringsTable sst;
    private String lastContents;
    private boolean nextIsString;
    String id;
    String names;
    String age;
    String gender;
    String address;

    int i = 1;

    private SheetHandler(SharedStringsTable sst) {
        this.sst = sst;

    }

    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        // c => cell
        if(name.equals("c")) {
            // Print the cell reference
            // Figure out if the value is an index in the SST
            String cellType = attributes.getValue("t");
            if(cellType != null && cellType.equals("s")) {
                nextIsString = true;
            } else {
                nextIsString = false;
            }
        }
        // Clear contents cache
        lastContents = "";
        //System.out.println("===>"+lastContents+"<====");
    }

    public void endElement(String uri, String localName, String name)
            throws SAXException {
        // Process the last contents as required.
        // Do now, as characters() may be called more than once
        if(nextIsString) {
            int idx = Integer.parseInt(lastContents);
            lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
            nextIsString = false;

        }

        // v => contents of a cell
        // Output after we've seen the string contents




        if(name.equals("v")) {
            System.out.print(lastContents+"\t");

            if(i == 1){
                id = lastContents;

                System.out.print(lastContents+"("+i+")");
            }
            if(i == 2){
                names = lastContents;

                System.out.print(lastContents+"("+i+")");
            }
            if(i == 3){
                age = lastContents;

                System.out.print(lastContents+"("+i+")");
            }
            if(i == 4){
                gender = lastContents;
                System.out.print(lastContents+"("+i+")");
            }
            if(i == 5){
                address = lastContents;

                System.out.print(lastContents+"("+i+")");
                insertInToDb(id, names, age, gender, address);
                i = 0;
            }

            i++;


        }

    }


    public void characters(char[] ch, int start, int length)
            throws SAXException {
        lastContents += new String(ch, start, length);
    }
}

    public void insertInToDb(String id,String names,String age, String gender,String address){

        try {

            ps.setString(1, id);
            ps.setString(2, names);
            ps.setString(3, age);
            ps.setString(4, gender);
            ps.setString(5, address);
            ps.setString(6, (String)hs.getAttribute("zoneId1"));
            ps.setString(7, (String)hs.getAttribute("location1"));
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
package com.fileupload;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.util.Iterator;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入javax.servlet.http.HttpSession;
导入javax.swing.text.ZoneView;
导入org.apache.poi.openxml4j.opc.OPCPackage;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入java.io.InputStream;
导入java.util.Iterator;
导入org.apache.poi.xssf.eventusermodel.XSSFReader;
导入org.apache.poi.xssf.model.SharedStringsTable;
导入org.apache.poi.xssf.usermodel.XSSFRichTextString;
导入org.apache.poi.openxml4j.opc.OPCPackage;
导入org.xml.sax.Attributes;
导入org.xml.sax.ContentHandler;
导入org.xml.sax.InputSource;
导入org.xml.sax.SAXException;
导入org.xml.sax.XMLReader;
导入org.xml.sax.helpers.DefaultHandler;
导入org.xml.sax.helpers.XMLReaderFactory;
公共类SendDataToDb扩展了HttpServlet{
PreparedStatement ps=null;
httphs;
连接con1;
@凌驾
public void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
hs=request.getSession(false);
试一试{
Class.forName(“com.mysql.jdbc.Driver”);
con1=DriverManager.getConnection(“jdbc:mysql://localhost:3306/xlsx“,”根“,”Inf123#“;”;
ps=con1.prepareStatement(“插入用户详细信息(ID、姓名、年龄、性别、地址、区域ID、位置)值(?,,,,,,,,,?)”;
}捕获(ClassNotFoundException e1){
e1.printStackTrace();
}捕获(SQLE异常){
e、 printStackTrace();
}
试一试{
processOneSheet(“C:/Users/Penchalaiah/Desktop/newfolder/”+hs.getAttribute(“filename1”);
System.out.println(“关闭连接”);
ps.close();
con1.close();
}捕获(例外e){
e、 printStackTrace();
}   
}
public void processOneSheet(字符串文件名)引发异常{
OPCPackage pkg=OPCPackage.open(文件名);
XSSFReader r=新XSSFReader(pkg);
SharedStringsTable sst=r.getSharedStringsTable();
XMLReader parser=fetchSheetParser(sst);
//要查找图纸名称/图纸顺序/rID,
//您需要处理核心工作簿流。
//通常它的形式是rId#或rSheet#
InputStream sheet2=r.getSheet(“rId2”);
InputSource sheetSource=新的InputSource(sheet2);
parser.parse(sheetSource);
表2.关闭();
}
公共XMLReader fetchSheetParser(SharedStringsTable sst)引发SAXException{
XMLReader parser=XMLReaderFactory.createXMLReader(“org.apache.xerces.parsers.SAXParser”);
ContentHandler=新的SheetHandler(sst);
setContentHandler(handler);
返回解析器;
}
/** 
*请参阅org.xml.sax.helpers.DefaultHandler javadocs
*/
私有类SheetHandler扩展了DefaultHandler{
私人股本;
私有字符串内容;
私有布尔连接字符串;
字符串id;
字符串名;
弦年龄;
字符串性别;
字符串地址;
int i=1;
专用SheetHandler(SharedStringsTable sst){
这个.sst=sst;
}
public void startElement(字符串uri、字符串localName、字符串name、,
属性)引发SAX异常{
//c=>单元
如果(名称等于(“c”)){
//打印单元格引用
//确定该值是否为SST中的索引
字符串cellType=attributes.getValue(“t”);
if(cellType!=null&&cellType.equals(“s”)){
nextIsString=true;
}否则{
nextiststring=false;
}
}
//清除内容缓存
lastContents=“”;

//System.out.println(“==>”+lastContents+”对多个记录执行一个批处理操作要比对多个记录执行每个插入查询快得多

您可以创建一个10000或任意数量的批,然后执行该批

 Connection con = null;
        PreparedStatement pstm = null;
        try {
            Class.forName("driver class");
            con = DriverManager.
                    getConnection("connectionUrlString","password");
            con.setAutoCommit(false);
            pstm = con.prepareStatement("your insert command );
            pstm .setInt(1, 3000); //set all parameters            
            pst.addBatch();
            int count[] = pst.executeBatch();
            for(int i=1;i<=count.length;i++){
                System.out.println("Query "+i+" has effected "+count[i]+" records");
            }
            con.commit();
            pst.close();
            con.close();
连接con=null;
PreparedStatement pstm=null;
试一试{
forName类(“驱动程序类”);
con=驾驶员管理器。
getConnection(“connectionUrlString”、“密码”);
con.setAutoCommit(假);
pstm=con.prepareStatement(“插入命令”);
setInt(1,3000);//设置所有参数
pst.addBatch();
int count[]=pst.executeBatch();

对于(int i=1;i)执行这段代码现在需要多长时间?您希望通过说“提高速度”在时间上有哪些改进?这需要30分钟以上,我需要在时间上有所改进您是否尝试过先阅读所有内容并收集insert语句,然后执行批处理操作?