[英]Database connection with JDBC using Ucanaccess no output
I try to create a connection between JDBC and MS Access.我尝试在 JDBC 和 MS Access 之间建立连接。
I follow the instruction as per this link .我按照此链接的说明进行操作。 I am using IntelliJ Idea.
我正在使用 IntelliJ Idea。 Here I am sharing some snaps to describe my problem.
在这里,我分享一些快照来描述我的问题。
This is the code that I write down to make a connection with Database Database2
.这是我写下的与 Database
Database2
建立连接的代码。 But as you can see there is no error neither any output.但正如您所见,output 也没有错误。 Now I am sharing the table structure and content on the table.
现在我正在分享桌子上的表格结构和内容。
My code is:我的代码是:
import java.sql.*;
public class Connection_sample {
public static void main(String[] args) {
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn= DriverManager.getConnection("jdbc:ucanaccess://D://tutorial/Database2.accdb");
Statement s = conn.createStatement();
s.executeQuery("select * from Student");
ResultSet rset = s.getResultSet();
while (rset.next()) {
System.out.println(rset.getInt(1)+""+rset.getInt(2));
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Can anyone help me to find the error?谁能帮我找出错误?
Your problem is the result of using getResultSet()
instead of using the result set returned by executeQuery()
.您的问题是使用
getResultSet()
而不是使用executeQuery()
返回的结果集的结果。 You should only use getResultSet()
in combination with execute()
.您应该只将
getResultSet()
与execute()
结合使用。
A result set should only be obtained once, and it was already returned from executeQuery
(which you ignored).结果集应该只获取一次,并且它已经从
executeQuery
返回(您忽略了它)。 When you called getResultSet
, you - apparently - got an empty one (which technically violates the contract).当您调用
getResultSet
时,您 - 显然 - 得到了一个空的(这在技术上违反了合同)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.