简体   繁体   English

无法连接到SQLite数据库

[英]Can't connect to a SQLite database

try {
con = DriverManager.getConnection("jdbc:sqlite:db/Freepark.sqlite");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("error al buscar la base de datos");
}

I am trying to do my first queries on an SQL database but I am having problems connecting to it, I think the problem is the URL for sure, the project name is BaseTest and inside project folder I have a subfolder called DB and inside it it's Freepark.sqlite. 我正在尝试在SQL数据库上进行第一个查询,但连接时遇到问题,我认为问题肯定是URL,项目名称是BaseTest,并且在项目文件夹中有一个名为DB的子文件夹,并且在其中Freepark.sqlite。 When I run the project the println message appears so I know that the problem is the url. 当我运行项目时,出现println消息,因此我知道问题出在URL。 Things like class.forName and so are already done above this code sample. 在此代码示例上方已经完成了诸如class.forName之类的工作。

Why dont you try either putting in the name with the relative path \\ like: db\\Freepark.sqlite 为什么不尝试使用相对路径\\来输入名称,例如:db \\ Freepark.sqlite

or also try putting the full path of the sqlite file. 或尝试放置sqlite文件的完整路径。

Also are you including before the statements to enable the driver for sqlite such as: 您还包括在语句之前为sqlite启用驱动程序,例如:

 Class.forName("SQLite.JDBCDriver").newInstance(); 

or 要么

 Class.forName("org.sqlite.JDBC"); 

use this example 使用这个例子

import java.sql.*; 导入java.sql。*;

public class Test { 公开课测试{

public static void main(String[] args) throws Exception { 公共静态void main(String [] args)引发异常{

Class.forName("org.sqlite.JDBC");

Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");

Statement stat = conn.createStatement();

stat.executeUpdate("drop table if exists people;");

stat.executeUpdate("create table people (name, occupation);");

PreparedStatement prep = conn.prepareStatement(
  "insert into people values (?, ?);");

prep.setString(1, "Gandhi");
prep.setString(2, "politics");
prep.addBatch();
prep.setString(1, "Turing");
prep.setString(2, "computers");
prep.addBatch();
prep.setString(1, "Wittgenstein");
prep.setString(2, "smartypants");
prep.addBatch();

conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);

ResultSet rs = stat.executeQuery("select * from people;");
while (rs.next()) {
  System.out.println("name = " + rs.getString("name"));
  System.out.println("job = " + rs.getString("occupation"));
}
rs.close();
conn.close();

} }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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