Archive

Posts Tagged ‘mysql’

Duplicate entry xxx for key xxx — MySQL and Hibernate

March 15th, 2007 Arun Manivannan No comments

This is one problem which ate around 2 hours of last night. Here are the possible solutions

1) There is a genuine case of duplication of the id column. Try querying the max (IDCOLUMN) from the table.

2) If the message says Duplicate entry ’127′ or ’32767′ or any of the max values in the following table

Type Bytes Minimum Value Maximum Value
    (Signed/Unsigned) (Signed/Unsigned)
TINYINT 1 -128 127
    0 255
SMALLINT 2 -32768 32767
    0 65535
MEDIUMINT 3 -8388608 8388607
    0 16777215
INT 4 -2147483648 2147483647
    0 4294967295
BIGINT 8 -9223372036854775808 9223372036854775807
    0 18446744073709551615

then you need to change your datatype. Your next increment value isn’t getting accommodated within the datatype.

3) If you have an increment id in your Hibernate, please check whether your datatype is varchar in mysql. If it is varchar in mysql and if your hibernate mapping file maps to an Integer property in your Value Object, this problem is sure to occur after your 9th record.

Categories: Uncategorized Tags: , , ,

Host not allowed to connect to mysql server

March 5th, 2007 Arun Manivannan No comments

The reason that we get this error in mysql when a remote machine tries to connect to mysql server is that by default mysql does not support remote access.

So, we need to specify the details of the host machine in the mysql server (the actual machine where mysql runs).

So, my machine’s IP is 192.168.1.10 and my friend’s machine (in which mysql runs is 192.168.1.20)

$ mysql -u root -p
mysql> use mysql;
mysql> grant all privileges on *.* to arun@192.168.1.10
identified by 'orange' with grant option;
mysql> exit

If you are not sure of the hostname, just replace the 192.168.1.10 with ‘%’

Categories: Uncategorized Tags: ,

Uploading Image/FormFile to MySQL using Struts and Hibernate

March 1st, 2007 Arun Manivannan No comments

I found this very interesting and thought I could post this here.

Let me jump directly to the code.

On your jsp, you have

<input id="productimage" name="productimage" type="file">

Your ActionForm will have

private FormFile productimage;

public FormFile getProductimage() {
return productimage;
}
public void setProductimage(FormFile productimage) {
this.productimage = productimage;
}

My product.hbm.xml has

<property
name="productimage"
type="blob"
column="productimage"
not-null="true"
length="250"
/>


My ActionClass has

Product product=new Product();
product.setProductimg(

product.toByteArrayImpl(adminForm.getProductimage()));
model.addProduct(factory, product);

And my model.addProduct() has


public boolean addProduct(SessionFactory factory, Product product) {
Session session =null;
try {
session = factory.openSession();
session.save(product);
} catch (Exception e) {
e.printStackTrace();
return false;
}
finally{
session.flush();
session.close();

}

return true;

}

and my ProductVO has

private byte[] productimg;
private Blob productimage;

/** Don't invoke this. Used by Hibernate only. */
public void setProductimage(Blob productimage) {
this.productimg = this.toByteArray(productimage);
}

/** Don't invoke this. Used by Hibernate only. */
public Blob getProductimage() {
return Hibernate.createBlob(this.productimg);
}


public byte[] getProductimg() {
return productimg;
}
public void setProductimg(byte[] productimg) {
this.productimg = productimg;
}

public byte[] toByteArray(Blob fromBlob) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
return toByteArrayImpl(fromBlob, baos);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException ex) {
}
}
}
}

public byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos)
throws SQLException, IOException {
byte[] buf = new byte[4000];
InputStream is = fromBlob.getBinaryStream();
try {
for (;;) {
int dataSize = is.read(buf);

if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
}
}
}
return baos.toByteArray();
}

public byte[] toByteArrayImpl(FormFile formfile)
throws SQLException, IOException {
byte[] buf = new byte[4000];
InputStream is = formfile.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
for (;;) {
int dataSize = is.read(buf);

if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
}
}
}
return baos.toByteArray();
}