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(System.in);
		System.out
				.println("Please provide below details to connect Oracle Database");
		System.out.println("Enter Database");
		String dbName = scanner.next();
		System.out.println("Enter UserName");
		String userName = scanner.next();
		System.out.println("Enter Password");
		String password = scanner.next();

		try {

			Class.forName("oracle.jdbc.driver.OracleDriver");

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		try {

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

		} catch (SQLException e) {

			e.printStackTrace();
		}

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

			try {
				statement = connection.createStatement();

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

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

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

 

6 Comments How to Create Table in Oracle Using statement in Java

  1. suma

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

    Failed to connect to Oracle DB

    Process completed.

    Reply
    1. admin

      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.

      Thanks

      Reply
  2. jakkani

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

    Reply

Leave A Comment

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.