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



嗨,我正在开发一个java系统,我试图得到我的表中存在的所有记录的计数,我尝试了很多,但它给我例外异常:- java.sql.SQLException: Driver不支持此函数

下面是我的代码

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();
  }

  } 

您必须在当前代码中使用:

 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是一个预编译的查询集

您可以尝试以下方法,

Select count(stdid) c from record
String getc = rsc.getString("c");

相关内容

  • 没有找到相关文章

最新更新