JDBC插入数据使用索引与否的差别

代码:

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
public class Compare {

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException, SQLException {
		try (BufferedReader fis = new BufferedReader(new FileReader("D:\\冰与火之歌.txt"))){
			char[] buffer = new char[5];
			int len,count=0;
			String str=null;
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
			Statement stmt = conn.createStatement();
			long begintime = System.currentTimeMillis();
			while((len=fis.read(buffer))!=-1){
				str = String.valueOf(buffer);
				stmt.executeUpdate("insert into test_2 values("+count+",'"+str+"')"); //test_1使用索引, test_2反之
				count++;
				if (count==100000) break;
			}
			long endtime = System.currentTimeMillis();
			System.out.println("耗时:"+(endtime-begintime)+"毫秒");
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

test_1耗时:6980毫秒

test_2耗时:6703毫秒

不知道口乍回事,明天改

你可能感兴趣的:(Java)