0

我正在使用 MS Access 2007 并尝试插入数据,我得到了异常,我已经尝试使用 [] 大括号,但它不起作用。它成功创建了 DBF 文件,但没有生成确切的输出。

           import java.sql.*;

           public class Test
           {
             public static void main(String[] args)
              {
               try 
                  {
                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                     String dataSourceName = "mdbTEST";
                     String dbURL = "jdbc:odbc:" + dataSourceName;
                     Connection con = DriverManager.getConnection(dbURL, "",""); 

          //  creating a java.sql.Statement so I can run queries

                    Statement s = con.createStatement();
                    s.execute("create table TESTME ( olumn_name integer )");
        // creating a table

       // inserting some data into the table

                    s.execute("insert into TESTME values(3)");

       // selecting the data from the table
                    s.execute("[select column_name from TESTME]");

       //getting any ResultSet that came from our query 

                    ResultSet rs = s.getResultSet();  
                   if (rs != null) 
       // if rs == null, then there is no ResultSet to view

                 while ( rs.next() ) 
                        {
        /* the next line will get the first column in our current row's ResultSet 
        as a String ( getString( columnNumber) ) and output it to the screen */ 

             System.out.println("[Data from column_name:]" + rs.getString(1) );
                        }

             s.execute("drop table TESTME");
             s.close(); 
             con.close(); 
                  }
            catch (Exception err)
                      {
                        System.out.println("ERROR: " + err);
                      }
        }
      }
4

1 回答 1

1

你的陈述

s.execute("[select column_name from TESTME]");

将不起作用,因为 Access SQL 使用方括号来分隔表名和列SELECT,因此您的 SQL“查询”由没有关键字的单个名称组成。我建议...

s.execute("SELECT [column_name] FROM [TESTME]");

CREATE TABLE...但是由于您的陈述中有错字,这可能行不通。尝试这个:

s.execute("SELECT [olumn_name] FROM [TESTME]");

编辑

以下代码适用于我:

import java.sql.*;

public class JDBCQuery {
    public static void main( String args[] )
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\Public\\Database1.accdb;");
            Statement s = conn.createStatement();
            s.execute("CREATE TABLE [TESTME] ([column_name] integer)");
            s.execute("INSERT INTO [TESTME] VALUES (3)");
            s.execute("SELECT [column_name] FROM [TESTME]");
            ResultSet rs = s.getResultSet();
            if (rs!=null)
            {
                while (rs.next())
                {
                    System.out.println("Data from column_name: " + rs.getString(1));
                }
            }
            s.execute("DROP TABLE [TESTME]");
            s.close();
            conn.close();
        }
        catch( Exception e ) {
            e.printStackTrace();
        }
    }
}

(请注意,Database1.accdb当我运行此代码时,数据库文件已经存在。)

于 2013-04-24T10:55:51.130 回答