try this! exact copy of mine! why error occurs!
import client.WebSocketClientImpl;
import pojoClass.BookTickerProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import sqlite.SqliteHelper;
import tools.ConsoleColors;
import tools.CoolFormatter;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class BookTickerStream {
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectReader objectReader = mapper.readerFor(BookTickerProperties.class);
@SuppressWarnings("SpellCheckingInspection")
private static final String symbol = "btcusdt";
public static void main(String[] args) {
WebSocketClientImpl client = new WebSocketClientImpl();
bookTickerWithSqlite(client);
}
private static void bookTickerWithSqlite(WebSocketClientImpl client) {
//Sqlite Connection
Connection connection = SqliteHelper.OpenOrCreateSqliteConnection(symbol);
String tableName = SqliteHelper.CreateBookTickerPropertyTableIfNotExists(connection);
System.out.println("All Tables : " + SqliteHelper.GetAllTables(connection));
client.bookTicker(symbol, (event) -> {
try {
BookTickerProperties tickerProperties = objectReader.readValue(event);
//Insert
SqliteHelper.InsertBookTickerPropertyIntoTable(connection, tableName, tickerProperties);
} catch (JsonProcessingException e) {
e.printStackTrace();
client.closeAllConnections();
SqliteHelper.CloseSqliteConnection(connection);
}
});
}
}
package sqlite;
import pojoClass.BookTickerProperties;
import tools.CoolFormatter;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class SqliteHelper {
public static Connection OpenOrCreateSqliteConnection(String dbName) {
// SQLite's connection string
String url = "jdbc:sqlite:C://sqlite/records/bookTicker/" + dbName + ".db";
Connection connection = null;
try {
connection = DriverManager.getConnection(url);
System.out.println("Sqlite connected to " + dbName.toUpperCase() + " database");
} catch (SQLException e) {
System.out.println("Sqlite connecting failed : " + e.getMessage());
}
return connection;
}
public static void CloseSqliteConnection(Connection connection) {
try {
connection.close();
System.out.println("Sqlite connection closed!");
} catch (SQLException e) {
System.out.println("Connection closing failed: " + e.getMessage());
}
}
public static String CreateBookTickerPropertyTableIfNotExists(Connection connection) {
String tableName = CoolFormatter.currentFormattedDateTime();
try {
Statement statement = connection.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS " + "'" + tableName + "'" +
"(\n" +
"\t\"updateId\"\tINTEGER NOT NULL UNIQUE,\n" +
"\t\"Symbol\"\tTEXT NOT NULL,\n" +
"\t\"bidPrice\"\tTEXT NOT NULL,\n" +
"\t\"bidQty\"\tTEXT NOT NULL,\n" +
"\t\"askPrice\"\tTEXT NOT NULL,\n" +
"\t\"askQty\"\tTEXT NOT NULL,\n" +
"\tPRIMARY KEY(\"updateId\")\n" +
")";
statement.execute(sql);
statement.close();
System.out.println("New table has been created if not exists, named as '" + tableName + "'");
} catch (SQLException e) {
System.out.println("New table creation failed, named as '" + tableName + "' : " + e.getMessage());
}
return tableName;
}
public static void InsertBookTickerPropertyIntoTable(Connection connection, String tableName, BookTickerProperties response) {
try {
String sql = "INSERT INTO " + "'" + tableName + "'" + " VALUES(?,?,?,?,?,?)";
PreparedStatement prepareStatement = connection.prepareStatement(sql);
prepareStatement.setLong(1, response.getUpdateId());
prepareStatement.setString(2, response.getSymbol());
prepareStatement.setString(3, response.getBidPrice());
prepareStatement.setString(4, response.getBidQty());
prepareStatement.setString(5, response.getAskPrice());
prepareStatement.setString(6, response.getAskQty());
prepareStatement.executeUpdate();
System.out.print("\rInsert operation success.." + CoolFormatter.currentFormattedDateTime() + " ");
prepareStatement.close();
} catch (SQLException e) {
System.out.println("Insert operation failed : " + e.getMessage());
}
}
}
package pojoClass;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@JsonIgnoreProperties(ignoreUnknown = true)
public class TradeProperties {
@JsonProperty("e")
private String eventType;
@JsonProperty("E")
private long eventTime;
@JsonProperty("s")
private String symbol;
@JsonProperty("t")
private long tradeId;
@JsonProperty("p")
private String price;
@JsonProperty("q")
private String quantity;
@JsonProperty("b")
private long buyerId;
@JsonProperty("a")
private long sellerId;
@JsonProperty("T")
private long timeStamp;
@JsonProperty("m")
private boolean isMaker; //'true' lower price 'false' higher price
@JsonProperty("M")
private boolean ignore;
private String tradeTime; //extracted from timeStamp. Usage:SqliteHelper::SelectAllFromTable
public TradeProperties() {
}
public String getEventType() {
return eventType;
}
public void setEventType(String eventType) {
this.eventType = eventType;
}
public long getEventTime() {
return eventTime;
}
public void setEventTime(long eventTime) {
this.eventTime = eventTime;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public long getTradeId() {
return tradeId;
}
public void setTradeId(long tradeId) {
this.tradeId = tradeId;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public long getBuyerId() {
return buyerId;
}
public void setBuyerId(long buyerId) {
this.buyerId = buyerId;
}
public long getSellerId() {
return sellerId;
}
public void setSellerId(long sellerId) {
this.sellerId = sellerId;
}
public long getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
public boolean isMaker() {
return isMaker;
}
public void setMaker(boolean maker) {
isMaker = maker;
}
public boolean isIgnore() {
return ignore;
}
public void setIgnore(boolean ignore) {
this.ignore = ignore;
}
public String getTradeTime() {
return tradeTime;
}
public void setTradeTime(String tradeTime) {
this.tradeTime = tradeTime;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append("tradeId", tradeId)
.append("symbol", symbol)
.append("price", price)
.append("quantity", quantity)
.append("buyerId", buyerId)
.append("sellerId", sellerId)
.append("tradeTime", tradeTime)
.append("isMaker", isMaker)
.toString();
}
}