The Database Connector plugin is a powerful tool designed to integrate your Codgen-X projects with various databases. This documentation provides an overview of the plugin's features, setup, and usage.
Features
- Database Connectivity: Connect to MySQL, PostgreSQL, SQLite, and more.
- Query Execution: Execute SQL queries and retrieve results.
- Parameterized Queries: Enhance security by preventing SQL injection.
- ORM Integration: Seamlessly integrate with Object-Relational Mapping (ORM) frameworks.
Setup
To set up the Database Connector plugin, follow these steps:
- Navigate to the Codgen-X plugins directory.
- Download the Database Connector plugin from here.
- Unzip the downloaded file and place the contents in the plugins directory.
- Restart Codgen-X to load the plugin.
Usage
Connecting to a Database
To connect to a database, use the following code:
DatabaseConnector connector = new DatabaseConnector();
connector.setDatabaseType(DatabaseType.MYSQL);
connector.setHostname("localhost");
connector.setUsername("user");
connector.setPassword("password");
connector.setDatabaseName("database");
if (connector.connect()) {
System.out.println("Connected to the database!");
} else {
System.out.println("Failed to connect to the database.");
}
Executing a Query
To execute a query, use the following code:
DatabaseConnector connector = new DatabaseConnector();
connector.setDatabaseType(DatabaseType.MYSQL);
connector.setHostname("localhost");
connector.setUsername("user");
connector.setPassword("password");
connector.setDatabaseName("database");
if (connector.connect()) {
ResultSet resultSet = connector.executeQuery("SELECT * FROM users");
while (resultSet.next()) {
System.out.println("User ID: " + resultSet.getInt("id"));
System.out.println("Username: " + resultSet.getString("username"));
}
connector.disconnect();
} else {
System.out.println("Failed to connect to the database.");
}
ORM Integration
For ORM integration, refer to the ORM documentation.
Database Connection