gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:30:07 24 4
gpt4 key购买 nike

您好,我正在用 Java 开发一个系统,我正在尝试获取我表中存在的所有记录的计数,我尝试了很多,但它给了我异常异常(exception)是: - 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();

}


}

最佳答案

要么你必须使用你当前的代码:

 String getc = rsc.getString(1);

或改变:

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

然后:

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

编辑:1

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

编辑 2:

你必须使用

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

因为PreparedStatement是一组预编译的query

关于java - 如何从 Access JDBC ODBC 数据库的表中获取 java 中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15377582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com