Java: Ways to autocomplete source code on netbeans with external source

First of all, I'm not sure if the title or tags are correct. If not, someone please correct me.

My question is if there are any tools or ways to create an autocomplete list with items from an external source, with a NetBeans cleanup and a warning if there are any errors.

- The problem . I am using JDBC and I want to simulate all my schemas, tables and columns in some way so that netbeans can parse it and alert me if something goes wrong. For example, in normal JDBC use, I would have a function:

ResultSet execSelect( String cols, String table ){
     return statement.executeQuery("SELECT "+cols+" FROM "+table); }

      

The problem is that someone has to know exactly what the available parameters have to pass the correct strings for.

I want netbeans to show me somehow an autocomplete list with all available options.

PS. I had the same problem while building a web app and wanted to somehow get all the paths for external resources like images, .js files, .css files, etc.

- Thoughts so far :
My thoughts so far have been to put a .java file with public static final strings with some nested static classes so that I can access from anywhere. For example:

DatabaseModel.MySchema.TableName1.ColumnName2

      

is a String variable with ColumnName2 and TableName1. It would help me with autocomplete, but the problem is there is no type checking. In other words, someone can use any row, globally defined or not as a table, and as a column that is also incorrect. I'm going to use nested enums somehow to cover these type checking cases, but I'm not sure if that would be a good solution anyway.

Any thoughts?

+3


source to share


1 answer


Finally, I came up with writing a "script" that connects to mysql, gets all the metadata (every column of every table of every schema) and creates a Java file with predefined classes and strings that describe the model. For example:
- If you want the column name C1 from table T1 from schema S1, you must enter DatabaseModel.S1.T1.C1._, which is a public static final row with the column name.
- If you need table T2 from schema S2, you must inject DatabaseModel.S2.T2, which is a class that implements the DatabaseTable interface. Thus, the: execSelect function can use DatabaseTable and DatabaseColumn as a parameter.

Here is the code (not tested, but the idea is clear I guess).

public static void generateMysqlModel(String outputFile) throws IOException, SQLException{
    //** Gather the database model
    // Maps a schema -> table -> column
    HashMap<String,HashMap<String,ArrayList<String>>> model = 
            new HashMap<String,HashMap<String,ArrayList<String>>>();

    openDatabase();
    Connection sqlConn = DriverManager.getConnection(url, username, password);
    DatabaseMetaData md = sqlConn.getMetaData();

    ResultSet schemas = md.getSchemas(); // Get schemas
    while( schemas.next() ){ // For every schema
        String schemaName = schemas.getString(1);
        model.put( schemaName, new HashMap<String,ArrayList<String>>() );
        ResultSet tables = md.getTables(null, null, "%", null);  // Get tables
        while (tables.next()) { // For every table
            String tableName = tables.getString(3);
            model.get(schemaName).put( tableName, new ArrayList<String>() );
            // Get columns for table
            Statement s = sqlConn.createStatement();  // Get columns
            s.execute("show columns in "+tables.getString(3)+";");
            ResultSet columns = s.getResultSet();
            while( columns.next() ){ // For every column
                String columnName = columns.getString(1);
                model.get(schemaName).get(tableName).add( columnName );
            }
        }
    }

    closeDatabase();


    //** Create the java file from the collected model
    new File(outputFile).createNewFile();
    BufferedWriter bw = new BufferedWriter( new FileWriter(outputFile) ) ;

    bw.append( "public class DatabaseModel{\n" );
    bw.append( "\tpublic interface DatabaseSchema{};\n" );
    bw.append( "\tpublic interface DatabaseTable{};\n" );
    bw.append( "\tpublic interface DatabaseColumn{};\n\n" );
    for( String schema : model.keySet() ){
        HashMap<String,ArrayList<String>> schemaTables = model.get(schema);
        bw.append( "\tpublic static final class "+schema+" implements DatabaseSchema{\n" );
        //bw.append( "\t\tpublic static final String _ = \""+schema+"\";\n" );
        for( String table : schemaTables.keySet() ){
            System.out.println(table);
            ArrayList<String> tableColumns = schemaTables.get(table);
            bw.append( "\t\tpublic static final class "+table+" implements DatabaseTable{\n" );
            //bw.append( "\t\t\tpublic static final String _ = \""+table+"\";\n" );
            for( String column : tableColumns ){
                System.out.println("\t"+column);
                bw.append( "\t\t\tpublic static final class "+column+" implements DatabaseColumn{"
                        + " public static final String _ = \""+column+"\";\n"
                        + "}\n" );
            }
            bw.append( "\t\t\tpublic static String val(){ return this.toString(); }" );
            bw.append( "\t\t}\n" );
        }
        bw.append( "\t\tpublic static String val(){ return this.toString(); }" );
        bw.append( "\t}\n" );
    }
    bw.append( "}\n" );

    bw.close();
}

      



PS. In the case of resources in a web application, I think someone could recursively get all the files from the "resources" folder and populate the model variable. This will create a java file with file paths. In this case, the interfaces can be file types or any other "kind of file" you want.

I also thought it would be useful to create a .java file from an XML file for any occasion, so that anyone could just create some definition in the XML file for that purpose. If anyone implements something like this, you can post it here.

Any comments / improvements would be appreciated.

0


source







All Articles