![](/img/trans.png)
[英]AES encryption method equivalent to MySQL aes_encrypt() function
[英]AES_ENCRYPT with mysql server 5.5
我試圖使用准備好的語句以加密格式將數據插入MySQL。 但我收到此錯誤。 我認為我的查詢是正確的。
Connection con=null;
PreparedStatement psmt1=null;
String tit=request.getParameter("tit");
String min=request.getParameter("minvalue");
String max=request.getParameter("maxvalue");
String disc=request.getParameter("disc");
System.out.println(tit);
String [] title=tit.split(":");
try{
con=databasecon.getconnection();
psmt1=con.prepareStatement("insert into medication(tid,titname,minvalue,maxvalue,disc) values(AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'))");
psmt1.setString(1,title[0]);
psmt1.setString(2,title[1]);
psmt1.setString(3,min);
psmt1.setString(4,max);
psmt1.setString(5,disc);
psmt1.executeUpdate();
}
catch(Exception ex)
{
out.println("Error in connection : "+ex);
}
下面是我的連接類
package databaseconnection;
import java.sql.*;
public class databasecon
{
static Connection con;
public static Connection getconnection()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cam","root","root");
}
catch(Exception e)
{
System.out.println("class error");
}
return con;
}
}
最后的錯誤是
Error in connection : java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'maxvalue,disc) values(AES_ENCRYPT('6', 'key'),AES_ENCRYPT('systolic bp', 'key'),' at line 1
我不知道為什么會這樣,但是您需要將字節數組作為第二個參數傳遞給MySQL的AES_ENCRYPT :
psmt1 = con.prepareStatement("insert into medication(tid,titname,minvalue,maxvalue,disc) values(AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'),AES_ENCRYPT(?, 'key'))");
byte[] keyBytes = "key".getBytes();
psmt1.setString(1, title[0]);
psmt1.setBytes(2, keyBytes);
psmt1.setString(3, title[1]);
psmt1.setBytes(4, keyBytes);
psmt1.setString(5, min);
psmt1.setBytes(6, keyBytes);
psmt1.setString(7, max);
psmt1.setBytes(8, keyBytes);
psmt1.setString(9, disc);
psmt1.setBytes(10, keyBytes);
psmt1.executeUpdate();
我說我沒有解釋,因為MySQL文檔暗示AES_ENCRYPT
兩個參數都是字符串。 我本人會犯這個錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.