[英]Store and retrieve Boolean Object to a MySql database
This is my first question here, but before asking to you I have searched all around the Internet without solution. 这是我在这里的第一个问题,但是在问您之前,我已经在Internet各处搜索了所有解决方案。
I want to store Java Objects in a MySql database and to do so I'm using the data type BLOB for all the objects. 我想将Java对象存储在MySql数据库中,并且这样做是针对所有对象使用数据类型BLOB。 The MySql table is:
MySql表是:
CREATE TABLE settaggi_variabili_pc (`id` int(11) NOT NULL AUTO_INCREMENT, PC1 BLOB, PC2 BLOB, PC3 BLOB, PC4 BLOB, PC5 BLOB, PC6 BLOB, PC7 BLOB, PC8 BLOB, PC9 BLOB, PC10 BLOB, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
In there I have to store Java objects like Color, Font and Boolean: 我必须在其中存储Java对象,例如Color,Font和Boolean:
prpdStmnt = con.prepareStatement("UPDATE settaggi_variabili_pc SET PC1=?, PC2=?, PC3=?, PC4=?, PC5=?, PC6=?, PC7=?, PC8=?, PC9=?, PC10=? WHERE id=?");
for (int i = 0; i < vDati.size(); i++)
{
Variabile_variabili_pc vpc = (Variabile_variabili_pc) vDati.get(i);
Object o = null;
if (vpc.getOggetto() instanceof Color)
o = (Color) vpc.getOggetto();
else if (vpc.getOggetto() instanceof Font)
o = (Font) vpc.getOggetto();
else if (vpc.getOggetto() instanceof JSwitchButton)
o = (Boolean)((JSwitchButton) vpc.getOggetto()).isSelected();
prpdStmnt.setInt(11, i + 1);
for (int x = 1; x <= 10; x++)
{
prpdStmnt.setObject(x, o);
}
prpdStmnt.executeUpdate();
The JSwitchButton
is a custom class that extends AbstractButton. JSwitchButton
是扩展AbstractButton的自定义类。 All this code works right. 所有这些代码都正确运行。
My problem is when I try to retrieve the Boolean Object, this is the code: 我的问题是当我尝试检索布尔对象时,这是代码:
ritorno = db.eseguiQuery("SELECT PC1, descrizione FROM settaggi_variabili_pc");
try
{
while(ritorno.next())
{
InputStream is = ritorno.getBlob(1).getBinaryStream();
ObjectInputStream ois = new ObjectInputStream(is);
o = ois.readObject();
if (o.getClass().equals(Color.class))
System.out.println("Object type Color");
else if (o.getClass().equals(Font.class))
System.out.println("Object type Font");
else if (o.getClass().equals(Boolean.class))
System.out.println("Object type Boolean");
}
}
catch (SQLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
When the ObjectInputStream
read the Boolean object the code return a java.io.EOFException
error. 当
ObjectInputStream
读取布尔对象时,代码将返回java.io.EOFException
错误。 This error come becouse the Boolean Object, during the store action, was tranlated to TINYINT value in MySql database and it is not stored as a Boolean Object. 发生此错误的原因是,在存储操作期间,布尔对象已在MySql数据库中转换为TINYINT值,并且未存储为布尔对象。
So the question is: how can I store a Boolean object to a BLOB MySql Type and how can I retrieve it to a Boolean object again like I done with Color or Font? 所以问题是:如何将布尔对象存储到BLOB MySql类型,又如何像使用Color或Font一样再次将其检索到布尔对象? Is there a way so I can store the Boolean like an Object and not like a primitive type?
有没有办法让我可以像对象而不是原始类型一样存储布尔值?
Thank you to all. 谢谢你们。
First of all, as @Jorge Campos pointed out, what you have is not a good structure for storing things in a database. 首先,正如@Jorge Campos所指出的那样,您拥有的不是一个用于在数据库中存储内容的良好结构。 Even for your application, simple configuration data would better be stored in some sort of flat text format ( ie INI Files or similar. If you really must use a database, you should certainly think about using either an object oriented database or even an Object-Relational Mapping (ORM) tool (such as Hibernate ) sitting on-top of MySQL.
即使对于您的应用程序,简单的配置数据也最好以某种平面文本格式( 即 INI文件或类似文件)存储。如果您确实必须使用数据库,则一定要考虑使用面向对象的数据库 ,甚至使用Object-位于MySQL顶部的关系映射(ORM)工具(例如Hibernate )。
Getting down to an actual answer , Just change the line else if (o.getClass().equals(
Boolean .class))
in your code above (note the bold), to use a more appropriate class, such as Integer
or Byte
- that way when the TINYINT is returned fro MySQL you will be able to represent it. else if (o.getClass().equals(
实际答案 ,只需在上面的代码else if (o.getClass().equals(
Boolean .class))
换行(请注意粗体),以使用更合适的类,例如Integer
或Byte
这样,当TINYINT从MySQL返回时,您将能够表示它。
As an aside, I think you would benefit from The StackExchange code review site https://codereview.stackexchange.com/ . 顺便说一句 ,我认为您将从StackExchange代码审查站点https://codereview.stackexchange.com/中受益。 Your code is VERY sloppy, and would certainly benefit from a review.
您的代码非常草率,一定会从审查中受益。 You can have your code reviewed without changing your database, if you really want.
如果确实需要,您可以在不更改数据库的情况下检查代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.