How to use Java to operate QuestDB
To operate QuestDB using Java, the following Maven dependencies need to be added:
<dependency>
<groupId>org.questdb</groupId>
<artifactId>questdb</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.questdb</groupId>
<artifactId>questdb-jdbc</artifactId>
<version>5.0.1</version>
</dependency>
The following is the sample code for adding, deleting, and querying data in QuestDB implemented in Java:
1. Connect to QuestDB:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class QuestDBExample {
public static void main(String[] args) {
Connection connection = null;
try {
//To connect to the database, you need to provide the URL, username, and password of QuestDB
connection = DriverManager.getConnection("jdbc:questdb:http://localhost:9000/", "admin", "quest");
//Perform query, insert, update, or delete operations here
// ...
} catch (SQLException e) {
e.printStackTrace();
} finally {
//Close database connection
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
2. Create a table and insert data:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateTableExample {
public static void main(String[] args) {
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:questdb:http://localhost:9000/", "admin", "quest");
Statement statement = connection.createStatement();
//Create Table
String createTableSQL = "CREATE TABLE quotes (timestamp TIMESTAMP, symbol SYMBOL, price DOUBLE)";
statement.execute(createTableSQL);
//Insert Data
String insertDataSQL = "INSERT INTO quotes VALUES (NOW(), 'AAPL', 135.25)";
statement.execute(insertDataSQL);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
3. Query data:
import java.sql.*;
public class QueryDataExample {
public static void main(String[] args) {
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:questdb:http://localhost:9000/", "admin", "quest");
Statement statement = connection.createStatement();
//Query data
String querySQL = "SELECT * FROM quotes";
ResultSet resultSet = statement.executeQuery(querySQL);
//Process query results
while (resultSet.next()) {
Timestamp timestamp = resultSet.getTimestamp("timestamp");
String symbol = resultSet.getString("symbol");
double price = resultSet.getDouble("price");
System.out.println(timestamp + "," + symbol + "," + price);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
4. Update data:
import java.sql.*;
public class UpdateDataExample {
public static void main(String[] args) {
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:questdb:http://localhost:9000/", "admin", "quest");
Statement statement = connection.createStatement();
//Update data
String updateSQL = "UPDATE quotes SET price = 140.50 WHERE symbol = 'AAPL'";
int rowsAffected = statement.executeUpdate(updateSQL);
System.out.println("Rows affected: " + rowsAffected);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
5. Delete data:
import java.sql.*;
public class DeleteDataExample {
public static void main(String[] args) {
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:questdb:http://localhost:9000/", "admin", "quest");
Statement statement = connection.createStatement();
//Delete data
String deleteSQL = "DELETE FROM quotes WHERE symbol = 'AAPL'";
int rowsAffected = statement.executeUpdate(deleteSQL);
System.out.println("Rows affected: " + rowsAffected);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
These sample codes demonstrate the basic operations of using Java to operate QuestDB, and you can modify and extend these codes as needed.