如何从Access JDBC ODBC数据库的表中获取java中的行数

时间:2013-03-13 05:16:30

标签: java netbeans ms-access-2003

您好我正在开发一个java系统我试图获取我的表中存在的所有记录的计数我尝试了很多,但它给了我一个例外 例外情况是:   - java.sql.SQLException:驱动程序不支持此功能

这是我的代码。

import java.sql.*;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;
public class myfram2 extends javax.swing.JFrame {
Connection con;
PreparedStatement ps;//I have also tried Statement but it give me exception that:
                     //Column not found     
public myfram2() {
   try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con= con=DriverManager.getConnection("jdbc:odbc:student");

    JOptionPane.showMessageDialog(rootPane,"Connection succeed");
    }catch(Exception ex){

    ex.printStackTrace();

    }

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
ResultSet rsc;
    try{
   //Here i am using sql count method and also tried max but it doesn't work
    String sqcount="Select count(stdid)from record";

  ps=con.prepareStatement(sqcount);

  rsc=ps.executeQuery(sqcount);
  if(rsc.next()){

  String getc= rsc.getString("count(stdid)");
  searchtx.setText(getc);

  }

    }
  catch(Exception ex){

  ex.printStackTrace();

  }


  } 

2 个答案:

答案 0 :(得分:2)

您必须使用当前代码:

 String getc = rsc.getString(1);

或更改:

  String sqcount = "Select count(stdid) countStdID from record";

然后:

String getc = rsc.getString("countStdID");

修改:1

您必须首先加载Driver Class,然后从该驱动程序类获取连接,并在程序中获得该连接。

编辑2:

您必须使用

rsc = ps.executeQuery();    // instead of rsc = ps.executeQuery(sqcount);

因为PreparedStatement是一组预编译的查询

答案 1 :(得分:0)

您可以尝试以下方式,

Select count(stdid) c from record

String getc = rsc.getString("c");