How to Create Table in Oracle Using statement in Java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

 * Here we will learn to Create Table in DB using JDBC Statements in Oracle and MySQL DB
public class CreateTableWithStatement {

	public static void main(String[] args) {

		String tableCreateQuery = "create table JBT(name varchar2(10), course varchar2(10))";
		Statement statement = null;
		Connection connection = null;

		Scanner scanner = new Scanner(;
				.println("Please provide below details to connect Oracle Database");
		System.out.println("Enter Database");
		String dbName =;
		System.out.println("Enter UserName");
		String userName =;
		System.out.println("Enter Password");
		String password =;

		try {


		} catch (ClassNotFoundException e) {

		try {

			connection = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:" + dbName, userName,

		} catch (SQLException e) {


		if (connection != null) {
			System.out.println("nSuccessfullly connected to Oracle DB");

			try {
				statement = connection.createStatement();

				System.out.println("Create Table Query :"+tableCreateQuery);

				System.out.println("Table Created Successfully");
			} catch (SQLException e) {

		} else {
			System.out.println("nFailed to connect to Oracle DB");


6 thoughts

  1. i got this error/////////
    java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
    at oracle.jdbc.dbaccess.DBError.throwSqlException(
    at oracle.jdbc.dbaccess.DBError.throwSqlException(
    at oracle.jdbc.driver.OracleConnection.(
    at oracle.jdbc.driver.OracleDriver.getConnectionInstance(
    at oracle.jdbc.driver.OracleDriver.connect(
    at java.sql.DriverManager.getConnection(
    at java.sql.DriverManager.getConnection(
    at CreateTableWithStatement.main(

    Failed to connect to Oracle DB

    Process completed.

    1. Hi Suma,

      As mentioned clearly in the error this problem is related to connection to Oracle DB.
      There could be several reason behind it. Check for the firewall access. Also chekc the tnsname.ora file for proper configuration.
      You can also use “tnsping” command in command prompt to check if connectivity is proper or not.

      First check the connectivity of Oracle DB then try to create table in DB.

      Please let me know if you need more help.


  2. remove that db name from query
    connection = DriverManager.getConnection(
    “jdbc:oracle:thin:@localhost:1521:” , userName,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top Search