0

我正在编写一个 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.processMouseEvent(Unknown Source) 在 javax.swing.JComponent.processMouseEvent(Unknown Source) 在 java.awt.Component.processEvent(Unknown Source) 在 java.awt.Container.processEvent(Unknown Source)在 java.awt.Component.dispatchEventImpl(Unknown Source) 在 java.awt.Container.dispatchEventImpl(Unknown Source) 在 java.awt.Component.dispatchEvent(Unknown Source) 在 java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 在 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 。零件。dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3 .run(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue 的 java.security.AccessController.doPrivileged(Native Method) $4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt。 java.awt 中的 EventQueue.dispatchEvent(Unknown Source)。EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread。 java.awt.EventDispatchThread.run(未知源)上的 pumpEvents(未知源)

那是什么意思?异常指向ps.execute();但我使用的是正确的类型?为什么?

4

0 回答 0