java 调用mysql命令行_java使用Runtime类 调用命令行 备份mysql数据库

java 调用命令行 备份mysql数据库

// 方式1

String command = "D:\\mysql\\bin\\mysqldump -h127.0.0.1 -uroot -p -ntd -R -dbname";

Runtime runtime = Runtime.getRuntime();

Process process = runtime.exec(command);

InputStreamReader reader = new InputStreamReader(process.getInputStream());

BufferedReader buffReader = new BufferedReader(reader);

String inStr;

String outStr;

StringBuffer buffer = new StringBuffer();

while ((inStr = buffReader.readLine()) != null) {

buffer.append(inStr + "\r\n");

}

outStr = buffer.toString();

OutputStream os = new FileOutputStream("D:\\sql.sql");

OutputStreamWriter writer = new OutputStreamWriter(os);

writer.write(outStr);

writer.flush();

reader.close();

buffReader.close();

os.close();

writer.close();

// 方式2

String command = "cmd /c mysqldump -h127.0.0.1 -uroot -p -ntd -R -dbname";

Runtime runtime = Runtime.getRuntime();

Process process = runtime.exec(command, null,new File("D:\\mysql\\bin"));

InputStreamReader reader = new InputStreamReader(process.getInputStream());

BufferedReader buffReader = new BufferedReader(reader);

String inStr;

String outStr;

StringBuffer buffer = new StringBuffer();

while ((inStr = buffReader.readLine()) != null) {

buffer.append(inStr + "\r\n");

}

outStr = buffer.toString();

OutputStream os = new FileOutputStream("D:\\sql.sql");

OutputStreamWriter writer = new OutputStreamWriter(os);

writer.write(outStr);

writer.flush();

reader.close();

buffReader.close();

os.close();

writer.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以通过调用命令行实现使用nmap扫描一个网段的端口是否存在于MySQL数据库中。以下是一个简单的实现示例: ``` import java.io.BufferedReader; import java.io.InputStreamReader; public class NmapScanner { public static void main(String[] args) { String ipRange = "192.168.0.0/24"; try { Process process = Runtime.getRuntime().exec("nmap -p 1-65535 -oG - " + ipRange); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { if (line.startsWith("Host: ")) { String ipAddress = line.substring("Host: ".length(), line.indexOf(" ")); String[] ports = line.substring(line.indexOf(":")+1).split(","); for (String port : ports) { if (port.contains("open")) { if (isPortInDatabase(ipAddress, port)) { System.out.println("Port " + port + " is open and exists in database."); } else { System.out.println("Port " + port + " is open but does not exist in database."); } } } } } } catch (Exception e) { e.printStackTrace(); } } private static boolean isPortInDatabase(String ipAddress, String port) { boolean result = false; // TODO: Implement database query to check if the port exists in MySQL database. return result; } } ``` 在上面的示例中,使用 Runtime exec() 方法调用命令行执行 nmap 命令来扫描指定 IP 地址范围内的所有端口。然后,使用 BufferedReader 读取命令输出的结果,并解析端口状态。如果端口状态为“open”,则调用 isPortInDatabase() 方法来检查该端口是否存在于 MySQL 数据库中。 需要注意的是,在使用 nmap 扫描工具时,应该遵守相关法律法规和道德规范,不要进行未经授权的扫描行为。同时,在实际应用中,需要根据实际情况对代码进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值