0

提出しなければならないプロジェクトがあり、完了するのに苦労しています。Microsoft Access データベースに情報を挿入する必要があります。UCanAccess を使用してデータベースに接続できましたが、挿入クエリが機能していないようです。これは、データベースにシャツを挿入しようとする私の方法の1つです(アフリカーンス語を許してください)

   public void InsertHemp2(){

     try {

         String inNuweBeskrywing = JOptionPane.showInputDialog("Hoe sal u die kledingstuk beskryf?"
                + " Bv Slaaphemp");
        int inVlakVanDeftigheid = Integer.parseInt(JOptionPane.showInputDialog("Op 'n skaal van 1 tot 10,"
                + " hoe deftig sal u die kledingstuk beskryf? Bv 'n pak is 'n 10"));
        String inKleur = JOptionPane.showInputDialog("Wat is die kleur van die kledingstuk?");

        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

        conn = DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\Neil\\Documents\\Skool\\Neil\\Matriek\\IT PAT\\Neil\\TrekRegAan '15 Code\\TrekRegAan_(3)\\TrekRegAan\\TrekRegAan'15\\TrekRegAan'15.accdb");
        st = conn.createStatement();
      st = conn.createStatement();
      String sql = ("INSERT INTO tblHemde (Beskrywing, VlakVanDeftigheid, Kleur)"
                + " SELECT " + "'" + inNuweBeskrywing + "'" + " AS Expr1" + "," + "'" + inVlakVanDeftigheid + "'"
                + " AS Expr2"  + "," + "'" + inKleur + "'" + " AS Expr3" + ";");
      st.executeQuery(sql);




     } catch (Exception e) {
         System.out.println(e);
         e.printStackTrace();
     }

}

String sql をコピーしてアクセスでクエリとして実行すると、レコードが挿入されるため、クエリに問題があるとは思えません。次のエラーが表示されます。

net.ucanaccess.jdbc.UcanaccessSQLException: unexpected token: ;
net.ucanaccess.jdbc.UcanaccessSQLException: unexpected token: ;
    at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:202)
    at Klasse.Hemp.InsertHemp2(Hemp.java:105)
    at GUI.GUI.jButtonVoegKlereByActionPerformed(GUI.java:148)
    at GUI.GUI.access$000(GUI.java:21)
    at GUI.GUI$1.actionPerformed(GUI.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6516)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.sql.SQLSyntaxErrorException: unexpected token: ;
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
    at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:199)
    ... 40 more
Caused by: org.hsqldb.HsqlException: unexpected token: ;
    at org.hsqldb.error.Error.parseError(Unknown Source)
    at org.hsqldb.ParserBase.unexpectedToken(Unknown Source)
    at org.hsqldb.ParserDQL.XreadSelect(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQuerySpecification(Unknown Source)
    at org.hsqldb.ParserDQL.XreadSimpleTable(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryPrimary(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryTerm(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryExpressionBody(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryExpression(Unknown Source)
    at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source`enter code here`)
    ... 43 more
4

1 に答える 1

1

エラーを再現できました。UCanAccess は、その形式の "INSERT ... SELECT ..." (FROM なし) を好まないようです。ただし、動的 ​​SQL を使用してそのようなステートメントを作成するべきではありません。アプリケーションがSQL インジェクションの脆弱性やその他の問題にさらされる可能性があるためです。

代わりに、次のようなパラメーター化されたクエリを使用する必要があります。

String sql = 
        "INSERT INTO tblHemde (Beskrywing, VlakVanDeftigheid, Kleur) " +
        "VALUES (?,?,?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
    ps.setString(1, inNuweBeskrywing);
    ps.setInt(2, inVlakVanDeftigheid);
    ps.setString(3, inKleur);
    ps.executeUpdate();
}
于 2015-06-07T13:02:15.670 に答える