我正在开发一个java applcation,我在一些网站托管服务器(000webhost)上存储了一个数据库,我编写了代码来从作为网站托管的PHP文件中的数据库服务器(MySQL)中获取数据。 以下是相同的代码:
//http://mahaweb.ml/java.php
<?php
require "connect.php";
$db = $_POST["db"];
mysqli_select_db($con,$db);
$mysql_qry = $_POST["query"];
$result = mysqli_query($con,$mysql_qry);
if(mysqli_num_rows($result) > 0){
while($row=mysqli_fetch_row($result)){ // for each row in results
foreach($row as $value){ // for each column in row
echo $value.'|';
}
}
}
else
echo 'null';
?>
从php文件中读取数据的java代码:
public static String getData(String query,String db){
HttpURLConnection conn=null;
String inputLine = null,result = null;
try{
URL url=new URL("http://mahaweb.ml/java.php");
conn=(HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
String postData = URLEncoder.encode("db","UTF-8")+"="+URLEncoder.encode(db,"UTF-8")+"&"+URLEncoder.encode("query","UTF-8")+"="+URLEncoder.encode(query,"UTF-8");
OutputStream out=conn.getOutputStream();
out.write(postData.getBytes());
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while((inputLine=in.readLine())!=null){
StringBuffer tmp1 = new StringBuffer(inputLine);
tmp1.deleteCharAt(tmp1.length()-1);
result = tmp1.toString();
}
in.close();
}catch(Exception e){
e.printStackTrace();
}finally{
conn.disconnect();
}
if(result.equals("null"))
return null;
return result;
}
我使用包含字节格式指纹值的blob列在数据库上存储指纹。 当我使用jdbc连接器从数据库获取数据并将该数据与指纹匹配时,它成功匹配,代码在这里是相同的:
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/dlap_application",user,pass);
PreparedStatement ps = con.prepareStatement("select fingerscan from login where empID like 'MH1200001'");
ResultSet rs = ps.executeQuery();
while(rs.next())
ISOTemplate = rs.getBytes("fingerscan");
ps.close();
rs.close();
con.close();
int ret = mfs100.AutoCapture(data, timeout, false, false);
if(ret == 0){
int score = 0;
score = mfs100.MatchISO(data.ISOTemplate(), ISOTemplate);
System.out.println("Score:"+score);
if(score > 14000){
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Success:"+score, ButtonType.OK);
alert.show();
}else if (score >= 0 && score < 14000) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Failure:"+score, ButtonType.OK);
alert.show();
}
}
else{
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Return Code:"+ret+" "+mfs100.GetLastError(), ButtonType.OK);
alert.show();
}
但是当我使用PHP代码从数据库中获取数据时,它会以字符串格式获取数据。 我正在将字符串数据转换为字节并尝试将其与新指纹数据匹配,但没有取得任何成功,因为数据在字符串到单播转换期间丢失。 那么,如何使用PHP以字节格式从数据库获取数据而不丢失数据?