dimanche 22 mars 2015

Where do I put my sqlite db file so my jar can access it? How does this affect the connection string?

I'm using Ant to build and run my simple java program, but am fairly shaky on how my file hierarchy is supposed to be laid out so that the program can access and modify a sqlite db file.


I have heard that I cannot include the db file in the jar, as that will cause it to become read only. My only other guess is to put it somewhere relative to the jar file (preferably the same directory?) but I'm not sure what to do to my ant build.xml file to reflect this when I execute 'ant run', nor am I sure how this modifies the jdbc connection string.


My current jdbc connection code:



logger.debug("Setting up database connection");
Class.forName("org.sqlite.JDBC");
dbConnection = DriverManager.getConnection("jdbc:sqlite:images.db");


My ant build.xml



<project name="Maower" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="lib.dir" value="lib"/>
<property name="log.dir" value="log"/>

<property name="main-class" value="good.maower.Maower"/>

<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<fileset dir="${src.dir}" includes="**/*.properties"/>
</path>

<target name="clean">
<delete dir="${build.dir}"/>
</target>

<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" includeantruntime="false"/>
<copy todir="${classes.dir}" overwrite="true">
<fileset dir="${src.dir}" includes="**/*.properties"/>
</copy>
</target>

<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>

<target name="run" depends="jar, input-runargs">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
<arg line="${args}"/>
</java>
</target>

<target name="input-runargs" unless="args" description="Ensures that subscriber email addresses are specified in command line arguments">
<input addProperty="args" message="List subscribers: "/>
</target>

<target name="clean-build" depends="clean,jar"/>

<target name="main" depends="clean,jar"/>

</project>

Aucun commentaire:

Enregistrer un commentaire