Generate Java Beans from a Database

This example facilitates the automatic generation of Java bean classes from existing database tables. By analyzing the table structures, DataPipeline generates corresponding Java classes, saving you valuable time and effort in manually writing repetitive code.

This can be used to speed up the development process in database-driven applications. It empowers you to quickly map database tables to Java objects, reducing the likelihood of errors and ensuring consistency between the data storage and application layers. DataPipeline streamlines the integration of data from databases into Java applications, making it a valuable tool for developers working on data-centric projects.

 

Java Code Listing

package com.northconcepts.datapipeline.foundations.examples.jdbc;

import com.northconcepts.datapipeline.foundations.jdbc.JdbcConnection;
import com.northconcepts.datapipeline.foundations.jdbc.JdbcTable;
import com.northconcepts.datapipeline.foundations.jdbc.JdbcTableColumn;
import com.northconcepts.datapipeline.sourcecode.CodeWriter;
import com.northconcepts.datapipeline.sourcecode.JavaCodeBuilder;


public class GenerateJavaBeansFromADatabase {

    static JavaCodeBuilder code = new JavaCodeBuilder();
    static CodeWriter sourceWriter = code.getSourceWriter();

    public static void main(String... args) {

        JdbcConnection connection = new JdbcConnection()
                .setDriverClassName("org.postgresql.Driver")
                .setUrl("jdbc:postgresql://localhost:5432/customers")
                .setUsername("username")
                .setPlainTextPassword("password");


        for(JdbcTable table : connection.loadTables().getTables()) {
            createTableClass(table);
        }

        System.out.println(code.getSource());
    }

    public static void createTableClass(JdbcTable table) {
        sourceWriter.println("public class %s {", table.getName());
        sourceWriter.indent();
        sourceWriter.println();
        for(JdbcTableColumn column : table.getColumns()) {
            sourceWriter.println("private %s %s;", column.getMethodSuffix(), column.getName());
        }
        sourceWriter.println();
        for(JdbcTableColumn column : table.getColumns()) {
            sourceWriter.println("public %s set%s(%s %s){", table.getName(), column.getName(),
                    column.getMethodSuffix(), column.getName());
            sourceWriter.indent();
            sourceWriter.println("this.%s = %s;", column.getName(), column.getName());
            sourceWriter.println("return this;");
            sourceWriter.outdent();
            sourceWriter.println("}");
            sourceWriter.println();
            sourceWriter.println("public %s get%s(){", column.getMethodSuffix(),column.getName());
            sourceWriter.indent();
            sourceWriter.println("return %s;", column.getName());
            sourceWriter.outdent();
            sourceWriter.println("}");
            sourceWriter.println();
        }
        sourceWriter.println("public %s read(ResultSet resultSet) {", table.getName());
        sourceWriter.indent();
        for(JdbcTableColumn column : table.getColumns()) {
            sourceWriter.println("%s = resultSet.get%s(\"%s\");", column.getName(), column.getMethodSuffix(),
                    column.getName());
        }
        sourceWriter.println("return this;");
        sourceWriter.outdent();
        sourceWriter.println("}");
        sourceWriter.outdent();
        sourceWriter.println("}");
        sourceWriter.println();
    }
}

 

Code Walkthrough

  1. JavaCodeBuilder and CodeWriter instances are created and declared at the class level. They are used to generate Java bean classes. 
  2. JDBCConnection is created with local database properties.
  3. From the database connection, tables are loaded, and Java classes matching each table are created inside a separate method.
  4. Java code phrases, methods (getter and setter), columns, and data types are written by calling methods of sourceWriter instance.
  5. Finally, code.getSource() is invoked and printed on the console.

 

Console Output

Tables from the database are loaded and written as Java classes in the console.

Mobile Analytics