#1 25.03.08 20:18
Java+mySQL. вопрос по записи/чтению.
Вот собственно функции записи и чтения в/из БД mySQL.
Проблема в том, что в поле ADCdata (longblob) пишется одно, а читается другое. В чем может быть дело, собственно?
public void loadFromDB(Long id){
try{
Connection conn = J1.util.J1FW.getConnection();
Statement stmt = conn.createStatement();
ResultSet set = stmt.executeQuery("select * from AdcData where id = "+id);
if(!set.next()) throw new Exception("AdcData with id="+id+" doesn't exist");
this.id = id;
expID = new Long(set.getLong("Exp_ID"));
name = set.getString("Name");
chanel = set.getInt("Chanel");
gain = set.getInt("Gain");
InputStream is = set.getBinaryStream("ADCdata");
DataInputStream dis = new DataInputStream(is);
data = new double[dis.available()/4];
int i=0;
while(dis.available()>0){
byte b1 = dis.readByte();
byte b2 = dis.readByte();
byte b3 = dis.readByte();
byte b4 = dis.readByte();
data[i++] = Float.intBitsToFloat (((b4&0xff)<<24) | ((b3&0xff)<<16) | ((b2&0xff)<< 8) | ((b1&0xff)<< 0));
}
dis.close();
is.close();
set.close();
stmt.close();
conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
//*************************************************************
public static void saveToDB(Vector adcdat, long exp_id)
{
Object[] d = adcdat.toArray();
int len = d.length;
float[] fdat = new float[len];
try
{
for(int i = 0; i<len; i++)
{
fdat[i] = Float.parseFloat(d[i].toString());
}
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bout);
float k;
byte[] bytes = new byte[fdat.length*4];
byte[] tmp;
for(int i=0; i<fdat.length; i++)
{
k = fdat[i];
out.writeFloat(k);
out.flush();
tmp = bout.toByteArray();
for(int j=0; j<4; j++)
bytes[i*4 + j] = tmp[i*4 + j];
}
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
Connection conn = J1.util.J1FW.getConnection();
String iqu = "insert into adcdata (exp_id, adcdata) values (?, ?)";
PreparedStatement psmt = conn.prepareStatement(iqu);
psmt.setString(1, ""+exp_id);
psmt.setBinaryStream(2, bin, bin.available());
psmt.executeUpdate();
psmt.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
Исправлено TieRR (25.03.08 20:36)
Offline

