2015-09-06 37 views
-1

我有一个包含大型邮件正文的字符串。我需要以blob的形式将它保存到数据库中。如何将它转换为java中的blob?下面 样品字符串:如何在Java中将字符串转换为blob?

<html><center> <body style='background-color: #e5e4e2;'> <table id='mainTable' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'><tr> <td> <table style='width: 100%;background-color: #2B547E; height: 50px; margin-top: 30px; margin-left: auto; margin-right: auto;'> <tr> <td width='50%'><img src="cid:image" height='50' width='150' style='padding-left: 10px;'></td> <td width='50%' style='text-align: right; color: white; font-family: verdana; font-size: 12px;'> </td></tr> </table> </td> </tr></table> <table id='mainTable1' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'> <tr> <td> <table style='width: 100%; height: 50px; margin-top: 30px; margin-left: auto; margin-right: auto;'><tr> <td width='50%' style='color:red;font-size:21px;'>Congratulations, Joselyn Valdes-Flores </td> <td width='50%' style='text-align: right; color: white; font-family: verdana; font-size: 12px;'> </td> </tr> <tr> <td style='font-size:18px;width:97%;font-style:italic'> This email confirms your Phone Interview with Manager, Accounts Receivable in Burger King Corporation You will be called at your mobile number </td> </tr> </table> </td> </tr> </table> <table style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'><tr style='color:green'> Interview LineUp <th style='color:gray;font-size:21px;text-align:left;font-style: italic'>Interviewe</th><th style='color:gray;font-size:21px;text-align:left;font-style: italic'>Timing1</th><th style='color:gray;font-size:21px;text-align:left;font-style: italic'>Timing2</th></tr><tr><td>Jeet Chatterjee</td><td>12:00am</td><td>2:00am</td></tr><tr><td>Ramu</td><td>2:00am</td><td>4:00am</td></tr></table><table id='mainTable2' style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto; margin-top:12px;'><tr> <td style='font-size:18px; color:#2B547E;word-wrap: break-word;font-style: italic'> hii</td> </tr> </table> <table style='background-color: #ffffff; width: 70%; height: auto;margin-left: auto; margin-right: auto;'> <tr> <td style='font-size:18px;color:#2B547E;word-wrap: break-word;font-style: italic'> Best Regards,</td><td style='font-size:24px;color:#2B547E;word-wrap: break-word;font-style: italic'> Jeet Chatterjee</td></tr> </table></body> </center> </html> 

我想将它保存在BLOB列的数据库。这个怎么做??

+0

大多数数据库都将任意“字节”保存为BLOB,因此您可以将字符串的字节赋予数据库驱动程序。 –

+0

对于非二进制文本,CLOB将更适合数据库级别的文本操作。 –

回答

1

您可以通过获取byte[]内容从String

byte[] byteData = yourString.getBytes("UTF-8");//Better to specify encoding 
Blob blobData = dbConnection.createBlob(); 
blobData.setBytes(1, byteData); 

做到这一点。此外,你可以直接存储到StringBLOB

+0

为什么我需要dbConnection? – lucifer

+0

@lucifer要创建'blob'实例,您可以使用数据库连接对象。 –

+0

没有数据库连接我不能做blob对象? – lucifer

相关问题