JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

"CREATE SCHEMA" Statements

This section describes how to create a schema in the current database.

A schema on SQL Server is a container of other database objects like tables, views or stored procedures. Creating schemas in the database can help to divide database objects into groups to make object management easier.

Here is a simple Java program that creates a new schema in the current database:

/**
 * CreateSchema.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class CreateSchema {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Load Microsoft JDBC Driver 1.0
      Class.forName(
        "com.microsoft.sqlserver.jdbc.SQLServerDriver");

// Obtaining a connection to SQL Server
      con = DriverManager.getConnection(
          "jdbc:sqlserver://localhost:1269;"
        + "user=sa;password=HerongYang;"
        + "database=AdventureWorksLT");

// Creating a database schema
      Statement sta = con.createStatement(); 
      int count = sta.executeUpdate("CREATE SCHEMA Herong");
      System.out.println("Schema created.");
      sta.close();        

      con.close();        
    } catch (java.lang.ClassNotFoundException e) {
      System.err.println("ClassNotFoundException: "
        +e.getMessage());
    } catch (SQLException e) {
      System.err.println("SQLException: "
        +e.getMessage());
    }
  }
}

If you run this program a new schema will be created with the following message.

Schema created.

Sections in This Chapter

Executing "Update" Statements - executeUpdate()

"CREATE SCHEMA" Statements

"CREATE TABLE" Statements

"ALTER TABLE" Statements

"DROP TABLE" Statements

Dr. Herong Yang, updated in 2007
"CREATE SCHEMA" Statements