简体   繁体   中英

IntelliJ extract data from sqlite database file?

I just started with sqlite and I stuck with a strange (maybe just for me) phenomenon. When I connect the testDB.db file in java, and make one or some query, the data and the table itself is disappearing. The consol said that SQL error or missing database, and when I check the database file in cmd, the situation is really that; there is no data in the file. Could anybody help me out with this basic problem? (I suppose that this is just because of the lack of my knowledge in this topic, but I'm open to new information)

public class jdbcTest{

public static void main(String[] strg) {

    Connection connection = null;
    try {
        connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Username\\Documents\\sqlite\\testDB");
        Statement statement = connection.createStatement();
        statement.setQueryTimeout(30);

        //statement.executeUpdate("drop table if exists person");
        ResultSet rs = statement.executeQuery("select * from company");
        while (rs.next()){
            System.out.print("id = "+rs.getInt("id")+"   ");
            System.out.println("name = "+rs.getString("name"));
        }
    }
    catch (SQLException e) {
        System.err.println(e.getMessage());
    }
    finally {
        try {
            if (connection!=null){
                connection.close();
            }
        }
        catch (SQLException e){
            System.err.println(e);
        }
    }
}

}

When opening a nonexistent database file, SQLite will happily create a new, empty one.

The file name you're giving to getConnection does not actually point to the database you saved.

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