将信息从Netbeans IDE插入Microsoft Access数据库

时间:2015-06-06 13:41:02

标签: ms-access jdbc ucanaccess

我有一个必须提交的项目,我正在努力完成。我需要将信息插入Microsoft Access数据库。我已经设法使用UCanAccess连接到数据库,但我的插入查询似乎不起作用。这是我尝试将衬衫插入数据库的方法之一(请原谅南非荷兰语)

   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

1 个答案:

答案 0 :(得分:1)

我能够重新创建你的错误。似乎UCanAccess不喜欢“INSERT ... SELECT ...”的形式(没有FROM)。但是,您确实不应该使用动态SQL来构造这样的语句,因为它会使您的应用程序对 SQL Injection 漏洞和其他麻烦开放。

相反,您应该使用参数化查询,如下所示:

int i = string.indexOf("");
if (i >= 0) {
    // do something with i here.
}