Query using the SQL 'go' command on a JAVA code
843859Aug 19 2008 — edited Aug 19 2008Hi,
I am trying to create a new database on MS SQL and at the same time verify whether the data base exist already and then add a new table. The query statement works well on the query windows on MS SQL, but when the query is place using a JAVA code it gives the following error:
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'go'.
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'go'.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
at DataBaseCreator.main(DataBaseCreator.java:30)
I have to add that if I only query: "CREATE DATABASE NameOfDatabase" the new data base is created without a problem using JAVA, but when I try to use the following query, I got the error message.
"USE Master "
+ "IF EXISTS (SELECT * FROM SysDatabases WHERE NAME='DatesTemps') "
+ " DROP DATABASE DatesTemps"
+ " go "
+ " CREATE DATABASE DatesTemps22 "
+ " go"
+ " USE DatesTemps "
+ " CREATE TABLE Fable ( "
+ " FableID INT NOT NULL CONSTRAINT FablePK PRIMARY KEY NONCLUSTERED, "
+ " Title VARCHAR(50) NOT NULL, "
+ " Moral VARCHAR(100) NOT NULL, "
+ " FableText VARCHAR(1536) NOT NULL, "
+ " BlobType CHAR(3) NULL DEFAULT 'doc', "
+ " Blob IMAGE NULL DEFAULT NULL )"
);
If it is useful my complete code is the following, I appreciate in advance your comments.
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DataBaseCreator {
public static void main (String[] args)
{
Connection Time =null;
Statement stmt = null;
String data = "jdbc:odbc:DataBaseCreation";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Time= DriverManager.getConnection(data,"","");
stmt = Time.createStatement();
//String query;
//java.sql.Timestamp ts1 = new java.sql.Timestamp(((3*60)+58)*60*1000);
// System.out.println(ts1 + " This is a Time Stamp");
ResultSet rec = stmt.executeQuery(
"USE Master "
+ "IF EXISTS (SELECT * FROM SysDatabases WHERE NAME='DatesTemps') "
+ " DROP DATABASE DatesTemps"
+ " go "
+ " CREATE DATABASE DatesTemps22 "
+ " go"
+ " USE DatesTemps "
+ " CREATE TABLE Fable ( "
+ " FableID INT NOT NULL CONSTRAINT FablePK PRIMARY KEY NONCLUSTERED, "
+ " Title VARCHAR(50) NOT NULL, "
+ " Moral VARCHAR(100) NOT NULL, "
+ " FableText VARCHAR(1536) NOT NULL, "
+ " BlobType CHAR(3) NULL DEFAULT 'doc', "
+ " Blob IMAGE NULL DEFAULT NULL )"
);
}
catch( Exception e )
{
System.err.println( e );
e.printStackTrace();
}
finally
{
try
{
stmt.close();
Time.close();
}
catch( Exception e )
{
System.err.println( e );
}
}
}
}