?Java???evalsha
??,?????????:
- ???????????SHA-1??????????????????SHA-1?????????????Java?
MessageDigest
???????? - ??
evalsha
???,???????????ProcessBuilder
??????????????????? - ??
evalsha
????????????????????????
????????????,??????Java???evalsha
??:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class EvalShaExample { public static void main(String[] args) { try { // ????? String input = "Hello, World!"; // ??SHA-1??? String sha1Hash = getSha1Hash(input); // ??evalsha?? ProcessBuilder processBuilder = new ProcessBuilder("mysql", "--local-infile=1", "your_database", "your_username", "your_password", "your_table_name"); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); // ????????evalsha?? process.getOutputStream().write((sha1Hash + "\n").getBytes()); process.getOutputStream().write((input + "\n").getBytes()); process.getOutputStream().flush(); process.getOutputStream().close(); // ??evalsha????? BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } // ???????????? int exitCode = process.waitFor(); System.out.println("Exit code: " + exitCode); } catch (IOException | NoSuchAlgorithmException | InterruptedException e) { e.printStackTrace(); } } private static String getSha1Hash(String input) throws NoSuchAlgorithmException { MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); byte[] hashBytes = messageDigest.digest(input.getBytes()); StringBuilder sb = new StringBuilder(); for (byte b : hashBytes) { sb.append(String.format("x", b)); } return sb.toString(); } }
???,??????mysql
?????????????,???????????????????????,?????????????????MySQL???,????????????
??????,evalsha
??????????,??????????MySQL??????????,????????????????,???????????