Go Back

Dynamic Table Creation in JDBC [Java Database Connectivity] with Oracle | Simple | Easy

views 326 Comments 50 15-Jul-2018


JDBC or Java Database Connectivity is the connection between the Java Application and the Database, Database may be Oracle, MySQL or SQLServer etc. We can make the connection of Java Program with database very easily. Let up begin the programming


So First of all one thing, we have to make clear that, we should have a little bit knowledge of Basics of Java Programming and Databases.
Here we will make the connection with Oracle Database.
To make the connection with the database we have to import the library of Oracle Driver in the Eclipse(In case we are using Eclipse otherwise set classpath with entire path of that library).
Library path may be like this in your System :
C:oraclexeapporacleproduct11.2.0serverjdbclibojdbc6.jar
If we are using oracle10 then library name is: ojdbc14.jar
If we are using oracle11 then library name is: ojdbc6.jar
After Import or setting the path in the classpath, we have to write the program like this.

 package com.codeplanet.dynamic;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Main {
	public static void main(String[] args) throws Exception {
		Class.forName("oracle.jdbc.OracleDriver");
		Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","Abjal");
		Statement st = con.createStatement();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));		
		String query = "";
		System.out.println("Enter Table Name : ");
		String tname = br.readLine();
		query = query + "create table "+tname+"(";
		String pri_cols = "";
		int count=0;
		while(true) {
			System.out.println("Column Name : ");
			String col_name = br.readLine();
			System.out.println("Column Data Type and size ");
			String type = br.readLine();			
			System.out.println("Primaey Key ..? YES/NO : ");
			String pri = br.readLine();
			if(pri.equals("YES")) {
				if(count==0) {
					pri_cols = pri_cols + col_name;
					count++;
				}
				else {
					pri_cols = pri_cols +","+ col_name;
				}
			}
			col_name = col_name +" "+ type; 
			System.out.println("Wish to add one more column [YES/NO] ? : ");
			String add = br.readLine();
			if(add.equals("YES")){
				query = query + col_name+", ";
			}
			else {
				query = query + col_name;
				query = query + ", primary key("+pri_cols+")";
				query = query + ")";
				break;
			}
		}
		st.executeUpdate(query);
		System.out.println("Table "+tname+" created Succesfully");
		br.close();
		st.close();
		con.close();
	}
}
        


"This is how we can implement the logic for dynamic table creation.
And the Final output will be like this (see below Image..)"


If you wish to download this program then you can download from my GitHub Profile..
Profile is : github.com/mohdafzal330/Spring-Projects

If you want more articles on JDBC then mention in the comment box, We can provide whole JDBC articles.
Have any doubt ..? Mention in comment box.

Thanks for reading this article, Keep subscribing us.






Your Reaction about this article...







Write your Comment



Name *

Email *

Comment *

Website (Optional)


Comments / Suggestions (50)


mytoptutorials

Fantastic website! Lots of information here http://www.mytoptutorials.com/

ebooksfeed

Another amazing article we are like wow http://www.ebooksfeed.com/

loginnn

wow Awesome article thanks from https://19216811login.co/

admin

Just amazing https://192168ll-admin.com/

loginnn

Always best https://192168ll-login.co/

happyquote

Thank you https://www.happyquote.in/

droidtipstricks

Beautiful lots of knowledge http://www.droidtipstricks.net/

iforgotpassword

This article has a lot of information https://iforgotpassword.co/

ikikforpc

One of the best sites in recent times that we http://facetimepc.co/ come across.

traceanumber

Nice Article wishes from https://tracenumber.co/

kikpc

Good https://kikpc.co/

licpolicylogin

Thank you from https://licpolicylogin.co/

aptoideapkk

Please tell us about http://aptoideapkk.co/

iservicenumber

Thank you for the great information http://iservicenumber.com/

loginnn

Awesome article!!! very interesting to read 192168ll-login

appvnapkk

Interesting website!!! Thanks for sharing in this article appvnapkk

ifacetimeforpc

nice post!! Thank you very much for sharing the best informative articles here. keep posting more updates like this ifacetimeforpc

shareitdownloadapp

Thanks for great article nice to be here Thanks from shareitdownloadapp

videoderapkdownload

very good explanation thanks for sharing article videoderapkdownload

vidmate-downloadi

Excellent article!!! Thanks for sharing vidmate-downloadi

izapyadownload

this post is very useful to us thank you for sharing izapyadownload

ishowboxapk

Good information thank you for sharing ishowboxapk

iluckypatcherapk

Great job! Useful information here. thank you for sharing iluckypatcherapk

iluckypatcherapk

your information very interesting! Thank you for sharing iluckypatcherapk

bluestacksdownloadd

Superb post!!! thank you for sharing in this article bluestacksdownloadd

garagebandforpcandroid

This is really amazing. Great information your blog garagebandforpcandroid

facetimefor

Nice article. I like your site facetimefor

vsharedownloadd

Awesome! Gives a clear idea thanks from vsharedownloadd

firestarterapkk

Nice information for a new blogger. It is really helpful firestarterapkk

ikikforpc

Very interesting, good job and thanks for sharing such a good blog ikikforpc

navyfederal

Great article! Thank you for sharing navyfederal

unblockedgames66

Wonderful site. Lots of useful information unblockedgames66

officedepots1

useful information thanks for the post officedepots

menardss

I really like this post menardss

upstracking

Thank you for sharing upstracking

chasee

Impressive thanks for the post chasee

fingerhutt

hi, nice article i really get interesting things from your blog fingerhutt

geicoo

This is really amazing. Great useful info about your blog geicoo

geicoo

This is really amazing. Great useful info about your blog geicoo

ratemyprofessors

a very nice article thanks from ratemyprofessors

hoodamaths

One of the best article in recent times. we are like now thanks from hoodamaths

kickasss

Fantastic website! Lots information here kickasss

cargurususedcars

Another amazing article we are like wow cargurususedcars

cargurususedcars

Another amazing article we are like wow cargurususedcars

rentometer

wow Awesome article thanks from rentometer

admin

Just amazing techsite

rentometer

wow Awesome article thanks from rentometer

gogoanimego

We gogoanimego additicted to your articles.