简体   繁体   中英

Database connection with JDBC using Ucanaccess no output

I try to create a connection between JDBC and MS Access.

I follow the instruction as per this link . I am using 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 . But as you can see there is no error neither any output. Now I am sharing the table structure and content on the table.

在此处输入图像描述

2nd picture is在此处输入图像描述

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() . You should only use getResultSet() in combination with execute() .

A result set should only be obtained once, and it was already returned from executeQuery (which you ignored). When you called getResultSet , you - apparently - got an empty one (which technically violates the contract).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM