如何在Java中执行SQL脚本文件?

编程入门 行业动态 更新时间:2024-10-12 05:54:18
本文介绍了如何在Java中执行SQL脚本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用Java执行一个SQL脚本文件而不将整个文件内容读入大查询并执行它。

I want to execute an SQL script file in Java without reading the entire file content into a big query and executing it.

还有其他标准方法吗?

推荐答案

没有可移植的方法。您可以执行本机客户端作为外部程序来执行此操作:

There is no portable way of doing that. You can execute a native client as an external program to do that though:

import java.io.*; public class CmdExec { public static void main(String argv[]) { try { String line; Process p = Runtime.getRuntime().exec ("psql -U username -d dbname -h serverhost -f scripfile.sql"); BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream())); while ((line = input.readLine()) != null) { System.out.println(line); } input.close(); } catch (Exception err) { err.printStackTrace(); } } }

  • 代码示例是从此处中提取并修改为回答问题,假设用户需要执行PostgreSQL脚本文件。
    • Code sample was extracted from here and modified to answer question assuming that the user wants to execute a PostgreSQL script file.

更多推荐

如何在Java中执行SQL脚本文件?

本文发布于:2023-11-23 19:10:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622568.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   文件   如何在   SQL   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!