【发布时间】:2018-05-10 14:07:42
【问题描述】:
我在 MacOS 上使用 SQLite 和 java 发出一个非常简单的选择时遇到问题。
这是我的代码:
public class TestJDBC1
{
public static Connection connect_DB() throws ClassNotFoundException {
Class.forName("org.sqlite.JDBC");
Connection conn = null;
try {
// db parameters
String url = "jdbc:sqlite:/Users/Shared/DB_Farmaci/oracle-sample.db";
// create a connection to the database
conn = java.sql.DriverManager.getConnection(url);
System.out.println("Connection a SQLite stabilita.");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public static void main( String[] args ) throws ClassNotFoundException, InterruptedException, SQLException
{
Connection conn = connect_DB();
try
{
conn.setAutoCommit(false);
} catch (SQLException ex) {
Logger.getLogger(TestJDBC1.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
Statement S = null ;
ResultSet rs = null ;
S = conn.createStatement();
String queryS = "select deptno, dname, loc from dept ;" ;
rs = S.executeQuery(queryS);
S.close();
conn.close();
int x = 0;
while (rs.next())
{
x++;
System.out.println(rs.getInt("deptno") + "\t" +
rs.getString("dname") + "\t" +
rs.getDouble("loc"));
}
if (x > 0)
{
System.out.println("# rows returned => " + x);
} else
{
System.out.println("no rows returned");
}
} catch (SQLException se) {
System.out.println(se);
System.out.println("errore DB");
}
}
如您所见,它确实很简单,但它不返回任何数据。
我的环境如下:
- NetBeans 8.2
- sqlite-jdbc-3.21.0.jar(当前版本)
使用类似的代码,我可以创建表并将行插入数据库,因此环境的设置应该没问题(属性=>库=>编译和运行)。
我也在 Eclipse 上进行了尝试,但结果相同。我还尝试了我发现的所有向后的 JDBC 驱动程序版本,同样的情况。我正在使用 SQLite 3.10.1 的 DB Browser,我可以读取我要查找的数据。
我的 Mac 上安装了 SQLite,我可以使用命令行命令读取数据。
这看起来像是驱动程序故障,除非我遗漏了一些非常重要的东西。
【问题讨论】: