gpt4 book ai didi

java - 使用 MySQL 和 Java

转载 作者:行者123 更新时间:2023-11-29 00:36:30 27 4
gpt4 key购买 nike

因此,我正致力于使用 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 对吗?看起来这段代码应该在正确的街区,但我看不出是什么导致了这个错误。谁能分享一些见解?

最佳答案

你真的需要 4 个准备好的语句吗?您是否需要为每个 taqQuotes 插入 1 行数据?如果是这样,那么我会这样做:

public static void quotesReader() throws ClassNotFoundException, SQLException {
File f = new File("/Users/Adam/Desktop/SQL Folder/20070620/20070620");
File[] files = f.listFiles();

try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/newTest", "root", "Kariya"
);
long counter = 1; // place this before the for(int i=0...) loop
pst = connect.prepareStatement("INSERT INTO newTestTable2(stockID, millisFromMid, bidPrice, askPrice) VALUES(?,?,?,?)");

for( int i=0; i < files.length; i++) {
ReadGZippedTAQQuotesFile taqQuotes = new ReadGZippedTAQQuotesFile( files[i] );
int nRecs = taqQuotes.getNRecs();
for( int j = 0; j < nRecs; j++ ) {
pst.setInt(1, counter++);
pst.setInt(2, taqQuotes.getMillisecondsFromMidnight( j ));
pst.setDouble(3, taqQuotes.getBidPrice( j ));
pst.setDouble(4, taqQuotes.getAskPrice( j ));
pst.executeUpdate();
}
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (pst != null) {
pst.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {

}
}
}

这将为每个 taqQuotes 插入 1 条记录,而不是 4 条记录,每条记录只填充一列

关于java - 使用 MySQL 和 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14048858/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com