如何计算ms访问数据库中的行数

时间:2021-06-13 20:14:28

I want to count the number of rows in the ms access table"testing" and assign it to a variable "i".right now i am able to print the counted number of rows in my table from the query variable. i want to assing in to variable "i".my second question is ,the sql statement to count is it correct and should i use any other query instead of that?

我想计算ms访问表“testing”中的行数,并将其分配给变量“i”。现在,我可以从查询变量中打印表中计算的行数。我想参与变量“我”。我的第二个问题是,要计数的sql语句是否正确,我应该使用任何其他查询而不是那个?

package amogh.java;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class readCSVnew1 {

    public static void main(String[] args) throws IOException {
    // // read 2.txt
    // create token1
    String token1 = "";
    // create Scanner inFile1
    Scanner inFile1 = new Scanner(new File("D:\\Warface     Launcher\\eclipse is fk\\something\\src\\2.txt")).useDelimiter(" ");

    // ArrayList 
   // List<String> temps = new LinkedList<String>();
    List<String> temps = new ArrayList<String>();

    // while loop
    while (inFile1.hasNext()) {
      // find next line
      token1 = inFile1.next();
      temps.add(token1);
    }
    inFile1.close();
    int i;
    int count = 1;
    String[] tempsArray = temps.toArray(new String[0]);
    for (String s : tempsArray) {
      System.out.println(s);
      try
      {   

          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          Connection     con=DriverManager.getConnection("jdbc:odbc:testing");
          PreparedStatement ps = con.prepareStatement("insert into     testing (ID,fileName)values(?,?)");

              ps.setInt(1,count);
              ps.setString(2,s);
              String query ="SELECT * COUNT(ID) FROM testing";
              System.out.println(query);
              System.out.println(s);
              System.out.println("inserted");
              count++;


              ps.executeUpdate();
              con.close();       
      }

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

    }
  }
}

1 个解决方案

#1


0  

your query is syntactical wrong it must be:

您的查询语法错误必须是:

SELECT COUNT(*) FROM testing

And you newer execute this selectquery in your code.

并且您在代码中更新执行此selectquery。

#1


0  

your query is syntactical wrong it must be:

您的查询语法错误必须是:

SELECT COUNT(*) FROM testing

And you newer execute this selectquery in your code.

并且您在代码中更新执行此selectquery。