您好我正在开发一个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();
}
}
答案 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");