我正在用java構建一個計費應用程序。在我的JTable
可能有n行數。我如何將它壓縮到數據庫然後從那裏檢索它?我如何在數據庫中存儲entier jtable n並對其進行修改
例如,假設有一個客戶,ABC
,並且他們在此列表或帳單中有100個項目。如何將這100個項目和相關列字段(即完整JTable
)放在數據庫的單個列中?
我正在使用MySQL。
JTable table = new JTable(row,column);
TableModel tm = table.getModel();
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(table.getModel());
oos.flush();
oos.close();
byte[] b = baos.toByteArray();
java.sql.Connection con=null;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ankit","root","");
java.sql.Statement stmt =con.createStatement();
String maketable = "CREATE TABLE if not exists contacttable(Name Varchar(25),Position Varchar(20),Phone Varchar(20))";
stmt.executeUpdate(maketable);
System.out.print("table created ");
//PreparedStatement pstmt = null; ;
// pstmt.setBytes(1, b);
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("INSERT INTO contacttable VALUES(?,?,?)");
pstmt.setBytes(1, b);
// . . .
簡短的回答,不要。數據庫如何工作以及如何使用序列化也不是這樣。這種方法可能會使您的數據在將來無法使用,並且在應用程序的不同實例之間不兼容 – MadProgrammer
那麼我應該如何保存帳單數據?將其映射到客戶ID – ankit12
規範化的數據庫應該做到這一點。如果你不知道這意味着什麼,我已經聽說過關於這本書的數據庫設計的好消息。 –