Java Reference
In-Depth Information
It would be nice to call some kind of isExist() method before creating
EMPLOYEES , but that method doesn't exist. However, we can create this method with
help from getTables() , and Listing 9-20 shows you how to accomplish this task.
Listing 9-20. Determining the existence of EMPLOYEES before creating this table
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class EmployeeDB
{
public static void main(String[] args)
{
String url = "jdbc:derby:employee;create=true";
try
(Connection
con
=
DriverMan-
ager.getConnection(url))
{
try (Statement stmt = con.createStatement())
{
String sql;
if (!isExist(con, "EMPLOYEES"))
{
System.out.println("EMPLOYEES doesn't ex-
ist");
sql = "CREATE TABLE EMPLOYEES(ID INTEGER,
NAME VARCHAR(30))";
stmt.executeUpdate(sql);
}
else
System.out.println("EMPLOYEES already ex-
ists");
sql = "INSERT INTO EMPLOYEES VALUES(1, 'John
Search WWH ::




Custom Search