填充文本框输入db?
我正在编写一个CRUD应用程序,并且想要将我的文本框输入填充到数据库中,但我得到一个异常:
这是我的方法:
public void create(Produkt p) {
if(p==null) {
log.error("Erstellen von null-Objekt in DB nicht möglich");
throw new IllegalArgumentException("Erstellen von null-Objekten in der DB nicht möglich");
}
if(p.getName().length()>30) {
log.error("Produktname zu lang!");
}
PreparedStatement ps=null;
try {
ps = hsqlmanager.getConnection().prepareStatement("INSERT INTO Produkt(name, kategorie, geloescht, haltbar, preis, altersfreigabe, bild) VALUES(?, ?, ?, ?, ?, ?, ?);");
} catch (SQLException e1) {
log.error("Es konnte keine Verbindung hergestellt werden im DAOProdukt!");
e1.printStackTrace();
}
try {
//TODO
ps.setString(1, p.getName());
ps.setString(2, p.getKategorie());
ps.setBoolean(3, p.isGeloescht());
ps.setBoolean(4, p.isHaltbar());
ps.setDouble(5, p.getPreis());
ps.setBoolean(6, p.isAltersfreigabe());
ps.setString(7, p.getBild());
System.out.println(p.getName() + p.getKategorie() + p.getPreis() + p.getBild() + p.isGeloescht() + p.isGeloescht() + p.isHaltbar());
ps.execute();//here I get the Exception
ps.close();
log.info("Produkt in DB erstellt.");
}
catch (SQLException e) {
log.error("createtes Produkt konnte nicht gespeicher werden - SQLFehler: " + e.toString());
e.printStackTrace();
}
}
这里是我填写文本框的地方:
String name=textBoxes.get(0).getText();
if(name.trim().isEmpty()) {
name="NO NAME ADDED!";
} else{
name = textBoxes.get(0).getText();
}
我想这跟这个名字有关,因为这个例外是这样说的:
Java.sql.SQLDataException: data exception: string data, right truncation
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.execute(Unknown Source)
at dao.DAOProdukt.create(DAOProdukt.java:50)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown
源)在java.awt.Component.processEvent(Unknown Source)上的java.awt.Component.processEvent(Unknown Source)在javax.swing.JComponent.processMouseEvent(Unknown Source) java.awt.Component.dispatchEventImpl(Unknown Source)at java.awt.Container.dispatchEventImpl(Unknown Source)at java.awt.Component.dispatchEvent(Unknown Source)at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)at java .awt.LightweightDispatcher.processMouseEvent(Unknown Source)at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)at java.awt.Container.dispatchEventImpl(Unknown Source)at java.awt.Window.dispatchEventImpl(Unknown Source)at java.awt .Component.dispatchEvent(Unknown Source)at java.awt.EventQueue.dispatchEventImpl(Unknown Source)at java.awt.EventQueue.access java.awt.EventQueue上的$ 200(Unknown Source)java.awt上的$ 3.run(Unknown Source) .EventQueue $ 3.run(未知来源)位于java.security.AccessController.doPrivileged(Nati (方法)在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源)在java.awt.EventQueue $ 4.run(未知源)在java.awt.EventQueue $ 4。在java.security.ProtectionDomain的java.security.AccessController.doPrivileged(Native Method)上运行(未知源代码)java.awt.EventQueue.dispatchEvent(Unknown Source)上的java.util.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)java.awt.EventDispatchThread.pumpOneEventForFilters (Unknown Source)java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at java.awt.EventDispatchThread.pumpEvents(Unknown源)在java.awt.EventDispatchThread.run(未知源)
那是什么意思? 异常指向ps.execute();
但我使用的是正确的类型? 为什么?