java sqlserver batch_求助!Java调用sql文件,实现判断数据库是否有表功能,报错java.sql.BatchUpdateException...

用Java链接数据库并执行数据库文件。实现初始化数据库(判断数据库插入数据时是否有表,有就插入,没有就创建。)

报错信息:

begin

执行了:0

更新错误

java.sql.BatchUpdateException: “;”附近有语法错误。

at sqljdbc4/com.microsoft.sqlserver.jdbc.SQLServerStatement.executeBatch(SQLServerStatement.java:1723)

at com.qfx.test.TestReadFile.getData(TestReadFile.java:47)

at com.qfx.test.TestReadFile.main(TestReadFile.java:15)

0

源代码如下:

package com.qfx.test;

import java.io.*;

import java.sql.*;

/**

* 读取指定文件下sql脚本,执行到数据库

* 朱行读取分批处理批量插入数据库

*/

public class TestReadFile {

public static void main(String[] args) {

System.err.println("begin");

long start = System.currentTimeMillis();

String path = "D:\\chushihua.sql";

getData(path);

System.err.print((System.currentTimeMillis() - start) / 1000);

}

private static void getData(String path) {

//读取文件

BufferedReader reader;

Connection conn = null;

Statement pst = null;

try {

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

conn = DriverManager.getConnection(

"jdbc:sqlserver://localhost:1433;DatabaseName=StudentTest", "sa", "123456");

pst = conn.createStatement();

reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));

String line;

int i = 0;

while ((line = reader.readLine()) != null) {

pst.addBatch(line);

/*  System.out.println("-----------------------");

System.out.println(line);

System.out.println("-----------------------");*/

if (i % 100 == 0) {

System.out.println("执行了:" + i);

pst.executeBatch();

}

i += 1;

}

reader.close();

// 执行批量更新

pst.executeBatch();

}

catch (Exception e) {

//此处判断是否在批量更新处抛出异常

System.out.println("更新错误");

e.printStackTrace();

} finally {

try {

if (pst != null) {

pst.close();

}

if (conn != null) {

conn.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

求助一下,一直找不到问题。

原文:https://www.cnblogs.com/wtayits/p/12991060.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值