通过Java执行.sql文件
我有一个sql脚本文件,我需要通过java执行存在的命令。我在网上搜索了相同的内容,我得到了一些定义解析器的代码来分离SQL语句并执行它。但是他们都没有为我的SQL脚本文件工作。因为我的脚本文件包含最终没有分号的创建语句和alter语句[相反它有GO]任何人都可以建议一个解决方案来执行脚本文件? 谢谢, 马赫什通过Java执行.sql文件
对于简单的脚本,我通常使用ibatis这个类 - ScriptRunner。另外,你可以从Java产生一个新的数据库客户端进程,并提供你想要执行的脚本。这将适用于所有脚本,因为当例如sql文件中的分隔符被更改时,像ScriptRunner这样的简单解决方案不能很好地工作。
下面是一个例子如何喂养SQL作为一个字符串转换为spawed DB客户端进程:
private void runSql(String pSql) {
String tCommand = "mysql -u " + username + (password != null ? " -p" + password : "") + " " + dbName;
System.out.println(tCommand);
try {
Process tProcess = Runtime.getRuntime().exec(tCommand);
OutputStream tOutputStream = tProcess.getOutputStream();
Writer w = new OutputStreamWriter(tOutputStream);
System.out.println(pSql);
w.write(pSql);
w.flush();
Scanner in = new Scanner(tProcess.getErrorStream());
String errorMessage = "";
while (in.hasNext()) {
errorMessage += in.next() + " ";
}
if (errorMessage.length() > 0) {
System.out.println(errorMessage);
throw new ClientSqlExecutionException(errorMessage);
}
} catch (IOException e) {
e.printStackTrace();
}
}
您需要更改解析器所以它产生的可执行语句。但是我不确定当你说“通过Java执行”时我明白你的意思。
Java将不会执行这些SQL语句 - 您连接的数据库将会执行。 Java可以使用JDBC连接到数据库并从文件发送SQL语句。
我不明白为什么你必须解析SQL,除非你希望Java在将它们发送到数据库服务器之前验证它们。服务器会再次分析和验证它们,所以感觉就像你在做额外的工作一样。
我可以向您呈现的最简单的解决方案就是假设我理解您的问题。
1)通过Java IO将文本文件读入字符串或数组。 2)通过JDBC将字符串或数组传递给MySQL。
从文件中读取例如,
import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
从收购
,http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml
最简单的方法是简单地让报表和检查,如果他们需要,他们在最后半柱:(这是一个例如,如果是通过线语句只:
public void executeScript(String script) {
BufferedReader in = new BufferedReader(new FileReader(script));
while (in.read() > 0) {
String statement = in.readLine();
statement = statement.trim().toLowerCase();
String command = statement.split("[ ]+")[0]; // split the statement.
if (command.equals("insert") || command.equals("update") /* || any other */) {
statement = statement + ";";
}
// execute statement using jdbc
}
}
如果你不知道如何使用JDBC,只问:-)
嘿感谢PIH ,,其实我的剧本没有通过行语句,,它具有超过行语句! – Mahesh 2011-04-28 07:58:03
你有某种语句分隔符吗?可能会有所帮助。 – Pih 2011-04-28 08:40:24
你我都有,它是 “GO”,这里是我的脚本文件内容:USE [大师] GO /******对象:表[DBO] [Allowed_Countries]脚本日期:04/08/2011 17点22分四十秒******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [DBO]。[Allowed_Countries]( \t [国家或地区名称] [VARCHAR( 100)NOT NULL, \t [CMName] [VARCHAR](100)NOT NULL, \t [允许] [字符](1)非空约束[DF_Allowed_Countries_Allowed] DEFAULT( 'Y') )ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [DBO] [Allowed_Countries]检查约束[CK_Allowed_Countries] GO – Mahesh 2011-04-28 10:38:20
使用此slightly modified version of the com.ibatis.common.jdbc.ScriptRunner class这是完全独立的,即你不需要有任何第三方依赖的JAR 。
可以将分隔符从;
更改为GO
。我认为应该这样做。
下面是一个例子:
Reader reader = new BufferedReader(*** Your FileReader instance ***);
try
{
ScriptRunner runner = new ScriptRunner(connection, false, true);
runner.setDelimiter("GO", true);
runner.runScript(reader);
}
finally
{
reader.close();
}
嗨乔纳斯,我试过上面的代码!!!它不工作。这里是我的脚本:USE [guru] GO /******对象:表[ 。DBO] [Allowed_Countries]脚本日期:04/08/2011 17时22分四十零秒******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [DBO] [Allowed_Countries]( \t [国家或地区名称] [VARCHAR](100)NOT NULL, \t [CMName] [VARCHAR](100)NOT NULL, \t [允许] [字符](1)非空约束[DF_Allowed_Countries_Allowed]默认('Y') )ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [DBO]。[Allowed_Countries]检查约束[CK_Allowed_Countries] GO – Mahesh 2011-04-28 07:59:55
Apache Ant的SQL任务为我工作。 https://ant.apache.org/manual/Tasks/sql.html
你能不能解释一下? – Robert 2015-03-20 16:26:26
嘿感谢Bozhidar !!!我会回来到u尝试这种代码。 – Mahesh 2011-04-28 09:51:22
嗨Bozhidar,我有一个包含MSSQL查询的SQL脚本文件。在MSSQL执行通过我们SQLCMD命令,,, SQLCMD本身不通过上面的代码工作的命令提示查询!!!! – Mahesh 2011-04-28 10:40:55
您应该调整它的参数。我从来没有使用过它,但这种方法是通用的 - 用正确的命令行参数它会与任何分贝工作。 – 2011-04-28 10:50:48