How to create a dynamic Table using JDBC ?
I was working on creating, JDBC code that can create Tables on the bases of user's input and can also show the tables present in the database
For now I am using Switch case to take users input how many columns user wants to enter. and setting every column as varchar. But this is not the optimal solution of this problem what if user wants to insert 10 columns or more we can't handle it manually by switch or else if is there any way to make it dynamic.
similar issue is there with selection, while fetching and displaying a table, we need to write resultSet.getInt() or getBoolean or getString, and here we should know all the columns also. But how to fetch all tables dynamically ?
First, let me warn that creating arbitrary tables/columns in a database isn't a practice I'd recommend. You can end up with a big mess of stuff that nobody understands and nobody can maintain. Fixed schemas are generally a better way to go.
But there are several ways to determine what's defined in a schema. If you have a ResultSet, you can get the metadata of the ResultSet, for example.
All modern databases have an information schema that's a database that describes all the databases in the server instance. This provides standardized tables that you can do SQL queries against to get schema information.
Science is the process of replacing what we "know" with what is TRUE. Politics, alas, often prefers to be the opposite.