2014-01-18 38 views
0

,直到我升级到Java 7(我猜测) HOWTO创建从Java连接数据库的语法我的代码附上sqlitedsb到另一个工作得很好?我的语法使用SQL-Tool可以正常工作,但是来自JAVA的我没有成功。附加DB SQLite中从Java

我的代码:

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class TesteAttachDB { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     testattach(); 
    } 

    public static void testattach(){ 

     Connection connection_historymapsdb = null; 
     String sTargetDB="C://temp//historydb11_maps_en.db"; 
     try { 
      Class.forName("org.sqlite.JDBC"); 
       connection_historymapsdb = DriverManager.getConnection("jdbc:sqlite:" + sTargetDB); 
     } catch (ClassNotFoundException e1) { 
      e1.printStackTrace(); 
     } catch (SQLException e2) { 
      e2.printStackTrace(); 
     } 
     Statement statement; 
     try { 
      //String sDatabasetoattach="C://temp//test.db"; 
      //String sDatabasetoattach="C:/temp/test.db"; 
      String sDatabasetoattach="C:\temp\test.db"; 
      //String sDatabasetoattach="C:\temp\\userdb\test.db"; 
      statement = connection_historymapsdb.createStatement(); 
      String sSQL="Attach '" + sDatabasetoattach + "' as chronica_maps_tests"; 
      System.out.println(sSQL); 
      statement.execute(sSQL); 
      String sTestSQL="select count(*) from chronica_maps_tests.testtable"; 
      statement.execute(sTestSQL); 
      System.out.println("worked."); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

错误消息:

答:

Attach 'C://temp//test.db' as chronica_maps_tests 
java.sql.SQLException: unable to open database: C://temp//test.db 
    at org.sqlite.DB.execute(DB.java:275) 
    at org.sqlite.Stmt.exec(Stmt.java:56) 
    at org.sqlite.Stmt.execute(Stmt.java:83) 
    at maps.TesteAttachDB.testattach(TesteAttachDB.java:48) 
    at maps.TesteAttachDB.main(TesteAttachDB.java:15) 

B:

Attach 'C:/temp/test.db' as chronica_maps_tests 
java.sql.SQLException: unable to open database: C:/temp/test.db 
    at org.sqlite.DB.execute(DB.java:275) 
    at org.sqlite.Stmt.exec(Stmt.java:56) 
    at org.sqlite.Stmt.execute(Stmt.java:83) 
    at maps.TesteAttachDB.testattach(TesteAttachDB.java:48) 
    at maps.TesteAttachDB.main(TesteAttachDB.java:15) 

C:

Attach 'C: emp est.db' as chronica_maps_tests 
java.sql.SQLException: no such table: chronica_maps_tests.testtable 
hat funktioniert ! 
    at org.sqlite.DB.throwex(DB.java:288) 
    at org.sqlite.NestedDB.prepare(NestedDB.java:115) 
    at org.sqlite.DB.prepare(DB.java:114) 
    at org.sqlite.Stmt.execute(Stmt.java:82) 
    at maps.TesteAttachDB.testattach(TesteAttachDB.java:52) 
    at maps.TesteAttachDB.main(TesteAttachDB.java:15) 

enter image description here

我尝试不同的语法(A,B,C)为sSQL,但毫无效果。 SQL字符串应该如何?

+0

您有特权吗?尝试在不同的驱动器。 – Keerthivasan

+0

是的,我有权限访问。已经尝试过。那不是问题。 –

+0

您正在使用单个('),尝试在文件名周围使用双引号(“)(也显示在[this](http://stackoverflow.com/a/9068549/3080094)answer)。 “C:\\ temp \\ test.db”(逃避Java代码中的转义)将起作用吗? – vanOekel

回答

0

在Java中的字符串,\是转义字符。 \t是水平制表控制字符,你可以在邮件中看到:

Attach 'C: emp est.db' as chronica_maps_tests 

正确的语法是这样的:

String sDatabasetoattach = "C:\\temp\\test.db"; 
+0

谢谢,但我也看到了,这只是为了表明我试过每种类型的语法。 VirtualMachine,我用“C://temp//test.db”开始了相同的代码,并且它可以工作!与此同时,我更新到JAVA 7并猜测这就是原因w它不工作了。我没有更改代码。我从旧的虚拟机中复制了相同的代码,并且在新的虚拟机中不起作用? –

0

我添加了一个新的JDBC驱动程序SQLite和它的工作。我猜这个驱动程序在JAVA 7中无法正常工作。我的旧版JDBC驱动程序是从2011年开始的。