所以我正在使用 Java 在 SQL 中创建一个数据库,但我一直遇到一个我无法解决的问题。非常感谢一些帮助。我正在使用以下代码将数据读入数据库。我目前将数据库命名为 newTestTable2,它有 4 列:stockID (INT)、millisFromMid (INT)、bidPrice (DOUBLE) 和 askPrice (DOUBLE)。我正在尝试使用准备好的语句向数据库添加新信息,使用 stockID 作为主键,但我遇到了很多麻烦。这是代码:
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.zip.GZIPInputStream;
public class ReadGZippedTAQQuotesFile {
// Header fields
protected int _secsFromEpoch;
protected int _nRecs;
// Record fields
protected int [] _millisecondsFromMidnight;
protected int [] _bidSize;
protected float [] _bidPrice;
protected int [] _askSize;
protected float [] _askPrice;
public int getSecsFromEpoch () { return _secsFromEpoch; }
public int getNRecs () { return _nRecs; }
public int getMillisecondsFromMidnight ( int index ) { return _millisecondsFromMidnight[ index ]; }
public int getBidSize ( int index ) { return _bidSize[ index ]; }
public float getBidPrice ( int index ) { return _bidPrice[ index ]; }
public int getAskSize ( int index ) { return _askSize[ index ]; }
public float getAskPrice ( int index ) { return _askPrice[ index ]; }
public static Connection connect = null;
public static PreparedStatement pst = null;
public static PreparedStatement pst1 = null;
public static PreparedStatement pst2 = null;
public static PreparedStatement pst3 = null;
public static ResultSet resultSet = null;
/**
* Constructor - Opens a gzipped TAQ quotes file and reads entire contents into memory.
*
* @param filePathName Name of gzipped TAQ quotes file to read
* @throws IOException
*/
public ReadGZippedTAQQuotesFile( File filePathName ) throws IOException {
// Open file
InputStream in = new GZIPInputStream( new FileInputStream( filePathName ) );
DataInputStream dataInputStream = new DataInputStream( in );
// Read and save header info
_secsFromEpoch = dataInputStream.readInt();
_nRecs = dataInputStream.readInt();
// Allocate space for data
_millisecondsFromMidnight = new int [ _nRecs ];
_bidSize = new int [ _nRecs ];
_bidPrice = new float [ _nRecs ];
_askSize = new int [ _nRecs ];
_askPrice = new float [ _nRecs ];
// Read all records into memory
for( int i = 0; i < _nRecs; i++ )
_millisecondsFromMidnight[ i ] = dataInputStream.readInt();
for( int i = 0; i < _nRecs; i++ )
_bidSize[ i ] = dataInputStream.readInt();
for( int i = 0; i < _nRecs; i++ )
_bidPrice[ i ] = dataInputStream.readFloat();
for( int i = 0; i < _nRecs; i++ )
_askSize[ i ] = dataInputStream.readInt();
for( int i = 0; i < _nRecs; i++ )
_askPrice[ i ] = dataInputStream.readFloat();
// Finished reading - close the stream
dataInputStream.close();
}
/**
* Example of using this class to read a TAQ quotes file and access
* individual records.
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void quotesReader() throws ClassNotFoundException, SQLException {
File f = new File("/Users/Adam/Desktop/SQL Folder/20070620/20070620");
File[] files = f.listFiles();
try {
// This will load the MySQL driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/newTest", "root", "Kariya"
);
// Statements allow us to issue SQL queries to the database
pst = connect.prepareStatement("INSERT INTO newTestTable2(stockID) VALUES(?)");
pst1 = connect.prepareStatement("INSERT INTO newTestTable2(millisFromMid) VALUES(?)");
pst2 = connect.prepareStatement("INSERT INTO newTestTable2(bidPrice) VALUES(?)");
pst3 = connect.prepareStatement("INSERT INTO newTestTable2(askPrice) VALUES(?)");
// Read entire TAQ quotes file into memory
pst.setInt(1, 0);
pst.executeUpdate();
for( int i=0; i < files.length; i++) {
ReadGZippedTAQQuotesFile taqQuotes = new ReadGZippedTAQQuotesFile( files[i] );
pst.setInt(1, i+1);
pst.executeUpdate();
// Iterate over all records, writing the contents of each to the console
int nRecs = taqQuotes.getNRecs();
for( int j = 0; j < nRecs; j++ ) {
pst1.setInt(1, taqQuotes.getMillisecondsFromMidnight( j ));
pst1.executeUpdate();
pst2.setDouble(1, taqQuotes.getBidPrice( j ));
pst2.executeUpdate();
pst3.setDouble(1, taqQuotes.getAskPrice( j ));
pst3.executeUpdate();
/*
System.out.println(
taqQuotes.getMillisecondsFromMidnight( j )
+ ","
+ taqQuotes.getBidSize( j )
+ ","
+ taqQuotes.getBidPrice( j )
+ ","
+ taqQuotes.getAskSize( j )
+ ","
+ taqQuotes.getAskPrice( j )
);
*/
}
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (pst != null) {
pst.close();
}
if (pst1 != null) {
pst1.close();
}
if (pst2 != null) {
pst2.close();
}
if (pst3 != null) {
pst3.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
}
}
public static void main( String[] args ) throws ClassNotFoundException, SQLException {
quotesReader();
}
}
我得到以下异常
Exception in thread "main"
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Duplicate entry '0' for key 'PRIMARY'"
所以我知道它告诉我我使用同一个股票 ID 的次数太多了,但是如果我不使用以下行:
pst.setInt(1, 0);
pst.executeUpdate();
然后它返回stockID
没有默认值,这看起来也很奇怪,因为 INT 应该有一个默认值 0 对吗?看起来这段代码应该在正确的附近,但我看不出是什么导致了这个错误。任何人都可以分享一些见解吗?