如何在java中运行ms sql server脚本?

时间:2015-02-06 05:43:13

标签: java sql-server

我正在使用此代码。

BufferedReader in = new BufferedReader(new FileReader(filename.getText()));
String str;
StringBuffer sb = new StringBuffer();
while ((str = in.readLine()) != null) {

    sb.append(str + "\n ");
}
in.close();
Statement stmt = con.createStatement();
stmt.execute(sb.toString());
System.err.println("Script Executed");

但是给了 Exception:com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'GO'

一些查询代码是:

USE [Medico]
GO
/****** Object:  User [medico1]    Script Date: 02/05/2015 19:19:40 ******/
CREATE USER [medico1] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  User [medico]    Script Date: 02/05/2015 19:19:40 ******/
CREATE USER [medico] WITHOUT LOGIN WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object:  Table [dbo].[employee]    Script Date: 02/05/2015 19:19:51           ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[employee](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](100) NULL,
[code] [varchar](100) NOT NULL,
[address] [varchar](max) NULL,
[city] [varchar](100) NULL,
[state] [varchar](50) NULL,
[phone] [varchar](50) NULL,
[mob] [varchar](50) NULL,
[email] [varchar](100) NULL,
[enrolldate] [date] NULL,
[qualification] [varchar](100) NULL,
[status] [varchar](100) NULL,
[description] [varchar](max) NULL,
[designation] [varchar](100) NULL,
[etype] [varchar](100) NULL,
[password] [varchar](100) NULL,
[username] [varchar](250) NULL,
[usercode] [varchar](250) NULL,
PRIMARY KEY CLUSTERED 
(
[code] ASC
 )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY =      OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
 ) ON [PRIMARY]
 GO
 SET ANSI_PADDING OFF
 GO

1 个答案:

答案 0 :(得分:0)

要从该脚本创建所有必需的对象,请打开Sql Server Management studio并运行该脚本。这是创建所有对象的最安全的方法